Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท crypto

Decompression bombs and zlib output limits

Why decompressing untrusted data is dangerous and how to cap it in Node.js: zlib maxOutputLength, byte-counting Transform streams, input limits and ratio checks.

A decompression bomb is a small compressed file that expands into an enormous amount of data. Compression works by describing repetition, and a stream of identical bytes is the extreme case: about one megabyte of gzip can unpack to a gigabyte, and carefully built nested archives reach far higher ratios. If your server decompresses data it receives (uploads, API request bodies, webhooks, archives), an attacker can send a few kilobytes and make your process allocate gigabytes until it is killed.

The defense is simple: never decompress without an upper bound on the output. Node gives you maxOutputLength for the buffer helpers, and a small counting stream for the streaming API. Limiting only the compressed input size is not enough, because the ratio is what the attacker controls.

NODE
import { gunzipSync } from 'node:zlib';

const body = gunzipSync(untrusted, { maxOutputLength: 1024 * 1024 }); // at most 1 MiB

Try it

Node.js The ratio and maxOutputLength

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Limiting a stream

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Defense in layers

  1. Cap the compressed input before you read it: a body-size limit on the HTTP server, a maximum upload size. This protects memory and CPU spent on reading.
  2. Cap the decompressed output with maxOutputLength (buffer helpers) or a counting Transform (streams). Choose the largest size a legitimate message can have, not "something big".
  3. Watch the ratio if you stream: abort when output bytes divided by input bytes exceeds a sane figure (JSON and text rarely beat 20:1 in normal use).
  4. Bound the time. Decompression of hostile input is CPU work too. Process it away from latency-critical paths, for example in a worker thread, and use AbortSignal.timeout() in the pipeline.
  5. Mind nesting. Archives inside archives multiply the problem. Cap the depth and the total across all entries, not per entry.

Usage notes

  • maxOutputLength exists on every buffer helper (gunzip, inflate, brotliDecompress, zstdDecompress and the sync forms). The error is ERR_BUFFER_TOO_LARGE, and its message names the limit.
  • The default is the maximum buffer size (about 2 GiB or more, depending on the platform), so without your own limit a single call can use gigabytes before failing.
  • Streams (createGunzip() and friends) do not enforce the option, so use a counting Transform like the one above.
  • A server that accepts Content-Encoding: gzip on requests must decompress with a limit. Many frameworks have a body-parser option for the limit measured after decompression; check that yours does.
  • The Web DecompressionStream has no output limit either; count bytes in the reader loop and call reader.cancel() when you hit your cap.
  • A limit that is too tight rejects valid users. Measure real payload sizes and add generous headroom.