fix(pyinstaller): validate CArchive member name framing (#12214)

Co-authored-by: rudycelekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
morlutoandrudycelekli authored and GitHub committed 2026-11-19 05:35:10 +00:00
1 parent 0e07437bc7
commit 166347f38b
4 files changed
+61 -14

No files matched your search

+17 -11
View File
@@ -1,6 +1,7 @@
import type { FileHandle } from "node:fs/promises";
import { readPyInstallerBytes } from "./PyInstallerBytes.js";
import { BinaryReader } from "../../domain/binaryContainer.js";
import { err, ok, type Result } from "../../domain/result.js";
import {
pyInstallerFailure,
@@ -165,6 +166,7 @@ export const readCArchiveToc = async (
export const parseCArchiveToc = (
bytes: Buffer,
): Result<readonly CArchiveEntry[], PyInstallerFailure> => {
const reader = new BinaryReader(bytes, false, "pyinstaller");
const entries: CArchiveEntry[] = [];
let offset = 0;
const malformedEntry = (message: string): Result<never, PyInstallerFailure> =>
@@ -184,7 +186,7 @@ export const parseCArchiveToc = (
);
if (offset + TOC_HEADER_BYTES > bytes.length)
return malformedEntry("CArchive table of contents entry is truncated");
const entryLength = bytes.readUInt32BE(offset);
const entryLength = reader.u32(offset);
if (entryLength < TOC_HEADER_BYTES || offset + entryLength > bytes.length)
return malformedEntry(
"CArchive table of contents entry extends outside the table",
@@ -212,9 +214,9 @@ export const parseCArchiveToc = (
typeCode: sourceTypeCode,
// Other printable codes keep generic-file behavior; compression is independent.
role: CARCHIVE_ENTRY_ROLES.get(sourceTypeCode) ?? "other",
offset: bytes.readUInt32BE(offset + 4),
compressedSize: bytes.readUInt32BE(offset + 8),
uncompressedSize: bytes.readUInt32BE(offset + 12),
offset: reader.u32(offset + 4),
compressedSize: reader.u32(offset + 8),
uncompressedSize: reader.u32(offset + 12),
compressionFlag,
});
offset += entryLength;
@@ -253,10 +255,11 @@ const parseCookie = (
),
);
const view = tail.subarray(relative, relative + cookieBytes);
const packageLength = view.readUInt32BE(8);
const tocOffset = view.readUInt32BE(12);
const tocLength = view.readUInt32BE(16);
const pythonVersion = view.readUInt32BE(20);
const reader = new BinaryReader(view, false, "pyinstaller");
const packageLength = reader.u32(8);
const tocOffset = reader.u32(12);
const tocLength = reader.u32(16);
const pythonVersion = reader.u32(20);
if (
!safeSum(tocOffset, tocLength, cookieBytes) ||
packageLength !== tocOffset + tocLength + cookieBytes
@@ -314,11 +317,14 @@ const libraryName = (
const tocName = (bytes: Buffer): Result<string, PyInstallerFailure> => {
const end = bytes.indexOf(0);
const text = end < 0 ? bytes : bytes.subarray(0, end);
if (text.length === 0)
if (end < 0 || bytes.subarray(end + 1).some((byte) => byte !== 0))
return err(
pyInstallerFailure("format", "CArchive entry name is not NUL-padded"),
);
if (end === 0)
return err(pyInstallerFailure("format", "CArchive entry name is empty"));
try {
return ok(strictUtf8Decoder("preserve").decode(text));
return ok(strictUtf8Decoder("preserve").decode(bytes.subarray(0, end)));
} catch {
return err(
pyInstallerFailure("format", "CArchive entry name is not valid UTF-8"),
@@ -3,7 +3,7 @@ import { readFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { deflateSync } from "node:zlib";
import { randomBytes } from "node:crypto";
import { expect, it, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { withPyInstallerReader } from "../../../tests/fixtures/pyinstallerReader.js";
import {
scanCanonicalArtifactInventory,
@@ -25,6 +25,47 @@ import {
type ArtifactEntry,
} from "../../resources/ArtifactReader.js";
describe.each(["2.0", "2.1"] as const)(
"CArchive names with a %s cookie",
(layout) => {
const name = "member-é";
const nameOffset = 18;
const record = Buffer.alloc(32);
record.writeUInt32BE(record.length, 0);
record[17] = 0x78; // data member
const nameEnd = nameOffset + record.write(name, nameOffset, "utf8");
const readPaths = async (tocBytes: Buffer) => {
const paths: string[] = [];
await withPyInstallerReader(
cArchiveExecutable({
layout,
pythonVersion: layout === "2.0" ? 27 : 309,
members: [],
tocBytes,
}),
async (reader) => {
for await (const entry of reader.entries()) paths.push(entry.path);
},
);
return paths;
};
it("preserves the UTF-8 name with a terminator and zero padding", async () => {
await expect(readPaths(record)).resolves.toEqual([name]);
});
it.each([
["missing terminator", nameEnd],
["nonzero byte after terminator", nameEnd + 1],
] as const)("reports %s at the rejected record", async (_case, fillAt) => {
const malformed = Buffer.from(record).fill(0x61, fillAt);
await expect(readPaths(malformed)).rejects.toMatchObject({
reason: "format",
message: expect.stringContaining("table index 0 at TOC offset 0"),
});
});
},
);
it.each([0, 1, 2])(
"identifies malformed CArchive compression at table index %i",
async (index) => {