diff --git a/src/artifacts/ArtifactReader.ts b/src/artifacts/ArtifactReader.ts index 81d448d07..782f2c883 100644 --- a/src/artifacts/ArtifactReader.ts +++ b/src/artifacts/ArtifactReader.ts @@ -27,6 +27,8 @@ export interface ArtifactEntry { }; /** The container records this member but does not hold its bytes, such as an unresolved hard link. */ readonly contentUnavailable?: boolean; + /** A consumed representation contradicted its declared integrity metadata. */ + readonly integrityMismatched?: boolean; /** Archive format of this member's own bytes, when the scan should expand it. */ readonly nestedArchive?: "gzip-cpio"; /** Filesystem identity captured during traversal, when supplied by an adapter. */ diff --git a/src/artifacts/ArtifactTraversal.ts b/src/artifacts/ArtifactTraversal.ts index 0481c3cf1..4e6fff7f6 100644 --- a/src/artifacts/ArtifactTraversal.ts +++ b/src/artifacts/ArtifactTraversal.ts @@ -45,9 +45,12 @@ export const visitArtifactTree = async ( continue; } const entry = next.value; - const path = normalizeArtifactPath( - frame.prefix === "" ? entry.path : `${frame.prefix}/${entry.path}`, - ); + // Charge the full retained namespace, including all enclosing prefixes, + // independently from each reader's own raw metadata/index charge. + const combined = + frame.prefix === "" ? entry.path : `${frame.prefix}/${entry.path}`; + root.decodedBudget?.consumeEntry(combined); + const path = normalizeArtifactPath(combined); const cpio = entry.nestedArchive === "gzip-cpio"; const asar = options.expandAsar !== false && diff --git a/src/artifacts/CpioArtifactReader.ts b/src/artifacts/CpioArtifactReader.ts index c9439f64e..fab622218 100644 --- a/src/artifacts/CpioArtifactReader.ts +++ b/src/artifacts/CpioArtifactReader.ts @@ -191,6 +191,7 @@ const entryOf = (member: { readonly limitations: readonly string[]; readonly size?: number | null; readonly contentUnavailable?: boolean; + readonly integrityMismatched?: boolean; }): ArtifactEntry => ({ path: member.path, kind: member.kind, @@ -209,6 +210,7 @@ const entryOf = (member: { limitations: member.limitations, adapterKey: member.key, ...(member.contentUnavailable === true ? { contentUnavailable: true } : {}), + ...(member.integrityMismatched === true ? { integrityMismatched: true } : {}), }); interface PendingLink { @@ -373,6 +375,7 @@ export class CpioArtifactReader implements ArtifactReader { key, header, limitations: "mismatch" in verified ? [verified.mismatch] : [], + integrityMismatched: "mismatch" in verified, }); return; } diff --git a/src/artifacts/InstallerPackageLimits.test.ts b/src/artifacts/InstallerPackageLimits.test.ts index 33ccf6ae6..a1841f39b 100644 --- a/src/artifacts/InstallerPackageLimits.test.ts +++ b/src/artifacts/InstallerPackageLimits.test.ts @@ -15,6 +15,7 @@ import { type XarFixtureMember, } from "./InstallerPackage.fixture.js"; import { XarArtifactReader } from "./XarArtifactReader.js"; +import { visitArtifactTree } from "./ArtifactTraversal.js"; const withReader = async ( bytes: Uint8Array, @@ -402,3 +403,50 @@ it("recovers directory CRC contradictions before yielding and reaches regular si await reader.close(); } }); + +it.each(["directory", "symlink"])( + "rejects data declarations attached to non-file type %s", + async (type) => { + const bytes = rewriteXarToc( + xarArchive([{ name: "a", data: Buffer.from("data") }]), + (xml) => xml.replace("file", `${type}`), + ); + await withReader(bytes, async (reader) => { + await expect( + (async () => { + for await (const entry of reader.entries()) void entry; + })(), + ).rejects.toMatchObject({ + reason: "format", + message: "xar non-file member declares data: a", + }); + }); + }, +); + +it("charges long enclosing prefixes for every retained nested entry", async () => { + const files = Array.from({ length: 100 }, (_, index) => ({ + name: `empty-${index}`, + mode: MODE.file, + })); + let member: XarFixtureMember = { name: "Payload", data: gzipCpio(files) }; + for (let index = 0; index < 18; index++) + member = { name: "d".repeat(200), type: "directory", children: [member] }; + await withReader( + xarArchive([member]), + async (reader) => { + let retained = 0; + await expect( + visitArtifactTree(reader, async ({ container }) => { + if (!container) retained++; + return container; + }), + ).rejects.toMatchObject({ + reason: "limit", + message: expect.stringContaining("Retained archive metadata budget"), + }); + expect(retained).toBeLessThan(files.length + 18); + }, + new ArtifactDecodedBudget(1024 * 1024, 512 * 1024), + ); +}); diff --git a/src/artifacts/XarArtifactReader.ts b/src/artifacts/XarArtifactReader.ts index bbc249916..c66df1795 100644 --- a/src/artifacts/XarArtifactReader.ts +++ b/src/artifacts/XarArtifactReader.ts @@ -231,6 +231,11 @@ const collectMembers = ( const type = textOf(next.element, "type")?.trim(); const mode = xarMode(textOf(next.element, "mode")); const { kind, unsupportedType } = classifyMemberType(type); + if (kind !== "file" && childElement(next.element, "data") !== undefined) + throw new ArtifactReaderFailure( + "format", + `xar non-file member declares data: ${path}`, + ); members.push({ path, kind, @@ -239,11 +244,11 @@ const collectMembers = ( data: parseData(next.element), link: kind === "symlink" ? textOf(next.element, "link") : undefined, }); - pending.push( - ...childElements(next.element, "file") - .map((element) => ({ element, parent: path })) - .reverse(), - ); + const children = childElements(next.element, "file"); + for (let index = children.length - 1; index >= 0; index--) { + const element = children[index]; + if (element !== undefined) pending.push({ element, parent: path }); + } } return members; }; diff --git a/src/artifacts/inventory/ArtifactGraphConstruction.ts b/src/artifacts/inventory/ArtifactGraphConstruction.ts index 91ac1f53e..f6730a20e 100644 --- a/src/artifacts/inventory/ArtifactGraphConstruction.ts +++ b/src/artifacts/inventory/ArtifactGraphConstruction.ts @@ -51,9 +51,11 @@ export const createOccurrence = ( executable: entry.executable, encrypted: entry.encrypted, hash_status: - entry.encrypted || entry.contentUnavailable === true - ? "unavailable" - : "not-hashed", + entry.integrityMismatched === true + ? "mismatched" + : entry.encrypted || entry.contentUnavailable === true + ? "unavailable" + : "not-hashed", source_location: entry.byteOffset === null || entry.declaredSize === null ? null @@ -107,6 +109,14 @@ export const materializeDirectoryNodes = ( format: "directory", executable: false, contentState: "virtual", + ...(directory.hash_status === "mismatched" + ? { + limitations: [ + ...directory.limitations, + "Directory structure identity is derived; its declared member integrity metadata disagreed.", + ], + } + : {}), }); const existing = nodes.get(node.artifact_id); nodes.set( @@ -114,7 +124,8 @@ export const materializeDirectoryNodes = ( existing?.kind === "framework" ? existing : node, ); directory.artifact_id = node.artifact_id; - directory.hash_status = "verified"; + if (directory.hash_status !== "mismatched") + directory.hash_status = "verified"; } }; diff --git a/tests/boundary/filesystem/installerPackageRecovery.test.ts b/tests/boundary/filesystem/installerPackageRecovery.test.ts index 7f0082a7b..00c62afb1 100644 --- a/tests/boundary/filesystem/installerPackageRecovery.test.ts +++ b/tests/boundary/filesystem/installerPackageRecovery.test.ts @@ -126,3 +126,39 @@ it("records stored-checksum contradictions without losing decoded content identi reason: "integrity", }); }); + +it("preserves recovered directory CRC mismatches through graph materialization", async () => { + const directory = await createTestTempDirectory("rea-pkg-directory-crc-"); + const path = join(directory, "Installer.pkg"); + await writeFile( + path, + xarArchive([ + { + name: "Payload", + data: gzipCpio( + [ + { name: "dir", mode: MODE.directory, check: 1 }, + { name: "dir/good", mode: MODE.file, data: "ok" }, + ], + "crc", + ), + }, + ]), + ); + const inventory = await scanArtifactInventory(path, { + integrity: { mode: "record-and-continue" }, + }); + expect( + inventory.occurrences.find( + ({ logical_path }) => logical_path === "Payload/dir", + ), + ).toMatchObject({ + hash_status: "mismatched", + limitations: ["cpio CRC disagrees with content: dir"], + }); + expect( + inventory.occurrences.find( + ({ logical_path }) => logical_path === "Payload/dir/good", + ), + ).toMatchObject({ hash_status: "verified" }); +});