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>
This commit is contained in:
@@ -12,6 +12,11 @@ import {
|
||||
type ArtifactReader,
|
||||
} from "./ArtifactReader.js";
|
||||
import type { ZipPackageFormat } from "../domain/zipPackageFormat.js";
|
||||
import {
|
||||
ARTIFACT_DECOMPRESSION_RATIO,
|
||||
artifactDecompressionBudgetBytes,
|
||||
MINIMUM_ARTIFACT_DECOMPRESSION_BYTES,
|
||||
} from "../domain/artifactDecompressionBudget.js";
|
||||
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
|
||||
import {
|
||||
NonRegularFileReadError,
|
||||
@@ -132,7 +137,6 @@ export class ZipArtifactReader implements ArtifactReader {
|
||||
string,
|
||||
{ readonly source: Entry; readonly metadata: ArtifactEntry }
|
||||
>();
|
||||
|
||||
/** Optionally bound each metadata read before allocating its backing buffer. */
|
||||
constructor(
|
||||
path: string,
|
||||
@@ -153,6 +157,8 @@ export class ZipArtifactReader implements ArtifactReader {
|
||||
|
||||
async *entries(signal?: AbortSignal): AsyncIterable<ArtifactEntry> {
|
||||
let index = 0;
|
||||
// A traversal owns its accounting, including overlapping or repeated passes.
|
||||
let declaredFileBytes = 0;
|
||||
for await (const entry of this.#reader.getEntriesGenerator({
|
||||
decodeText: decodeZipEntryText,
|
||||
})) {
|
||||
@@ -160,6 +166,22 @@ export class ZipArtifactReader implements ArtifactReader {
|
||||
// ZIP member identity is its directory occurrence, not its filename.
|
||||
// Logical path uniqueness belongs to the artifact inventory boundary.
|
||||
const metadata = projectZipEntry(entry, index++);
|
||||
// zip.js enforces each member's declared output size, so this sum bounds streamed work.
|
||||
if (
|
||||
metadata.kind === "file" &&
|
||||
!metadata.encrypted &&
|
||||
metadata.declaredSize !== null &&
|
||||
Number.isSafeInteger(metadata.declaredSize) &&
|
||||
metadata.declaredSize >= 0
|
||||
) {
|
||||
declaredFileBytes += metadata.declaredSize;
|
||||
const budget = artifactDecompressionBudgetBytes(this.#source.size);
|
||||
if (declaredFileBytes > budget)
|
||||
throw new ArtifactReaderFailure(
|
||||
"limit",
|
||||
`ZIP entries declare ${String(declaredFileBytes)} uncompressed bytes, exceeding this archive's ${String(budget)}-byte decompression budget (${String(MINIMUM_ARTIFACT_DECOMPRESSION_BYTES)}-byte minimum, ${String(ARTIFACT_DECOMPRESSION_RATIO)}x archive size); a small archive cannot expand this far`,
|
||||
);
|
||||
}
|
||||
const stored = this.#entries.get(metadata.adapterKey);
|
||||
if (stored !== undefined && !sameArtifactEntry(metadata, stored.metadata))
|
||||
throw new ArtifactReaderFailure(
|
||||
|
||||
Reference in New Issue
Block a user