fix(artifacts): preserve completed CRC evidence and bound raw read work
This commit is contained in:
@@ -38,7 +38,7 @@ export interface ArtifactEntry {
|
||||
|
||||
/** Checksum evidence for a member representation the scanner cannot hash itself. */
|
||||
export interface ArtifactChecksumObservation {
|
||||
readonly representation: "stored";
|
||||
readonly representation: "stored" | "decoded";
|
||||
readonly algorithm: string;
|
||||
readonly declared: string;
|
||||
readonly observed: string;
|
||||
|
||||
@@ -10,6 +10,7 @@ import type { ArtifactCommand } from "../domain/artifactGraph.js";
|
||||
import {
|
||||
ArtifactReaderFailure,
|
||||
type ArtifactEntry,
|
||||
type ArtifactChecksumObservation,
|
||||
type ArtifactReader,
|
||||
} from "./ArtifactReader.js";
|
||||
|
||||
@@ -256,6 +257,14 @@ export class CpioArtifactReader implements ArtifactReader {
|
||||
readonly #links = new Map<string, Buffer>();
|
||||
readonly #pending = new Map<string, PendingLink[]>();
|
||||
readonly #aliases = new Map<string, Buffer>();
|
||||
readonly #integrity = new Map<
|
||||
string,
|
||||
readonly ArtifactChecksumObservation[]
|
||||
>();
|
||||
readonly #linkIntegrity = new Map<
|
||||
string,
|
||||
readonly ArtifactChecksumObservation[]
|
||||
>();
|
||||
#buffered = 0;
|
||||
|
||||
constructor(
|
||||
@@ -509,6 +518,9 @@ export class CpioArtifactReader implements ArtifactReader {
|
||||
bytes: Buffer,
|
||||
): ArtifactEntry {
|
||||
this.#aliases.set(key, bytes);
|
||||
const sourceIntegrity = this.#linkIntegrity.get(header.identity);
|
||||
if (sourceIntegrity !== undefined)
|
||||
this.#integrity.set(key, sourceIntegrity);
|
||||
return entryOf({
|
||||
path,
|
||||
kind: "file",
|
||||
@@ -588,9 +600,15 @@ export class CpioArtifactReader implements ArtifactReader {
|
||||
);
|
||||
}
|
||||
|
||||
#remember(header: CpioHeader, bytes: Buffer): void {
|
||||
#remember(
|
||||
header: CpioHeader,
|
||||
bytes: Buffer,
|
||||
observations: readonly ArtifactChecksumObservation[] = [],
|
||||
): void {
|
||||
if (this.#links.has(header.identity)) return;
|
||||
this.#links.set(header.identity, bytes);
|
||||
if (observations.length > 0)
|
||||
this.#linkIntegrity.set(header.identity, observations);
|
||||
this.#buffered += bytes.length;
|
||||
}
|
||||
|
||||
@@ -599,6 +617,7 @@ export class CpioArtifactReader implements ArtifactReader {
|
||||
source: ByteSource,
|
||||
header: CpioHeader,
|
||||
path: string,
|
||||
record?: (observation: ArtifactChecksumObservation) => void,
|
||||
): AsyncGenerator<Buffer> {
|
||||
let sum = 0;
|
||||
this.#cancelled();
|
||||
@@ -615,11 +634,20 @@ export class CpioArtifactReader implements ArtifactReader {
|
||||
source.take(padding(header.format, header.fileSize), "data padding"),
|
||||
);
|
||||
this.#cancelled();
|
||||
if (header.format === "crc" && sum !== header.check)
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
`cpio CRC disagrees with content: ${path}`,
|
||||
);
|
||||
if (header.format === "crc" && sum !== header.check) {
|
||||
if (record !== undefined)
|
||||
record({
|
||||
representation: "decoded",
|
||||
algorithm: "cpio-byte-sum",
|
||||
declared: header.check.toString(16).padStart(8, "0"),
|
||||
observed: sum.toString(16).padStart(8, "0"),
|
||||
});
|
||||
else
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
`cpio CRC disagrees with content: ${path}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async #collect(
|
||||
@@ -643,7 +671,10 @@ export class CpioArtifactReader implements ArtifactReader {
|
||||
|
||||
open(entry: ArtifactEntry): Promise<Readable> {
|
||||
const alias = this.#aliases.get(entry.adapterKey);
|
||||
if (alias !== undefined) return Promise.resolve(Readable.from([alias]));
|
||||
if (alias !== undefined) {
|
||||
this.decodedBudget.consume(alias.length, entry.path);
|
||||
return Promise.resolve(Readable.from([alias]));
|
||||
}
|
||||
const source = this.#source;
|
||||
const current = this.#current;
|
||||
if (
|
||||
@@ -661,13 +692,24 @@ export class CpioArtifactReader implements ArtifactReader {
|
||||
),
|
||||
);
|
||||
this.#consumed = true;
|
||||
const chunks = this.#verified(source, current.header, current.path);
|
||||
const chunks = this.#verified(
|
||||
source,
|
||||
current.header,
|
||||
current.path,
|
||||
this.integrity === "record-and-continue"
|
||||
? (observed) => this.#integrity.set(entry.adapterKey, [observed])
|
||||
: undefined,
|
||||
);
|
||||
if (!this.#shouldKeep(current.header))
|
||||
return Promise.resolve(Readable.from(chunks));
|
||||
// Keep a linked member's bytes for paths that share them.
|
||||
const kept: Buffer[] = [];
|
||||
const remember = (): void => {
|
||||
this.#remember(current.header, Buffer.concat(kept));
|
||||
this.#remember(
|
||||
current.header,
|
||||
Buffer.concat(kept),
|
||||
this.#integrity.get(entry.adapterKey),
|
||||
);
|
||||
};
|
||||
return Promise.resolve(
|
||||
Readable.from(
|
||||
@@ -686,6 +728,13 @@ export class CpioArtifactReader implements ArtifactReader {
|
||||
return [];
|
||||
}
|
||||
|
||||
/** Completed member CRC evidence, including inherited hard-link source contradictions. */
|
||||
integrityObservations(
|
||||
entry: ArtifactEntry,
|
||||
): readonly ArtifactChecksumObservation[] {
|
||||
return this.#integrity.get(entry.adapterKey) ?? [];
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
await this.#source?.close();
|
||||
this.#source = undefined;
|
||||
|
||||
@@ -62,6 +62,52 @@ it("shares an inflation budget across separate zlib members", async () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("charges raw member reads to the same cumulative work budget", async () => {
|
||||
await withReader(
|
||||
xarArchive([
|
||||
{ name: "a", data: Buffer.alloc(40, 65) },
|
||||
{ name: "b", data: Buffer.alloc(40, 66) },
|
||||
]),
|
||||
async (reader) => {
|
||||
let read = 0;
|
||||
await expect(
|
||||
(async () => {
|
||||
for await (const entry of reader.entries()) {
|
||||
await buffer(await reader.open(entry));
|
||||
read++;
|
||||
}
|
||||
})(),
|
||||
).rejects.toMatchObject({
|
||||
reason: "limit",
|
||||
message: expect.stringContaining("Decoded archive budget"),
|
||||
});
|
||||
expect(read).toBe(1);
|
||||
},
|
||||
new ArtifactDecodedBudget(64),
|
||||
);
|
||||
});
|
||||
|
||||
it("validates unsafe extents even when a member is unsupported and would not be opened", async () => {
|
||||
const bytes = rewriteXarToc(
|
||||
xarArchive([{ name: "a", encoding: "bzip2", data: Buffer.from("opaque") }]),
|
||||
(xml) =>
|
||||
xml.replace(
|
||||
/<offset>20<\/offset>/u,
|
||||
`<offset>${Number.MAX_SAFE_INTEGER}</offset>`,
|
||||
),
|
||||
);
|
||||
await withReader(bytes, async (reader) => {
|
||||
await expect(
|
||||
(async () => {
|
||||
for await (const entry of reader.entries()) void entry;
|
||||
})(),
|
||||
).rejects.toMatchObject({
|
||||
reason: "format",
|
||||
message: expect.stringContaining("heap extent"),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it.each(["", " ", "1e2", "0x10"])(
|
||||
"rejects nondecimal/empty numeric TOC data: %j",
|
||||
async (value) => {
|
||||
|
||||
@@ -6,7 +6,10 @@ import { createInflate, inflateSync } from "node:zlib";
|
||||
import { DOMParser, type Element, type Node } from "@xmldom/xmldom";
|
||||
|
||||
import type { ArtifactCommand } from "../domain/artifactGraph.js";
|
||||
import { ArtifactDecodedBudget } from "./ArtifactDecodedBudget.js";
|
||||
import {
|
||||
ArtifactDecodedBudget,
|
||||
ArtifactBudgetTransform,
|
||||
} from "./ArtifactDecodedBudget.js";
|
||||
import { artifactStreamPipeline } from "./ArtifactStreamPipeline.js";
|
||||
import {
|
||||
xarInteger as integer,
|
||||
@@ -233,10 +236,7 @@ const collectMembers = (
|
||||
kind,
|
||||
...(unsupportedType === undefined ? {} : { unsupportedType }),
|
||||
mode,
|
||||
data:
|
||||
kind === "file" && unsupportedType === undefined
|
||||
? parseData(next.element)
|
||||
: undefined,
|
||||
data: parseData(next.element),
|
||||
link: kind === "symlink" ? textOf(next.element, "link") : undefined,
|
||||
});
|
||||
pending.push(
|
||||
@@ -508,6 +508,13 @@ export class XarArtifactReader implements ArtifactReader {
|
||||
await this.#verifyToc(tocElement, compressed, headerChecksumAlg);
|
||||
this.#members = collectMembers(tocElement, this.decodedBudget);
|
||||
for (const member of this.#members) {
|
||||
if (member.data !== undefined)
|
||||
xarHeapPosition(
|
||||
this.#heap,
|
||||
member.data.offset,
|
||||
member.data.length,
|
||||
this.#size,
|
||||
);
|
||||
if (this.#byPath.has(member.path))
|
||||
throw new ArtifactReaderFailure(
|
||||
"path",
|
||||
@@ -781,8 +788,10 @@ export class XarArtifactReader implements ArtifactReader {
|
||||
this.#integrity.set(entry.adapterKey, [observation]);
|
||||
},
|
||||
);
|
||||
const stages: Transform[] =
|
||||
archivedVerifier === undefined ? [] : [archivedVerifier];
|
||||
const stages: Transform[] = [
|
||||
new ArtifactBudgetTransform(this.decodedBudget, entry.path, signal),
|
||||
];
|
||||
if (archivedVerifier !== undefined) stages.push(archivedVerifier);
|
||||
if (data.encoding === "application/x-gzip")
|
||||
stages.push(
|
||||
createInflate(),
|
||||
|
||||
@@ -63,9 +63,11 @@ export const extractArtifact = async (
|
||||
manifest: snapshot.manifest,
|
||||
occurrences,
|
||||
nodes,
|
||||
unavailablePaths: snapshot.occurrences
|
||||
.filter(({ hash_status }) => hash_status === "unavailable")
|
||||
.map(({ logical_path }) => logical_path),
|
||||
unavailablePaths: new Set(
|
||||
snapshot.occurrences
|
||||
.filter(({ hash_status }) => hash_status === "unavailable")
|
||||
.map(({ logical_path }) => logical_path),
|
||||
),
|
||||
};
|
||||
const selected = selectedOccurrences.map((occurrence) => {
|
||||
if (
|
||||
@@ -147,7 +149,7 @@ const materializeSelection = async ({
|
||||
if (selectedItem === undefined) {
|
||||
if (
|
||||
entry.contentUnavailable === true &&
|
||||
inventory.unavailablePaths.includes(path)
|
||||
inventory.unavailablePaths.has(path)
|
||||
)
|
||||
return false;
|
||||
if (entry.kind === "file" || entry.kind === "slice")
|
||||
@@ -228,7 +230,7 @@ const createExtractionResult = (
|
||||
provenance: [],
|
||||
limitations: [
|
||||
"All available selected regular files were materialized; expanded PKG Payload/Scripts archives are represented by their extracted children.",
|
||||
...inventory.unavailablePaths.map(
|
||||
...[...inventory.unavailablePaths].map(
|
||||
(path) =>
|
||||
`Unavailable member ${path} was not extracted; see its inventory occurrence for the reason.`,
|
||||
),
|
||||
@@ -240,7 +242,7 @@ interface LoadedInventory {
|
||||
readonly manifest: ArtifactGraphManifest;
|
||||
readonly occurrences: ReadonlyMap<string, ArtifactOccurrence>;
|
||||
readonly nodes: ReadonlyMap<string, ArtifactNode>;
|
||||
readonly unavailablePaths: readonly string[];
|
||||
readonly unavailablePaths: ReadonlySet<string>;
|
||||
}
|
||||
|
||||
const collectOccurrences = (
|
||||
|
||||
@@ -232,8 +232,8 @@ const digestArtifactEntry = async (
|
||||
]
|
||||
: []),
|
||||
...storedMismatches.map(
|
||||
({ algorithm, declared, observed }) =>
|
||||
`Declared stored ${algorithm} ${declared} disagrees with observed ${observed}.`,
|
||||
({ representation, algorithm, declared, observed }) =>
|
||||
`Declared ${representation} ${algorithm} ${declared} disagrees with observed ${observed}.`,
|
||||
),
|
||||
],
|
||||
};
|
||||
|
||||
@@ -213,11 +213,18 @@ describe("installer package CRC recovery", () => {
|
||||
}),
|
||||
).normalized_result,
|
||||
);
|
||||
const bad = inventory.occurrences.find(
|
||||
({ logical_path: logical }) => logical === "Payload/bad",
|
||||
);
|
||||
expect(bad).toMatchObject({ hash_status: "mismatched" });
|
||||
expect(
|
||||
inventory.occurrences.find(
|
||||
({ logical_path: logical }) => logical === "Payload/bad",
|
||||
inventory.nodes.find(
|
||||
({ artifact_id }) => artifact_id === bad?.artifact_id,
|
||||
),
|
||||
).toMatchObject({ hash_status: "unavailable" });
|
||||
).toMatchObject({ sha256: sha256("abc"), size: 3 });
|
||||
expect(bad?.limitations).toContain(
|
||||
"Declared decoded cpio-byte-sum 00000001 disagrees with observed 00000126.",
|
||||
);
|
||||
expect(
|
||||
inventory.occurrences.find(
|
||||
({ logical_path: logical }) => logical === "Payload/good",
|
||||
|
||||
Reference in New Issue
Block a user