Blog / A04:2021 Insecure Design
A04:2021 · Insecure Design

The archive says the entry is small. Believe it at your peril.

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.

caller sends ZIPbytesnamed entrylocatedentry expandedinto a byte arraydeclared sizeunderstates…ATTACKER INPUTIMPACTresource exhausted
Attack path · caller sends ZIP bytes → named entry located → entry expanded into a byte array → declared size understates the real one

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

Vulnerable
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)
}
Hardened
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
}
How the pipeline adjudicated itHELD
ABCDEFGH

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.

Ed25519 · per-tenant signing key · gate ledger A–H · verify offline against the published payload hash

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