Files
rea/src/domain/artifactDecompressionBudget.ts
T
c59fbf5e9c fix(artifacts): bound archive decompression by source size (#2144)
* fix(artifacts): bound archive decompression by source size

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.

* fix(artifacts): own decompression accounting per traversal

* test(artifacts): verify declared ZIP output enforcement

---------

Co-authored-by: baron <richstyles@gmail.com>
2026-10-13 06:15:34 +09:00

36 lines
1.6 KiB
TypeScript

/**
* Aggregate decompression ceiling for one archive-backed artifact operation.
*
* A ZIP/APK/IPA/MSIX member is attacker-controlled and can expand far beyond the
* archive that carries it, so a small input can drive unbounded decompression
* work during inventory hashing and extraction. A fixed per-entry byte cap is
* the wrong shape: it rejects legitimately large members inside large packages
* and cannot distinguish a bomb from a large file.
*
* The ceiling instead scales with the archive's own compressed size, so a large
* package keeps a proportionally large budget while a small archive cannot
* amplify into unbounded CPU time or disk writes. The floor keeps ordinary
* small archives usable, and the ratio sits above real-world artifact
* compression (measured ~3:1-15:1, up to ~350:1 for pure text) but below the
* DEFLATE ceiling (~1030:1) that a single-entry bomb relies on.
*/
/** Allow a whole archive to expand by this factor before decompression is refused. */
export const ARTIFACT_DECOMPRESSION_RATIO = 500;
/**
* Smallest aggregate budget granted to any archive, so small packages stay usable.
* This floor keeps ordinary archives extractable and stays well above the largest
* single members real packages carry; it is not a per-entry cap.
*/
export const MINIMUM_ARTIFACT_DECOMPRESSION_BYTES = 256 * 1024 * 1024;
/** Largest total decompressed bytes allowed for an archive of this compressed size. */
export const artifactDecompressionBudgetBytes = (
compressedBytes: number,
): number =>
Math.max(
MINIMUM_ARTIFACT_DECOMPRESSION_BYTES,
Math.floor(compressedBytes) * ARTIFACT_DECOMPRESSION_RATIO,
);