Commit cb2ac09
authored
fix(upgrade): bound untrusted patch newSize to prevent pre-verification OOM (#1279)
## What
The TRDIFF10 patch header's `newSize` field is attacker-controlled, and
`applyReaderToMemory` uses it to preallocate the output buffer (`new
Uint8Array(newSize)`) **before** the patch content is verified. With no
upper bound, a malicious or corrupt patch declaring a huge `newSize`
(e.g. 8 GiB) forces an out-of-memory allocation — a crash (or worse)
reachable from any patch the CLI downloads or loads from cache.
## Fix
Adds `MAX_OUTPUT_SIZE` (2 GiB) and enforces it in `parsePatchHeader`,
rejecting any `newSize` above the cap **before any allocation**. 2 GiB
is far above any realistic sentry-cli binary (~30 MB), so legitimate
patched outputs never approach the cap.
This mirrors the same fix we made in the Lore gateway's ported copy of
this file (where an adversarial security review flagged the identical
bug).
## Tests
- A header claiming `newSize = MAX_OUTPUT_SIZE + 1` is rejected
(`"exceeds maximum"`).
- A header at `newSize = MAX_OUTPUT_SIZE` (boundary) is still accepted.
- **Mutation-verified:** removing the guard turns the over-cap test RED
while the boundary test stays GREEN (sharp boundary).
## Verification
- 17/17 `test/lib/bspatch.test.ts` pass, `tsc --noEmit` clean, `biome
check` clean.1 parent 7019b60 commit cb2ac09
2 files changed
Lines changed: 44 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
48 | 59 | | |
49 | 60 | | |
50 | 61 | | |
| |||
114 | 125 | | |
115 | 126 | | |
116 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
117 | 136 | | |
118 | 137 | | |
119 | 138 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
152 | 177 | | |
153 | 178 | | |
154 | 179 | | |
| |||
0 commit comments