Extracting a ZIP entry into a byte array based on what the archive claims lets a crafted entry declare little and expand into your entire heap.
What goes wrong
The path pulls a named entry out of the caller's ZIP and expands it into a byte array. The decision about how much to read leans on the size the archive reports, and the archive is attacker-controlled.
A ZIP entry can claim one size in its header and expand into something far larger, or simply declare a size big enough to exhaust the heap on its own. Because the extraction believes the metadata, it either over-allocates immediately or keeps inflating past anything reasonable. The one field you trusted is the one the attacker set.
The pattern
function readArchive(bytes) {
const zip = openZip(bytes)
const entry = zip.get(TARGET_ENTRY)
const buf = allocate(entry.declaredSize) // trusts the header
return inflateInto(buf, entry)
}function readArchive(bytes) {
if (bytes.length > MAX_ARCHIVE) throw new Error('archive too large')
const zip = openZip(bytes)
const entry = zip.get(TARGET_ENTRY)
return inflateCapped(entry, MAX_ENTRY) // ignore the header, cap the read
}Grounding and reachability were confirmed. The exploit-path gate would not conclude without the calling code, so the finding was held and an evidence request was raised instead of being reported as fact.
Why it matters to you
Archive metadata exists to help you allocate efficiently, which makes it tempting to trust. Under attack it becomes a way to make you allocate on command. Treat the declared size as a hint at most and enforce your own ceiling on the bytes you actually read. Then a lie in the header costs the attacker a rejected request instead of your memory.
Anywhere you size a buffer from a number inside the input, ask who wrote that number. If the answer is the caller, it needs a limit you own.
Check your own code
- Never allocate based on a size the input itself declares.
- Enforce a maximum extracted size while you read, not before.
- Cap the compressed archive at intake.
- Add a test with an entry whose declared size is a lie.