ShrinkRay All Articles
Engineering Culture

The Tiny Upload That Ate Production: Decompression Bombs and the Limits You Forgot to Set

By ShrinkRay Engineering Culture
The Tiny Upload That Ate Production: Decompression Bombs and the Limits You Forgot to Set

Photo: NOIRLab/NSF/AURA/T. Slovinský, CC BY 4.0, via Wikimedia Commons

A 28 KB gzip file can expand into 4 GB. That sentence should make an on-call engineer sit up. A decompression bomb is not always malware in the scary-movie sense. It can be a perfectly valid archive with a pathological compression ratio, and if your upload pipeline trusts it, one request can eat memory, fill a disk, or pin a worker until the timeout fairy shows up.

ShrinkRay spends a lot of time telling people to compress more. That advice is still right. But compression is not just a byte-count decision. It is a security surface. The moment your service accepts a zip, gzip, tar, or image upload, you are accepting a promise that the expanded output will be reasonable. Attackers do not make that promise.

The upload that looked harmless

Picture a support tool that accepts ticket attachments. Someone uploads a 42 KB zip. The API says yes, drops it into object storage, and queues a background job to inspect the contents. The worker downloads the archive, calls an unzip library, and reads the result into memory so it can scan for text. Ten seconds later the worker is dead, the queue is backing up, and nobody knows why.

The archive did not contain a virus. It contained a lot of zeros. Compression loves repetition, so a small file can describe a huge amount of repeated data. A zip bomb, a gzip bomb, a nested archive, a pixel flood image, an XML entity expansion attack — they are all variations on the same theme: small input, unreasonable output.

How a decompression bomb actually works

The mechanics are boring in the best way. DEFLATE and similar algorithms replace repeated patterns with compact references. A file made of megabytes of null bytes compresses beautifully. The archive header may even declare the uncompressed size, but you should not trust that number. It can be wrong, missing, or deliberately misleading. Nested archives make it worse: a small outer file contains several inner files, each of which contains more, and the expansion happens in stages.

The dangerous part is not the math. It is the assumption. Most upload paths are built around the compressed size because that is what arrives over the wire. The code checks file size against a limit, sees 40 KB, and moves on. Then the decompressor starts producing gigabytes.

The parser is part of the attack surface

Even a well-behaved archive can hurt you if the parser is buggy. Decompression libraries are complex C or C++ code in many stacks, and complex code has sharp edges. A malformed header, an integer overflow, a bad length field — these are the kinds of bugs that turn a boring upload endpoint into a remote code execution headline.

That does not mean you need to panic about every zip file. It means you should treat unpacking as untrusted work. Keep your libraries patched. Prefer well-maintained parsers. Fuzz the upload path if you can. And do not run the unpacker in the same process that serves user traffic if you can avoid it.

Guardrails that belong in every upload path

Cap the compressed file

This is table stakes. Set a maximum upload size at the edge, and make it smaller than whatever your worker can safely handle. A 10 MB compressed upload limit is reasonable for many apps. A 500 MB limit with no expansion control is a gift to whoever finds it.

Cap the expanded bytes, not just the file

Your decompressor should count output as it streams. Stop at a hard byte budget. If a job is only supposed to inspect a 50 MB document, abort at 50 MB. Do not wait for the library to finish and then check the size. By then the damage is done.

Enforce a ratio ceiling

Compression ratios are useful signals. A 10:1 ratio is normal for text. A 200:1 ratio is suspicious. A 1000:1 ratio is a smoke alarm. Set a policy: reject above a hard ceiling, alert above a softer one. You will occasionally block a legitimate highly repetitive file, and that is a fine trade for not taking down production.

Stream, do not slurp

Reading an entire archive into memory is the classic mistake. Stream the decompressed output into a temp file with a disk quota, or process it in chunks. If you must hold data in memory, put a hard limit on the buffer and fail fast when it is crossed.

Isolate the unpacker

Run decompression in a separate worker with a memory limit, a CPU limit, a wall-clock timeout, and no unnecessary network access. Use an unprivileged user. Mount a small temp directory with a quota. If the worker gets killed by the kernel, that is the system working as intended, not a disaster.

Monitor the weird stuff

Track rejected uploads, ratio outliers, unpack durations, peak memory, and disk usage per job. Alert when a single job expands past a threshold or when the rejection rate jumps. Attackers test quietly before they push harder. Your metrics should notice the probing.

A practical policy sketch

A reasonable starting point for a general-purpose upload service: 5 MB compressed limit, 100:1 ratio warning, 200:1 ratio rejection, 200 MB expanded byte budget, 10-second unpack timeout, one level of nested archives, a 1 GB temp directory quota, and a worker that runs in its own container. Tune the numbers to your product, but write them down. Undocumented limits are just surprises waiting to happen.

Also consider running decompression asynchronously. The request that accepts the upload should validate the compressed file and enqueue the work. It should not sit there unpacking a mystery archive while the client waits and your worker memory climbs.

The efficiency lesson

Compression saves bandwidth, storage, and money. It also concentrates power in a very small input. That is exactly why it is worth using, and exactly why it needs limits. ShrinkRay's whole pitch is to waste nothing — bytes, CPU, or trust. A decompression bomb wastes all three.

So keep accepting compressed uploads. Just stop treating the compressed size as the whole story. Measure the expansion, cap it, isolate it, and watch it. The tiny upload is fine. The tiny upload that becomes 40 GB is the one you need to plan for.