Skip to content

fix(artifacts): bound archive decompression by source size - #2144

Merged
baron merged 3 commits into
morluto:mainfrom
DeryFerd:fix/artifact-decompression-budget
Oct 12, 2026
Merged

baron merged 3 commits into
morluto:mainfrom
DeryFerd:fix/artifact-decompression-budget

Conversation

@DeryFerd

Copy link
Copy Markdown
Contributor

Summary

inspect-artifact and extract-artifact read 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 main while 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-artifact hashes every member to EOF during inventory and extract-artifact writes 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, InterfaceBuilderAnalysis all 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 limit failure 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.ts owns 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

  • Semantic owner and earliest changed stage: decompression policy in src/domain/artifactDecompressionBudget.ts; enforcement in ZipArtifactReader.entries() before any member is read.
  • CLI and MCP/tool-catalog contract: no schema change. An over-budget archive fails with the existing ArtifactReaderFailure reason: "limit", which the CLI/MCP error projection already maps to a truncated/limit outcome.
  • Provider, bridge, target-format, or platform compatibility: ZIP-backed package families are affected uniformly. Non-ZIP readers (ASAR, directory, Mach-O, DMG) are unchanged.
  • Evidence, artifact, provenance, or reconstruction contract: the archive is refused before extraction, so no partial output tree is published; inventory and extraction keep their current success shapes for accepted archives.
  • Process execution, authorization, cleanup, or containment impact: none. No new process, no change to output ownership, and the failure path releases the reader and any created output tree as before.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: none. No schema or catalog change.

