From 2412f890d726ff3cde0c8cfaacfdeed075a77422 Mon Sep 17 00:00:00 2001 From: SahinurDEV <68515168+SahinurDEV@users.noreply.github.com> Date: Mon, 2 Nov 2026 10:15:49 +0600 Subject: [PATCH] fix(artifacts): report oversized Zstandard ZIP windows as a resource limit --- src/artifacts/ZipArtifactReader.test.ts | 82 +++++++++++++++++++------ src/artifacts/ZipArtifactReader.ts | 20 +++++- 2 files changed, 81 insertions(+), 21 deletions(-) diff --git a/src/artifacts/ZipArtifactReader.test.ts b/src/artifacts/ZipArtifactReader.test.ts index a117ea9ba3a..8399dc3d654 100644 --- a/src/artifacts/ZipArtifactReader.test.ts +++ b/src/artifacts/ZipArtifactReader.test.ts @@ -27,6 +27,7 @@ import { MINIMUM_ARTIFACT_EXPANDED_BYTES } from "../domain/artifactExpansionBudg import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js"; import { hashReadable } from "./ArtifactHash.js"; import { translateArtifactFailure } from "./ArtifactOperationFailure.js"; +import { analysisErrorUserMessage } from "../domain/analysisErrorPresentation.js"; /** Small real ZIP carriers with forged sizes; metadata-only controls, never decompressed. */ const writeZipWithDeclaredSize = async ( @@ -115,25 +116,28 @@ const admittedZip = async () => { return { path, content, handle, positional, initial }; }; +/** Read the first member of one method-93 ZIP fixture. */ +const readFirstZstandardMember = async (archive: Buffer): Promise => { + const root = await createTestTempDirectory("rea-zip-zstd-"); + const path = join(root, "method93.zip"); + await writeFile(path, archive); + const reader = new ZipArtifactReader(path, "zip"); + try { + const entries = []; + for await (const entry of reader.entries()) entries.push(entry); + const entry = entries[0]; + if (entry === undefined) throw new Error("Missing ZIP member"); + const chunks: Buffer[] = []; + for await (const chunk of await reader.open(entry)) + chunks.push(Buffer.from(chunk)); + return Buffer.concat(chunks); + } finally { + await reader.close(); + } +}; + describe("ZIP Zstandard method 93", () => { - const read = async (archive: Buffer): Promise => { - const root = await createTestTempDirectory("rea-zip-zstd-"); - const path = join(root, "method93.zip"); - await writeFile(path, archive); - const reader = new ZipArtifactReader(path, "zip"); - try { - const entries = []; - for await (const entry of reader.entries()) entries.push(entry); - const entry = entries[0]; - if (entry === undefined) throw new Error("Missing ZIP member"); - const chunks: Buffer[] = []; - for await (const chunk of await reader.open(entry)) - chunks.push(Buffer.from(chunk)); - return Buffer.concat(chunks); - } finally { - await reader.close(); - } - }; + const read = readFirstZstandardMember; it.each([ Buffer.alloc(0), @@ -178,7 +182,6 @@ describe("ZIP Zstandard method 93", () => { it.each([ ["bad frame", Buffer.from("not a Zstandard frame")], ["truncated frame", Buffer.from("28b52ffd", "hex")], - ["excessive decoder window", Buffer.from("28b52ffd0090010000", "hex")], ] as const)("reports %s as an archive fault", async (_label, compressed) => { await expect( read(zstandardZipFixture(Buffer.alloc(0), { compressed })), @@ -267,6 +270,47 @@ describe("ZIP Zstandard method 93", () => { }); }); +describe("ZIP Zstandard decoder window", () => { + it("reports a valid frame above the decoder window as a resource limit", async () => { + const content = Buffer.from("REA independent frame control\n"); + // Window descriptor 0x90 declares 256 MiB; the single raw block is valid. + const largeWindow = Buffer.from( + "28b52ffd0090f1000052454120696e646570656e64656e74206672616d6520636f6e74726f6c0a", + "hex", + ); + const failure = await readFirstZstandardMember( + zstandardZipFixture(content, { compressed: largeWindow }), + ).catch((cause: unknown) => cause); + expect(failure).toMatchObject({ + name: "ArtifactReaderFailure", + reason: "limit", + boundary: "memory", + message: expect.stringContaining("fixed 128 MiB allowance"), + cause: { code: "ZSTD_error_frameParameter_windowTooLarge" }, + }); + const translated = translateArtifactFailure("extract_artifact", failure); + expect(translated).toMatchObject({ + reason: "limit", + boundary: "memory", + detail: expect.stringContaining("fixed 128 MiB allowance"), + }); + const userMessage = analysisErrorUserMessage(translated); + expect(userMessage).toContain("fixed 128 MiB allowance"); + expect(userMessage).not.toContain("fresh copy"); + + // Control: the same frame with a 1 MiB window descriptor decodes exactly. + const smallWindow = Buffer.from(largeWindow); + smallWindow[5] = 0x50; + expect( + ( + await readFirstZstandardMember( + zstandardZipFixture(content, { compressed: smallWindow }), + ) + ).equals(content), + ).toBe(true); + }); +}); + describe("ZIP Zstandard member streams", () => { it.each(["abort", "consumer-close"] as const)( "settles a streaming Zstandard read after %s and allows another read", diff --git a/src/artifacts/ZipArtifactReader.ts b/src/artifacts/ZipArtifactReader.ts index 152c22a116c..2b1681a34fc 100644 --- a/src/artifacts/ZipArtifactReader.ts +++ b/src/artifacts/ZipArtifactReader.ts @@ -52,6 +52,13 @@ const ZIP_UTF8_DECODER = new TextDecoder("utf-8", { ignoreBOM: true, }); +/** Fixed method-93 decoder window: 2^27 bytes, matching the ELF/GPU decoders. */ +const ZIP_ZSTANDARD_WINDOW_LOG_MAX = 27; + +/** A frame above the window cap is a deliberate resource refusal, not damage. */ +const ZIP_ZSTANDARD_WINDOW_LIMIT = + "ZIP Zstandard member requires a decoder window above REA's fixed 128 MiB allowance; the archive is not necessarily corrupt, and this limit cannot be raised at runtime. Recompress the member with a window of at most 128 MiB, or decompress it with a tool that admits larger windows, then retry."; + /** Keep zip.js's framing, CRC, size checks and backpressure for method 93. */ class ZipZstandardDecompressionStream { readonly readable; @@ -62,7 +69,9 @@ class ZipZstandardDecompressionStream { createZstdDecompress({ chunkSize: options?.chunkSize, // Match the existing ELF/GPU Zstandard decoder's 128 MiB window cap. - params: { [constants.ZSTD_d_windowLogMax]: 27 }, + params: { + [constants.ZSTD_d_windowLogMax]: ZIP_ZSTANDARD_WINDOW_LOG_MAX, + }, }), ); this.readable = stream.readable; @@ -519,6 +528,14 @@ const typedZipFailure = ( "ZIP archive uses an unsupported Zstandard dictionary", { cause }, ); + if ( + compressionMethod === 93 && + code === "ZSTD_error_frameParameter_windowTooLarge" + ) + return new ArtifactReaderFailure("limit", ZIP_ZSTANDARD_WINDOW_LIMIT, { + cause, + boundary: "memory", + }); const invalidZstandard = compressionMethod === 93 && (code === "ZSTD_error_prefix_unknown" || @@ -526,7 +543,6 @@ const typedZipFailure = ( code === "ZSTD_error_corruption_detected" || code === "ZSTD_error_dictionary_corrupted" || code === "ZSTD_error_frameParameter_unsupported" || - code === "ZSTD_error_frameParameter_windowTooLarge" || code === "Z_BUF_ERROR"); const reason = ZIP_FAILURE_REASONS.get(cause.message) ??