Lorixen.
EN

Stop oversized images and zip bombs crashing your server

Short answer: A small upload can expand into something enormous: a tiny image file that declares gigantic dimensions, or a compressed archive that unpacks to many gigabytes. Processing it can exhaust your server's memory or disk and take the app down for everyone. Check declared image dimensions before decoding, cap total uncompressed size and file count when extracting archives, process uploads in a separate worker with memory and time limits, and reject anything over your limits with a clear message.

What a decompression bomb is

Compression is the reason uploads are small. An image format stores the picture compactly and a decoder expands it into raw pixels in memory; an archive stores files compactly and extraction writes them back out in full. Both can be abused. A file can be crafted, or simply exported carelessly, so that the expanded result is wildly larger than the upload:

  • Image bombs: an image with enormous pixel dimensions but almost no real detail compresses to a small file. Decoding it means allocating memory for every pixel.
  • Archive bombs: a ZIP or similar archive that contains huge files of repeated data, or nested archives, expanding far beyond its download size.
  • XML bombs: XML that defines entities referring to other entities, expanding exponentially when parsed.

OWASP's File Upload Cheat Sheet lists ZIP bombs and XML bombs among the ways uploads are abused, alongside simply sending huge files to fill storage, and says that when you accept compressed files, size limits should be applied to the decompressed result using safe methods of calculating it.

Where small business apps are exposed

  • Product image uploads that are resized automatically.
  • Profile photo or document uploads in customer portals.
  • Bulk import features that accept a ZIP of product images or documents.
  • Integrations that accept XML feeds from suppliers.
  • Any "upload your logo" feature on a marketing site.

Often nobody is attacking you. A customer uploads a very large panorama or a badly exported scan, and the resizing step quietly uses all the memory on a small server.

Images: check dimensions before decoding

Most image libraries can read an image's declared width and height from its header without decoding all of its pixels. Read them first and reject images whose pixel count is beyond what you need.

Pillow, the common Python imaging library, has a built-in guard. Its documentation explains that, to protect against decompression bombs, it issues a DecompressionBombWarning when an image's pixel count is over MAX_IMAGE_PIXELS, and raises a DecompressionBombError when the count is more than twice that limit. You can lower the threshold to match your needs. The documentation also shows how to disable it, which you shouldn't do for user uploads.

Other libraries have similar settings, such as pixel limits on input. Check yours and set a limit that comfortably covers genuine photos from modern phones and cameras.

Archives: count while you extract

Python's zipfile documentation has a section on decompression pitfalls that names decompression bombs among the risks, noting they can exhaust disk space. Good practice for any language:

  • Don't trust the sizes listed inside the archive. Track bytes actually written as you extract and stop when a total limit is reached.
  • Limit the number of files and the nesting depth. Don't automatically extract archives found inside archives.
  • Extract into a fresh temporary folder with its own quota, never into your app's directories.
  • Validate file names inside the archive so entries can't write outside the target folder.
  • Delete everything if any limit is hit.

XML: disable entity expansion

Use parsers configured to reject or limit entity expansion and external entities. Many languages offer hardened parsers or safe defaults; check the documentation for the one you use and avoid enabling features you don't need.

Isolate the processing

Even with checks, treat upload processing as risky work:

  • Run it in a background worker, not in the web request, so a heavy file can't freeze the site.
  • Set memory and time limits on that worker, for example in its container or service definition, so a runaway job is killed rather than taking the server down.
  • Use a separate machine or service for heavy processing if uploads are central to your business.
  • Store originals outside the web root and serve processed versions.

Test your defences

Keep a small set of deliberately awkward test files in your test suite: an image with huge declared dimensions, an archive with many tiny files, an archive whose contents total far more than your limit, and an XML file with nested entities. Run them against staging after any change to the upload code or image library version. Each should be rejected quickly with your friendly message, while memory use on the worker stays flat. If a test file ever takes the worker down, you've found the gap before a customer did.

Tell users what's allowed

State limits on the upload form: accepted types, maximum file size and maximum image dimensions. When a file is rejected, say why in plain language, such as "This image is larger than 10,000 pixels on its longest side; please export a smaller version." The limits in your own messages are your choice; make them generous enough for real customers.

A worked example

Take a hypothetical Sheffield print-on-demand shop whose customers uploaded artwork that was automatically resized for previews. Twice in a week the site went down after customers uploaded huge scans, each small as a file but enormous in pixels. The developer added a dimension check before decoding, kept the image library's pixel limit on, moved resizing into a background worker with a memory cap, and added clear limits to the upload page. Oversized files are now rejected with instructions, and preview generation no longer affects the shop.

FAQ

Isn't a file size limit enough?

No. The danger is the expanded size, which can be huge even when the upload is small.

Will these limits block real customer photos?

Not if you set them sensibly. Test with large photos from current phones and your customers' typical files.

Do image hosting services handle this for me?

Many apply limits, but check their documentation and still validate before you pass files on.

Should I scan uploads for malware too?

For documents and archives, often yes. Malware scanning and bomb protection solve different problems.

If your site processes customer uploads and you're unsure how it handles oversized files, tell us what it accepts.

Sources

WhatsAppBook a call