Repository navigation
fix(artifacts): bound archive decompression by source size - #2144
Conversation
A ZIP/APK/IPA/MSIX member is attacker-controlled and can expand far beyond the archive that carries it. Inventory hashed every member to EOF and extraction wrote every member with no ceiling, so a ~2 MiB archive could drive ~30 s of CPU during inventory and ~150 s plus 2 GiB of disk writes during extraction. A fixed per-entry byte cap is the wrong shape: it rejects legitimately large members inside large packages. The ceiling instead scales with the archive's own size (500x, with a 256 MiB floor), so large packages keep a proportionally large budget while a small archive cannot amplify into unbounded work. The cumulative declared size is charged from the central directory before any entry is decompressed, and zip.js already verifies streamed output against each declaration, so the check is exact and covers both inventory and extraction through the shared reader.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
TL;DR: Head refuses zip bombs that tell the truth about their size, fast and with nothing left on disk. It still takes big real archives, including a 100 MiB member, the case #1081 rejected. One catch: with many members, head unpacks most of them before it refuses. I tested head 8b09b40 against its base fe4b94a with the real CLI. The PR was still open and unchanged when I finished. Results
Both trees gave the same results where both unpacked. The file hashes match.
Worth knowing
Two smaller notes
Not run
Thanks, DeryFerd, for the clear write-up and for the careful fix after #1081. The budget scales with the archive, and the real archives I tried all still work. Test setup
CommandsEach run sat inside The archives came from a short Python script that uses only the standard library. The bombs are zeros. I can share the script if it helps. 🤖 Generated with Claude Code (Sonnet 5.5, verified by Opus 5.5) 🦊 |
There was a problem hiding this comment.
Reviewed repaired exact head cf1f3c2, original contributor head 8b09b40, and current-main integration. Oracle and independent exact-source review found no remaining blocking source defect; follow-up recommendations are source-confirmed.
Root cause: independent entries() traversals reset/incremented a reader-global byte accumulator. Traversal-local ownership removes the shared state and single-use charging wrapper without changing policy, entry identity, encryption admission, cancellation or cleanup ownership. Misleading forged-positive/scale tests were replaced by aggregate boundary, interleaved exact-floor and real-small-stream cases. The dependency's declared-output enforcement is documented and a small under-declared real stream now discriminates that invariant; forged declarations are not claimed as valid large-package verification.
Pinned Node 24.18.0/npm 11.16.0: 22 focused ZIP/integrity/backpressure/owned-cleanup tests passed, fresh check:fast passed, and 916-file discovery passed. No local hdiutil/live provider or large bomb execution; no comparative workflow performance claim. Fresh published-head hosted CI is green, including Static checks, Test (coverage + timing), and CI required. Original-head CI was not reused as repair-head proof. No directly related cleanup remains.
|
Thank you very much @CassieFox23 @baron |
Summary
inspect-artifactandextract-artifactread a ZIP-backed package (.zip,.ipa,.apk,.msix,.appx,.hap,.hsp, app pack) with no ceiling on how far its members may expand. A member is attacker-controlled and can decompress far beyond the archive that carries it, so a small file can drive a long-running or disk-filling operation on the analyst's machine. This change bounds the total decompressed bytes an archive is allowed to produce, scaled to the archive's own size.There is no linked issue. The behavior was reproduced against current
mainwhile reviewing the archive readers; the closest prior work is PR #1081, which proposed a fixed per-entry cap and was closed because it rejected legitimately large members inside large packages.Problem and expected behavior
Smallest trigger: a ~2 MiB ZIP whose single member is 2 GiB of zeros. On the base revision,
inspect-artifacthashes every member to EOF during inventory andextract-artifactwrites every member to disk, so this tiny input produces about 30 seconds of CPU during inventory and, on extraction, roughly 150 seconds and 2 GiB written to the output directory. Nothing in the reader refuses it.The violated invariant is that the work an operation performs must stay proportional to the artifact the analyst chose, not to what a hostile member declares. REA already treats the artifact as untrusted and applies per-document budgets in sibling readers (
KeyedArchiveReader,AppleAssetCatalogAnalysis,InterfaceBuilderAnalysisall use 64 MiB), so archive-backed packages should carry an equivalent bound instead of none.Change and scope
The ceiling scales with the archive's compressed size:
max(256 MiB, 500 × archiveSize). A large package keeps a proportionally large budget, and a small archive cannot amplify into unbounded work. This is deliberately different from PR #1081's fixed per-entry cap, which would reject a single large member even when the surrounding package is legitimate. The ratio sits above real artifact compression (measured roughly 3:1 to 15:1, up to about 350:1 for pure text) and below the DEFLATE ceiling (about 1030:1) that a single-entry bomb relies on.Enforcement is cumulative and happens during enumeration, before any member is decompressed. The reader charges each regular entry's declared uncompressed size against the budget and refuses the archive with the existing typed
limitfailure once the running total exceeds it. Because zip.js already verifies streamed output against each member's declaration, the declared total is an exact upper bound on the decompression work any later read can drive, and the check covers both inventory hashing and extraction through the shared reader.The new
src/domain/artifactDecompressionBudget.tsowns the policy so the number and its rationale live in one place. The reader change is additive: existing metadata binding, CRC, overlap, metadata-budget, encrypted-entry, and cancellation checks are unchanged. Not included: a configurable environment override, an output-size accounting pass during extraction itself, or changes to the non-ZIP readers. The declared-size bound already covers those paths, and adding an override or a second accounting layer would widen the change without closing a reachable gap.Contract and boundary impact
src/domain/artifactDecompressionBudget.ts; enforcement inZipArtifactReader.entries()before any member is read.ArtifactReaderFailurereason: "limit", which the CLI/MCP error projection already maps to a truncated/limit outcome.docs/public/product-catalog.json), package, or installation impact: none. No schema or catalog change.Evidence and regression coverage
src/artifacts/ZipArtifactReader.test.tscovers an archive that declares more than its size can justify (refused withreason: "limit"), an archive within its budget (enumerates normally), a small archive that still receives the floor, and the budget function's floor-and-scale behavior. The fixture forges the central-directory uncompressed size so the test is fast and deterministic without building a multi-gigabyte archive.dist/tree (1364 entries, about 6:1 compression) still inventories normally.ZIP entries declare 2147483648 uncompressed bytes, exceeding this archive's 1043707000-byte decompression budget (268435456-byte minimum, 500x archive size); a small archive cannot expand this far.For evidence-bearing changes:
Validation performed
npx vitest run src/artifacts/ZipArtifactReader.test.ts— 4 passednpx vitest run tests/boundary/filesystem/artifactZipIntegrity.test.ts tests/boundary/providers/native/zipBackpressureCancellation.test.ts tests/boundary/filesystem/ownedArtifactReaderCleanup.test.ts— 16 passednpx tsc --noEmit -p tsconfig.json— passednpx oxlint src/artifacts/ZipArtifactReader.ts src/artifacts/ZipArtifactReader.test.ts src/domain/artifactDecompressionBudget.ts— 0 warnings, 0 errorsnpx oxfmt --check <same files>— correct formatnode scripts/verify-module-boundaries.mjs— clean across 1571 source filesCompatibility, safety, and release
inspect-artifactandextract-artifact.Review checklist
type(scope): outcome.