Evidence and regression coverage

  • Tests added or updated: src/artifacts/ZipArtifactReader.test.ts covers an archive that declares more than its size can justify (refused with reason: "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.
  • Base reproduction or other evidence: on the base revision, a 2 MiB ZIP with a 2 GiB member inventories in about 30 seconds (RSS stayed near 150 MB) and extracts in about 150 seconds while writing 2.0 GiB; a 512 MiB member inventories in about 11 seconds. After the change, both are refused in under 100 ms with no output written. A legitimate 4.9 MiB archive of the real dist/ tree (1364 entries, about 6:1 compression) still inventories normally.
  • User-visible CLI/MCP output (if applicable): an over-budget archive returns the typed limit failure, for example 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.
  • Remaining proof gaps: the byte counts above come from a direct harness against the built reader on Windows with Node 24; Linux/macOS and the full CLI/MCP entry points were not exercised for this exact fixture. Extraction-side accounting was not added, so a member that lies about its size and passes the declaration check relies on zip.js's streamed verification, which is already enforced.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npx vitest run src/artifacts/ZipArtifactReader.test.ts — 4 passed
  • npx vitest run tests/boundary/filesystem/artifactZipIntegrity.test.ts tests/boundary/providers/native/zipBackpressureCancellation.test.ts tests/boundary/filesystem/ownedArtifactReaderCleanup.test.ts — 16 passed
  • npx tsc --noEmit -p tsconfig.json — passed
  • npx oxlint src/artifacts/ZipArtifactReader.ts src/artifacts/ZipArtifactReader.test.ts src/domain/artifactDecompressionBudget.ts — 0 warnings, 0 errors
  • npx oxfmt --check <same files> — correct format
  • node scripts/verify-module-boundaries.mjs — clean across 1571 source files
  • Empirical probe against the built reader — 2 GiB and 512 MiB bombs refused under 100 ms; a legitimate 4.9 MiB archive still inventories

Compatibility, safety, and release

  • Breaking changes or migration steps: none for a supported contract. An archive that previously would have been processed (a genuine bomb) now fails with a typed limit error; archives within the budget are unaffected.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable. This path does not start a provider; it is the archive reader used by inspect-artifact and extract-artifact.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: this is the security change. It removes a resource-exhaustion path where a small attacker-controlled archive could consume unbounded CPU and disk. The bound is enforced before decompression, uses the existing typed limit failure, and preserves the reader's current containment and cleanup behavior.

Review checklist

  • This PR addresses a concrete problem or an agreed enhancement.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-12T11:13:12.561736Z 8b09b40 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@CassieFox23

Copy link
Copy Markdown
Contributor

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

Archive Base Head
One 257 MiB member, 256 KiB file unpacked, 257 MiB written refused, 0 bytes written
Ten 26 MiB members, 260 KiB file unpacked, 260 MiB written refused, 0 bytes written
One 600 MiB member, 597 KiB file unpacked, 600 MiB written refused, 0 bytes written
One 100 MiB member (over the #1081 cap) unpacked unpacked
Three 100 MiB members, over the 256 MiB floor unpacked unpacked
400 MiB log, 11 to 1 unpacked unpacked
300 MiB log, 391 to 1 unpacked unpacked
Ghidra API docs, 384 MiB in a 51 MiB file unpacked unpacked
Ghidra Base.jar, 6,813 entries unpacked unpacked
300 MiB sparse image, 680 to 1 unpacked refused
Forged sizes (two shapes) refused by zip.js same refusal

Both trees gave the same results where both unpacked. The file hashes match.

  • A refusal on head took 0.9 seconds, the same as base takes to refuse a corrupt archive. The same bomb took 2 to 6 seconds to unpack on base.
  • After every refusal the output folder was empty. No new rea-extracted folder appeared in /tmp.
  • The refusal is the typed limit failure. For the 257 MiB bomb it says the entries declare 269,484,032 bytes against a budget of 268,435,456.
  • The PR test passes on Node 22.23.1, 24.18.0 and 26.11.1, with 4 of 4 each time. The three related suites in the PR body pass too, 16 of 16 on Node 24.18.0.

Worth knowing

  1. The ten-member bomb is only refused at the tenth member. Head hashes each member as it lists it, so it inflated nine members (234 MiB) in memory first. It wrote nothing to disk. The work stays under the limit. The PR body says the check runs "before any member is decompressed". That is true for one member, not for many.
  2. The limit is a ratio of 500 to 1 above a 256 MiB floor. A 300 MiB sparse image at 680 to 1 is refused, while a log at 391 to 1 passes. That is a fair trade, but a legitimate file can land on the wrong side.
Two smaller notes
  • By the formula, a 20 MiB archive may still declare 10,000 MiB. I did not run that size.
  • The refusal says "Narrow the requested path". That cannot help with one archive.

Not run

  • The 2 GiB bomb from the PR body. My cap was 600 MiB per member.
  • A zip inside a zip.
  • The MCP entry points, macOS, Windows, and other extensions such as .apk or .ipa. I used .zip and .jar.
  • An archive padded with junk bytes to raise its size.
  • The full test suite, lint and typecheck.

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
  • Box: Nobara 44 (Fedora 44 base), Linux 7.2.9
  • CPU: AMD Ryzen 9 5900X, 12 cores / 24 threads
  • RAM: 32 GB DDR4-3600
  • Node: 22.23.1, 24.18.0, 26.11.1 (the CLI runs used 24.18.0; the PR test ran on all three)
  • Head / base: 8b09b40 / fe4b94a (main moved on, but not in the PR's file or the two files that call it)
  • Other: zip.js 2.15.0; Ghidra 12.1.4 (Apache-2.0), read in place for the two real archives
  • The box was shared with another test agent. Times are rough. Every CLI run had a 4 GB memory cap and a 120 second limit.
Commands
node scripts/rea.mjs inspect-artifact <archive> --format json
TMPDIR=/tmp/pr2144/out-<case> node scripts/rea.mjs extract-artifact <archive> --format json

Each run sat inside systemd-run --user --scope -p MemoryMax=4G -- /usr/bin/time -v timeout 120 env .... I ran each of 14 archives on head and on base, with both commands. That is 56 runs. Exit code 0 for every unpacked archive, 1 for every refusal, and none timed out.

node count-inflated.mjs <head or base> <archive>      exit 0, 12 runs
vitest run src/artifacts/ZipArtifactReader.test.ts    exit 0 on Node 22, 24 and 26
vitest run tests/boundary/filesystem/artifactZipIntegrity.test.ts tests/boundary/providers/native/zipBackpressureCancellation.test.ts tests/boundary/filesystem/ownedArtifactReaderCleanup.test.ts    exit 0 on Node 24

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) 🦊

@baron baron left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@baron
baron merged commit c59fbf5 into morluto:main Oct 12, 2026
34 checks passed
@DeryFerd

Copy link
Copy Markdown
Contributor Author

Thank you very much @CassieFox23 @baron

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: evidence Evidence, artifacts, and reconstruction bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants