Commit d95a40f
fix(json-paste): measure the size guard in UTF-8 bytes, not code units
Addresses the PR #27 review (Copilot). The guard is named/documented as a byte
cap (MAX_BYTES / maxBytes) but checked `trimmed.length` — UTF-16 code units. For
non-ASCII pastes that undercounts the real size (a CJK char is 1 code unit but 3
UTF-8 bytes), so a payload up to ~3× the intended cap could slip past and get
parsed/stringified on paste, defeating the ext-host-stall protection.
Switched the check to Buffer.byteLength(trimmed, 'utf8') so the cap means what
its name says, and added a regression test: a JSON blob whose .length is under
the cap but whose byte size is over it is now blocked (and still beautifies when
the cap is raised above its byte size).
Verified: 13 tests pass; a 58-code-unit / 158-byte payload is now too-large at
maxBytes=100, where a .length guard would have parsed it.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>1 parent 4324812 commit d95a40f
2 files changed
Lines changed: 16 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
| |||
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
39 | | - | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
90 | 103 | | |
0 commit comments