fix(artifacts): preserve checksum facets without losing readable members

This commit is contained in:
morluto
2026-10-08 23:35:02 +08:00
parent ae44e01c04
commit 938d7adf70
9 changed files with 236 additions and 59 deletions
@@ -1,16 +1,22 @@
import { createHash } from "node:crypto";
import { writeFile } from "node:fs/promises";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { z } from "zod";
import {
MODE,
gzipCpio,
xarArchive,
rewriteXarToc,
} from "../../../src/artifacts/InstallerPackage.fixture.js";
import { runProviderAnalysis } from "../../../src/composition/directAnalysis.js";
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
import {
artifactInventoryResultSchema,
artifactExtractionResultSchema,
} from "../../../src/domain/artifactGraph.js";
import { analysisErrorProjectionSchema } from "../../../src/contracts/errorSchemas.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
@@ -61,6 +67,102 @@ const productPackage = (payloadText: string, extractedSha1?: string) =>
},
]);
describe("installer package checksum evidence", () => {
it("reports a stored SHA-1 mismatch independently of a valid decoded MD5", async () => {
const directory = await createTestTempDirectory("rea-pkg-checksum-reason-");
const path = join(directory, "Installer.pkg");
const data = Buffer.from("readable content");
const declared = "0".repeat(40);
await writeFile(
path,
rewriteXarToc(
xarArchive([{ name: "member", data, archivedSha1: declared }]),
(xml) =>
xml.replace(
/<extracted-checksum style="sha1">[^<]+<\/extracted-checksum>/u,
`<extracted-checksum style="md5">${createHash("md5").update(data).digest("hex")}</extracted-checksum>`,
),
),
);
const { error, ...projection } = z
.object({ error: z.string() })
.passthrough()
.parse(await runProviderAnalysis(path, "inventory_artifact", {}));
expect(error).toBe("Analysis failed");
const failed = analysisErrorProjectionSchema.parse(projection);
expect(failed).toMatchObject({
code: "artifact_integrity_mismatch",
details: {
logical_path: "member",
checksum_mismatches: [
{
representation: "stored",
algorithm: "sha1",
declared,
observed: createHash("sha1").update(data).digest("hex"),
},
],
},
});
});
it.each(["raw", "zlib"] as const)(
"hashes and extracts %s content with unsupported stored and decoded checksum algorithms",
async (encoding) => {
const directory = await createTestTempDirectory(
"rea-pkg-unknown-checksum-",
);
const path = join(directory, "Installer.pkg");
const data = Buffer.from("readable content");
await writeFile(
path,
rewriteXarToc(xarArchive([{ name: "member", data, encoding }]), (xml) =>
xml
.replaceAll(
'<archived-checksum style="sha1">',
'<archived-checksum style="unknown-stored">',
)
.replaceAll(
'<extracted-checksum style="sha1">',
'<extracted-checksum style="unknown-decoded">',
),
),
);
const inventory = artifactInventoryResultSchema.parse(
parseEvidence(await runProviderAnalysis(path, "inventory_artifact", {}))
.normalized_result,
);
const member = inventory.occurrences.find(
({ logical_path }) => logical_path === "member",
);
expect(member).toMatchObject({
hash_status: "verified",
limitations: [
expect.stringContaining("unsupported checksum unknown-decoded"),
expect.stringContaining(
"unsupported archived checksum unknown-stored",
),
],
});
expect(
inventory.nodes.find(
({ artifact_id }) => artifact_id === member?.artifact_id,
),
).toMatchObject({ sha256: sha256(data.toString()), size: data.length });
const output = join(directory, "extracted");
const extraction = artifactExtractionResultSchema.parse(
parseEvidence(
await runProviderAnalysis(path, "extract_artifact", {
output_root: output,
}),
).normalized_result,
);
expect(extraction.artifacts).toHaveLength(1);
expect(await readFile(join(output, "member"))).toEqual(data);
},
);
});
describe("installer package inventory", () => {
it("inventories xar members and expands gzip-cpio scripts and payloads", async () => {
const directory = await createTestTempDirectory("rea-pkg-inventory-");
@@ -138,7 +240,7 @@ describe("installer package inventory", () => {
.digest("hex");
expect(info).toMatchObject({ hash_status: "mismatched" });
expect(info?.limitations).toContain(
`Declared sha1 ${"0".repeat(40)} disagrees with observed ${observedSha1}.`,
`Declared decoded sha1 ${"0".repeat(40)} disagrees with observed ${observedSha1}.`,
);
expect(inventory.limitations).toContainEqual(
expect.stringContaining(