From df375cc70d3979f43970b6b79b7cbe4f5ac91d8f Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:08:45 -0500 Subject: [PATCH] fix(pyinstaller): validate TOC record framing before capacity (#12622) * fix(pyinstaller): validate TOC record framing before capacity * test(pyinstaller): assert TOC framing facts without prose --- src/artifacts/pyinstaller/CArchiveLayout.ts | 15 ++++---- .../PyInstallerFormatBoundaries.test.ts | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/artifacts/pyinstaller/CArchiveLayout.ts b/src/artifacts/pyinstaller/CArchiveLayout.ts index b9b3d980ba..4fc19297b2 100644 --- a/src/artifacts/pyinstaller/CArchiveLayout.ts +++ b/src/artifacts/pyinstaller/CArchiveLayout.ts @@ -177,13 +177,6 @@ export const parseCArchiveToc = ( ), ); while (offset < bytes.length) { - if (entries.length >= MAX_TOC_ENTRIES) - return err( - pyInstallerFailure( - "limit", - "CArchive table of contents exceeds its entry bound", - ), - ); if (offset + TOC_HEADER_BYTES > bytes.length) return malformedEntry("CArchive table of contents entry is truncated"); const entryLength = reader.u32(offset); @@ -191,6 +184,14 @@ export const parseCArchiveToc = ( return malformedEntry( "CArchive table of contents entry extends outside the table", ); + // Only a framed record can exceed the retained entry allowance. + if (entries.length >= MAX_TOC_ENTRIES) + return err( + pyInstallerFailure( + "limit", + "CArchive table of contents exceeds its entry bound", + ), + ); const nameBytes = bytes.subarray( offset + TOC_HEADER_BYTES, offset + entryLength, diff --git a/src/artifacts/pyinstaller/PyInstallerFormatBoundaries.test.ts b/src/artifacts/pyinstaller/PyInstallerFormatBoundaries.test.ts index 89cb5c4905..3548472f2a 100644 --- a/src/artifacts/pyinstaller/PyInstallerFormatBoundaries.test.ts +++ b/src/artifacts/pyinstaller/PyInstallerFormatBoundaries.test.ts @@ -17,6 +17,7 @@ import { PYTHON_39_MAGIC, } from "./CArchive.fixture.js"; import { readPyzArchive } from "./PyzArchive.js"; +import { parseCArchiveToc } from "./CArchiveLayout.js"; import { marshalCode } from "../../python/PythonPyc.fixture.js"; import { createEvidence } from "../../domain/evidence.js"; import { parseArtifactInventoryEvidence } from "../../domain/artifactInventoryEvidence.js"; @@ -66,6 +67,43 @@ describe.each(["2.0", "2.1"] as const)( }, ); +describe("CArchive entry admission at the TOC record limit", () => { + const entryCount = 100_000; + const entry = Buffer.alloc(32); + entry.writeUInt32BE(entry.length); + entry[17] = 0x78; + entry.write("member.txt", 18, "ascii"); + const boundary = entry.length * entryCount; + const toc = Buffer.alloc(boundary + entry.length, entry); + + it("accepts exactly the limit and refuses one more framed record", () => { + const admitted = parseCArchiveToc(toc.subarray(0, boundary)); + if (!admitted.ok) throw new Error(admitted.error.message); + expect(admitted.value).toHaveLength(entryCount); + expect(parseCArchiveToc(toc)).toMatchObject({ + ok: false, + error: { reason: "limit" }, + }); + }); + + it.each([4, 20])( + "preserves framing failure for %i trailing bytes at the entry limit", + (tailSize) => { + expect( + parseCArchiveToc(toc.subarray(0, boundary + tailSize)), + ).toMatchObject({ + ok: false, + error: { + reason: "format", + message: expect.stringContaining( + `table index ${entryCount} at TOC offset ${boundary}:`, + ), + }, + }); + }, + ); +}); + it.each([0, 1, 2])( "identifies malformed CArchive compression at table index %i", async (index) => {