fix(artifacts): budget full namespaces and retain directory contradictions
This commit is contained in:
@@ -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. */
|
||||
|
||||
@@ -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 &&
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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("<type>file</type>", `<type>${type}</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),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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" });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user