Repository navigation
fix(fetchers): decode compressed bodies as they are read, never inflate a bomb (repo#2717) - #699
Conversation
…te a bomb (repo#2717) Bun's fetch inflates a Content-Encoding body in full as soon as it arrives, read or not, so no byte cap in the crawl bounded a compression bomb: a 1 MB gzip page that inflates to 1 GiB cost 2.5-2.8 GB, a robots.txt or a discarded redirect body the same. fetchUndecoded fetches with decompress: false (and the Accept-Encoding the runtime would have sent); readBodyCapped and decodedBody decode lazily, no further than the read's cap. Every crawl and audit fetch of a site URL goes through it: page fetchers on every redirect hop, root probes, sitemaps, seed resolution, scripts, assets, external links, soft-404 and cloaking re-checks, the CLI reachability probe. Decode failures stay decode failures; Accept-Encoding now survives a cross-origin hop. Signed-off-by: Nik Cubrilovic <[email protected]>
|
Review of #699 (undecoded fetches and pull-based decoding). I read the diff, mainly The design holds up.
Test coverage is good. There are new tests for the utils helper, the crawler bomb cases, the engine probes, the fetchers and the CLI, plus a benchmark harness. |
#292 fallback is plain fetch (repo#2717) Signed-off-by: Nik Cubrilovic <[email protected]>
|
Triage of the review, at 83afcc9:
|
|
Review of #699 (undecoded fetch + lazy decoding for #2717). I read the diff and the core helper in The approach is sound. Decoding is pull-based and the redirect hops go through
No security or performance problems found beyond the above. This is a strong mitigation for a real memory-exhaustion vector. Nothing here blocks the merge, but the |
…re ignored (repo#2717) Bun 1.4's DecompressionStream rejects bytes after a complete stream (gzip, br, deflate and zstd with appended junk, gzip with zero padding) and drops the output it still held, where the runtime's own decoding read those pages. Decoding now runs through node:zlib, whose decoders ignore trailing bytes; gzip is read past its header as raw deflate to the end of its first member, as browsers read it. Still lazy and bounded on Bun 1.3.14 and 1.4.0. Signed-off-by: Nik Cubrilovic <[email protected]>
|
Triage of the second review. The trailing-bytes point was a real regression, fixed in a8fb476:
|
Review of #699: undecoded fetches for Content-Encoding bombsI read the diff and the existing
Tests: coverage is good. There are tests for undecoded fetches in the CLI, the audit-engine and the crawler, plus a memory-bound check on a 1 GiB bomb. The RSS-delta assertion (< 256 MiB, sampled every 1 ms) is likely to be flaky on loaded CI runners. Asserting that Security: this is a net improvement, and I found no new issues. The remaining gap is the sitemap fetch above. Two of the findings are medium and none is blocking. |
|
Triage of the third review, at a8fb476. No code change in this round.
|
Summary
Fixes squirrelscan/repo#2717. Bun's
fetchinflates aContent-Encodingbody in full as soon as it arrives. It does this whether the body is read under a byte cap, cancelled unread, or thrown away with a redirect, so no cap in the crawl stopped a compression bomb. Measured on the parent commit: a 1 MB gzip page that inflates to 1 GiB cost 2.5 to 2.8 GB of RSS on Bun 1.3.14, and a whole audit of a bomb site cost 2.3 to 2.8 GB on the released v0.0.108 (Bun 1.4.0).packages/utils/src/response-body.ts, one shared helper:fetchUndecodedfetches withdecompress: falseand sends theAccept-EncodingBun would have sent (gzip, deflate, br, zstd, or the caller's own value). In Bun it records each response that still carries a coding. Other runtimes get a plainfetch.decodedBody/readBodyCappeddecode through a pull bridge over node:zlib. A chunk goes to the decoder only once its output has been read, and node:zlib pauses while its output is unread, so nothing is inflated past what the reader pulls. Cancelling stops the raw body too.DecompressionStream: on Bun 1.4.0 it rejects bytes after a complete stream (gzip with appended junk or zero padding, br, deflate and zstd with junk) and drops the output it still held. The runtime's own decoding read those pages fine. node:zlib ignores trailing bytes. gzip is read past its header (parsed here, RFC 1952) as raw deflate, up to the end of its first member, the way browsers read it.deflateis sniffed as zlib or raw deflate. An empty encoded body reads as"", as it did before.AbortError. A body that does not decode throwsContentDecodingError, andisDecompressionError(now shared) recognises it, soCrawlError.decodeand the fetchers' docs(changelog): add the v0.0.92 numbers table #292 raw refetch behave as before.safeRedirectFetch(url, init, send)sends every manual hop throughsend.safeFetchWithDeadlinepassesfetchUndecoded, which covers robots, rsl, llms, markdown, well-known, agent-access and sitemaps..text()reads up to the cap + 1, matching the engine's.accept-encodingis now kept on cross-origin redirect hops. Without it, a sitemap hop would ask for the default codings, including zstd, which the sitemap reader does not decode (found in the codex review).The "too-large outcome" (AC 1). A decoded body over its cap is handled the same way as an uncompressed one over the cap. For pages that means truncation at the 10 MiB document cap, and the audit reports it: in the end-to-end run,
crawl/html-sizefails/bombat 10.0 MB andcrawl/robots-txtwarns that robots.txt is very large (512 KB). Scripts still reportscript too large. I did not add a separate "too large" field for pages, because that would change what happens to oversized uncompressed pages too.On "about 2x the cap". A bomb now costs the same as an uncompressed body over the same cap; the control row in the record shows this. Through
readBodyCappedthat is 3 to 5 times the 10 MiB page cap. The overhead comes from the existing reader, not from decoding: 10 MiB of bytes plus the string decoded from them is 21 MB on its own. Brotli has one more fixed cost. Each open stream keeps a window of up to 16 MiB, so 20 concurrent large-window bombs measured 357 to 367 MB, where inflating them would take 20 GiB.Behaviour changes for ordinary responses. Bun sent no
Accept-Encodingat all with its decoding off, so the header is now set explicitly to Bun's own value, and origins see the same request.x-gzipand an upper-caseGZIPare now decoded; Bun used to pass them through as raw bytes. Stacked and unknown codings still pass through untouched. Bytes after a complete compressed body are still ignored. A multi-member gzip body is read to the end of its first member, as Chrome and Bun 1.3.14 read it; Bun 1.4.0's own decoding read every member. The text of a decode-failure message is now the decoder's reason (for examplegzip body does not decode (no gzip header)), and the runtime no longer prints its ownDecompression error:line.Docs: new "Compressed responses" section in
docs/configuration/crawler.mdx, the decode-failure example is updated, and there is a CHANGELOG entry under Unreleased. Benchmark record:benchmarks/2026-10-content-encoding-bombs.md, with a reproducible harness atbenchmarks/harness/encoding-bomb.ts(server in a separate process,--rootto measure another checkout).Skills: no change needed in squirrelscan/skills. CLI usage and output are unchanged.
Verification
Peak RSS growth. The bomb server runs in its own process and the figures are min to max over the repeats. Full table:
benchmarks/2026-10-content-encoding-bombs.md.End to end,
squirrel audit http://127.0.0.1:<port>/ --offline --http --max-pages 10(fakeHOME). The site's page, redirect, robots.txt and llms.txt are 1 GiB bombs. Max RSS is from/usr/bin/time -l:Both reports are identical: 4 pages, score 46, same findings.
Tests. The spy tests in crawler, fetchers and audit-engine were run against the parent commit, where they fail only on the
decompressassertions; their decoded-content assertions pass on both. The RSS bound was checked against the runtime's own decoding, which used 1,388 MB against the 256 MiB bound:packages/utils/tests/fetch-undecoded.test.ts: decoding of gzip, x-gzip, GZIP, deflate (zlib and raw), br and zstd; bytes after a complete stream ignored (gzip with junk, zero padding, a second member and a header with FEXTRA, FNAME and FCOMMENT; br, deflate and zstd with junk); identity, unknown and stacked codings pass through; an empty body; liar and truncated bodies raiseContentDecodingError; a cancel in flight; an abort staysAbortError; 1 GiB gzip and br bombs read to a 1 MiB cap stay under 256 MiB of RSS; the native-redirect final hop arrives undecoded; Accept-Encoding survives a cross-origin hop;safeRedirectFetchsends every hop throughsend.packages/crawler/tests/content-encoding-bombs.test.ts: a page bomb is cut at 10 MiB under 256 MiB of RSS; a 302 hop bomb is never inflated; robots and llms bombs are read to their caps. A full crawl with a global fetch spy (seed redirect, robots, llms, markdown, the well-known paths, sitemaps, pages and a redirect hop, with gzip, br, deflate, raw deflate and zstd pages) checks that every request went out withdecompress: falseand that every page was stored decoded.packages/fetchers/src/fetch-fetcher.test.ts: three manual hops, each withdecompress: false, and the page is decoded.packages/audit-engine/tests/undecoded-probe-fetches.test.ts: script, resource (decoded size, plus a 1 GiB bomb counted without being held), external link (decoded 403 WAF page), cloaking and soft-404, each undecoded with decoded results.apps/cli/tests/crawler/undecoded-fetches.test.ts: CLI script fetch, a 1 GiB script refused asscript too largeunder 256 MiB, external links, andrequestOnceScoped.Local gates:
tsgo --noEmitin utils, crawler, fetchers, audit-engine and apps/cliformat:checkandlintdetect-secrets-hookon the changed files[email protected] check --strict: 426 pages, all checks passedapps/cli/tests/tools/request-deadline.test.ts> "checkReachability returns instead of hanging on a stalled body", which also times out onmainon this Mac: 127.0.0.2 is not routable on macOS. It is not related to this change.Codex review: one full pass and two delta passes. Its findings are fixed: the resource-checker fallback was still buffering the body, and the sitemap Accept-Encoding was lost on cross-origin hops. It found no blockers in the node:zlib bridge (lost wakeups, the write callbacks, backpressure, the gzip header parser). The trailing-bytes regression came from a hint in the PR review; it is fixed in a8fb476.
After merge: a private gitlink bump in squirrelscan/repo.
Checklist
Signed-off-byline (git commit -s).