diff --git a/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts b/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts index 8795172c53..685929fb2e 100644 --- a/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts +++ b/src/artifacts/pyinstaller/PyInstallerArtifactReader.test.ts @@ -644,51 +644,45 @@ it("rejects a cookie whose package range is outside the file", async () => { await reader.close(); }); -it("rejects a member whose declared size exceeds the decompression budget", async () => { - const path = await writeArchive( - "bomb", - cArchiveExecutable({ - layout: "2.1", - pythonVersion: 314, - pythonLibrary: "libpython3.14.dylib", - members: [ - { - name: "bomb.bin", - typeCode: "b", - payload: Buffer.alloc(0), - compress: true, - declaredUncompressedSize: 300 * 1024 * 1024, - }, - ], - }), - ); - await expect(scanCanonicalArtifactInventory(path)).rejects.toMatchObject({ - reason: "limit", - }); -}); - -it("rejects a zlib member that expands past its declared size", async () => { - const path = await writeArchive( - "expand", - cArchiveExecutable({ - layout: "2.1", - pythonVersion: 314, - pythonLibrary: "libpython3.14.dylib", - members: [ - { - name: "expand.bin", - typeCode: "b", - payload: Buffer.from("hello world hello world"), - compress: true, - declaredUncompressedSize: 4, - }, - ], - }), - ); - await expect(scanCanonicalArtifactInventory(path)).rejects.toMatchObject({ - reason: "limit", - }); -}); +it.each([ + ["an oversized declaration", "", 300 * 1024 * 1024, { reason: "limit" }], + [ + "plaintext longer than declared", + "hello world", + 4, + { reason: "integrity", integrityCheck: "declared-size" }, + ], + [ + "nonempty plaintext declared empty", + "x", + 0, + { reason: "integrity", integrityCheck: "declared-size" }, + ], +] as const)( + "distinguishes %s when hashing a compressed CArchive member", + async (_, body, declaredSize, expected) => { + const path = await writeArchive( + "compressed-size", + cArchiveExecutable({ + layout: "2.1", + pythonVersion: 314, + pythonLibrary: "libpython3.14.dylib", + members: [ + { + name: "member.bin", + typeCode: "b", + payload: Buffer.from(body), + compress: true, + declaredUncompressedSize: declaredSize, + }, + ], + }), + ); + await expect(scanCanonicalArtifactInventory(path)).rejects.toMatchObject( + expected, + ); + }, +); it("reads a compressed member whose uncompressed size is zero", async () => { const path = await writeArchive( @@ -731,27 +725,6 @@ it("reads a compressed member whose uncompressed size is zero", async () => { (node) => node.sha256 === sha256(derived(Buffer.alloc(0))), ), ).toMatchObject({ size: derived(Buffer.alloc(0)).length }); - - const expanded = await writeArchive( - "zero-declared", - cArchiveExecutable({ - layout: "2.1", - pythonVersion: 309, - pythonLibrary: "libpython3.9.so", - members: [ - { - name: "expand.bin", - typeCode: "x", - payload: Buffer.from("x"), - compress: true, - declaredUncompressedSize: 0, - }, - ], - }), - ); - await expect(scanCanonicalArtifactInventory(expanded)).rejects.toMatchObject({ - reason: "limit", - }); }); it("preserves an uppercase Z ZIP member as a readable file", async () => { diff --git a/src/artifacts/pyinstaller/PyInstallerBoundaries.test.ts b/src/artifacts/pyinstaller/PyInstallerBoundaries.test.ts index 87ffbe2025..0bf787492a 100644 --- a/src/artifacts/pyinstaller/PyInstallerBoundaries.test.ts +++ b/src/artifacts/pyinstaller/PyInstallerBoundaries.test.ts @@ -767,34 +767,22 @@ it("rejects metadata expansion before retaining every repeated long container pa ); }); -it.each([false, true])( - "validates compressed member bounds through the stream (truncated: %s)", - async (truncated) => { - const payload = Buffer.from("complete module payload"); - const encoded = deflateSync(payload); - const bytes = cArchiveExecutable({ - layout: "2.1", - pythonVersion: 309, - members: [ - { - name: "data", - typeCode: "x", - payload, - compress: true, - declaredUncompressedSize: truncated ? payload.length : 4, - }, - ], +it("reports a truncated compressed member when reading its stream", async () => { + const payload = Buffer.from("complete module payload"); + const encoded = deflateSync(payload); + const bytes = cArchiveExecutable({ + layout: "2.1", + pythonVersion: 309, + members: [{ name: "data", typeCode: "x", payload, compress: true }], + }); + bytes.writeUInt32BE(encoded.length - 2, 64 + encoded.length + 8); + await withPyInstallerReader(bytes, async (reader) => { + const entry = (await members(reader))[0]!; + await expect(buffer(await reader.open(entry))).rejects.toMatchObject({ + reason: "format", }); - if (truncated) - bytes.writeUInt32BE(encoded.length - 2, 64 + encoded.length + 8); - await withPyInstallerReader(bytes, async (reader) => { - const entry = (await members(reader))[0]!; - await expect(buffer(await reader.open(entry))).rejects.toMatchObject({ - reason: truncated ? "format" : "limit", - }); - }); - }, -); + }); +}); it("joins a late private-file close and retries its failed owner after cancellation", async () => { const bytes = cArchiveExecutable({ diff --git a/src/artifacts/pyinstaller/PyInstallerFormatBoundaries.test.ts b/src/artifacts/pyinstaller/PyInstallerFormatBoundaries.test.ts index 3548472f2a..7e2bfefd67 100644 --- a/src/artifacts/pyinstaller/PyInstallerFormatBoundaries.test.ts +++ b/src/artifacts/pyinstaller/PyInstallerFormatBoundaries.test.ts @@ -286,9 +286,11 @@ it.each(["malformed zlib", "declared plaintext overrun"] as const)( const list = async () => { for await (const entry of reader.entries()) void entry; }; - await expect(list()).rejects.toMatchObject({ - reason: fault === "malformed zlib" ? "format" : "limit", - }); + await expect(list()).rejects.toMatchObject( + fault === "malformed zlib" + ? { reason: "format" } + : { reason: "integrity", integrityCheck: "declared-size" }, + ); }); }, ); diff --git a/src/artifacts/pyinstaller/PyInstallerMemberStream.ts b/src/artifacts/pyinstaller/PyInstallerMemberStream.ts index c7426d1afa..6171281c39 100644 --- a/src/artifacts/pyinstaller/PyInstallerMemberStream.ts +++ b/src/artifacts/pyinstaller/PyInstallerMemberStream.ts @@ -51,11 +51,22 @@ export const readPyInstallerMemberPrefix = async ( return prefix.subarray(0, copied); }; +const declaredSizeMismatch = (): ArtifactReaderFailure => + new ArtifactReaderFailure( + "integrity", + "PyInstaller member size disagrees with its declaration or previous observation", + { integrityCheck: "declared-size" }, + ); + /** Backpressure bounds plaintext independently of the untrusted declared size. */ export const pyInstallerMemberStream = ( source: Readable, options: MemberStreamOptions, ): { readonly output: Readable; readonly completion: Promise } => { + const plaintextBound = Math.min( + options.maxBytes, + options.expectedBytes ?? options.maxBytes, + ); let bytes = 0; let headerSent = false; let completing = false; @@ -70,11 +81,13 @@ export const pyInstallerMemberStream = ( try { abortIfNeeded(options.signal); bytes += chunk.length; - if (bytes > options.maxBytes) - throw new ArtifactReaderFailure( - "limit", - "PyInstaller member exceeds its plaintext budget", - ); + if (bytes > plaintextBound) + throw options.expectedBytes === plaintextBound + ? declaredSizeMismatch() + : new ArtifactReaderFailure( + "limit", + "PyInstaller member exceeds its plaintext budget", + ); options.charge?.(chunk.length); prependHeader(); callback(null, chunk); @@ -99,11 +112,7 @@ export const pyInstallerMemberStream = ( options.expectedBytes !== undefined && bytes !== options.expectedBytes ) - throw new ArtifactReaderFailure( - "integrity", - "PyInstaller member size disagrees with its declaration or previous observation", - { integrityCheck: "declared-size" }, - ); + throw declaredSizeMismatch(); await options.complete?.(bytes); abortIfNeeded(options.signal); prependHeader();