fix(pyinstaller): distinguish declared-size overruns from limits (#13648)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 22:06:03 +00:00
1 parent 7a2be3c7d4
commit ae3a225f79
4 files changed
+78 -106

No files matched your search

@@ -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 () => {
@@ -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({
@@ -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" },
);
});
},
);
@@ -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<void> } => {
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();