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

A few kilobytes of ZIP can be gigabytes in your heap

Loading arbitrary bytes as a ZIP and reading an entry fully into memory, with no ratio or size cap, is the textbook decompression bomb waiting to happen.

caller submitsZIP bytesarchive openedentry read fullyinto memorycompression ratioblows up the heapATTACKER INPUTIMPACTresource exhausted
Attack path · caller submits ZIP bytes → archive opened → entry read fully into memory → compression ratio blows up the heap

Loading arbitrary bytes as a ZIP and reading an entry fully into memory, with no ratio or size cap, is the textbook decompression bomb waiting to happen.

What goes wrong

The decode path takes any bytes the caller sends, opens them as a ZIP, and reads the target entry completely into memory as text. Nothing limits the archive size, the compression ratio, the entry size, or how much you extract.

Compression ratios of a thousand to one are ordinary, and crafted archives reach far higher. A payload small enough to slip past a request size limit expands into something that will not fit in memory. You are not defending against a big upload. You are defending against a small one that lies about how big it really is.

The pattern

Vulnerable
function readArchive(bytes) {
  const zip = openZip(bytes)
  const entry = zip.get(ENTRY_NAME)
  return readAllText(entry)     // expands with no cap
}
Hardened
function readArchive(bytes) {
  if (bytes.length > MAX_ARCHIVE) throw new Error('archive too large')
  const zip = openZip(bytes)
  const entry = zip.get(ENTRY_NAME)
  if (entry.declaredSize > MAX_ENTRY) throw new Error('entry too large')
  return readCapped(entry, MAX_ENTRY)  // stop reading at the limit
}
How the pipeline adjudicated itPROVEN
ABCDEFGH

Every gate held. The exploit path was reproduced against a negative control, the impact was calibrated, and the finding was signed report-ready.

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

Why it matters to you

Zip bombs are old, which is exactly why they still work. The code that opens archives is often years old and was written for trusted files. The moment it sits behind an endpoint that accepts uploads, every assumption about well-behaved input is gone. One request can take a worker out of rotation, and a handful can take the service down.

Enforce a limit on the compressed input and on the extracted output, and read entries through a counting stream that aborts when it crosses the line. Trusting the size the archive declares is trusting the attacker.

Check your own code