Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(artifacts): bind inspection provenance to every retained inventory

morluto9ac91e0c1e13
diff --git a/src/domain/artifactInventoryEvidence.test.ts b/src/domain/artifactInventoryEvidence.test.ts
new file mode 100644
index 000000000..850d7f00d
--- /dev/null
+++ b/src/domain/artifactInventoryEvidence.test.ts
@@ -0,0 +1,75 @@
+import { expect, it } from "vitest";
+
+import { ARTIFACT_COMPARISON_EXAMPLE } from "../contracts/artifactComparisonExample.js";
+import { createArtifactInspection } from "./artifactInspection.js";
+import { parseArtifactInventoryEvidence } from "./artifactInventoryEvidence.js";
+import { createEvidence } from "./evidence.js";
+import { jsonValueSchema } from "./jsonValue.js";
+
+const inventoryEvidence = ARTIFACT_COMPARISON_EXAMPLE.left;
+const target = inventoryEvidence.subject;
+if (target === null) throw new Error("Artifact fixture has no target");
+
+const inspectionEvidence = (result: unknown, subject = target) =>
+ createEvidence(
+ {
+ path: subject.local_path,
+ sha256: subject.digest.sha256,
+ format: subject.format,
+ },
+ inventoryEvidence.provider,
+ {
+ operation: "inspect_artifact",
+ parameters: {},
+ result: jsonValueSchema.parse(result),
+ confidence: "observed",
+ authority: "shipped-artifact",
+ },
+ );
+
+const inspection = createArtifactInspection(inventoryEvidence);
+
+it("accepts an inspection whose carrier, declaration, and substep identities agree", () => {
+ expect(
+ parseArtifactInventoryEvidence(inspectionEvidence(inspection)).inventory,
+ ).toMatchObject({ complete: true, coverage: { status: "complete" } });
+});
+
+it("rejects an inspection carrier whose Evidence subject disagrees with its nested inventory", () => {
+ const carrier = inspectionEvidence(inspection, {
+ ...target,
+ digest: { sha256: "a".repeat(64) },
+ });
+ expect(() => parseArtifactInventoryEvidence(carrier)).toThrow(
+ "Artifact inspection Evidence subject does not match its root manifest",
+ );
+});
+
+it("rejects an inspection declaration whose root manifest disagrees with its nested inventory", () => {
+ const mismatched = {
+ ...inspection,
+ subject: {
+ ...inspection.subject,
+ root_artifact_id: `art_${"f".repeat(64)}`,
+ },
+ };
+ expect(() =>
+ parseArtifactInventoryEvidence(inspectionEvidence(mismatched)),
+ ).toThrow(
+ "Artifact inspection subject does not match its inventory manifest",
+ );
+});
+
+it("rejects a substep evidence ID that disagrees with its authenticated nested Evidence", () => {
+ const [first, ...remaining] = inspection.substeps;
+ if (first === undefined) throw new Error("Expected inventory substep");
+ const mismatched = {
+ ...inspection,
+ substeps: [{ ...first, evidence_id: `ev_${"f".repeat(64)}` }, ...remaining],
+ };
+ expect(() =>
+ parseArtifactInventoryEvidence(inspectionEvidence(mismatched)),
+ ).toThrow(
+ "Artifact inspection substep evidence ID does not match nested Evidence",
+ );
+});
diff --git a/src/domain/artifactInventoryEvidence.ts b/src/domain/artifactInventoryEvidence.ts
index a4e071a9b..7554e8655 100644
--- a/src/domain/artifactInventoryEvidence.ts
+++ b/src/domain/artifactInventoryEvidence.ts
@@ -17,7 +17,10 @@ import {
occurrenceIdForLocation,
} from "./artifactIdentity.js";
import { parseEvidence, type Evidence } from "./evidence.js";
-import { artifactInspectionResultSchema } from "./artifactInspection.js";
+import {
+ artifactInspectionResultSchema,
+ type ArtifactInspectionResult,
+} from "./artifactInspection.js";
/** Complete or partially assembled inventory used by comparison workflows. */
export interface InventorySet {
@@ -52,36 +55,28 @@ export const parseArtifactInventoryEvidence = (
): ParsedInventorySet => {
const evidence = (Array.isArray(input) ? input : [input]).map(parseEvidence);
const pages = evidence.flatMap((carrier) => {
- let inventoryEvidence: readonly Evidence[] | undefined;
- if (carrier.operation === "inventory_artifact") {
- inventoryEvidence = [carrier];
- } else if (carrier.operation === "inspect_artifact") {
- const inspection = artifactInspectionResultSchema.parse(
- carrier.normalized_result,
- );
- if (inspection.coverage.substeps_completed !== inspection.substeps.length)
- throw new TypeError(
- "Artifact inspection substep count does not match its coverage declaration",
- );
- inventoryEvidence = inspection.substeps.map((substep) =>
- parseEvidence(substep.evidence),
- );
- }
- if (inventoryEvidence === undefined)
+ if (carrier.operation === "inventory_artifact")
+ return [parseInventoryEvidencePage(carrier)];
+ if (carrier.operation !== "inspect_artifact")
throw new TypeError(
"Artifact comparison requires inspect_artifact Evidence",
);
- return inventoryEvidence.map((pageEvidence) => {
- const inventory = artifactInventoryResultSchema.parse(
- pageEvidence.normalized_result,
- );
- validateArtifactInventorySubject(
- pageEvidence.subject,
- inventory.manifest,
+
+ const inspection = artifactInspectionResultSchema.parse(
+ carrier.normalized_result,
+ );
+ if (inspection.coverage.substeps_completed !== inspection.substeps.length)
+ throw new TypeError(
+ "Artifact inspection substep count does not match its coverage declaration",
);
- validateInventoryPage(inventory);
- return inventory;
- });
+ const inventoryPages = inspection.substeps.map(
+ parseInspectionInventoryPage,
+ );
+ const firstPage = inventoryPages[0];
+ if (firstPage === undefined)
+ throw new TypeError("Artifact inspection requires inventory substeps");
+ validateInspectionIdentity(carrier, inspection, firstPage.manifest);
+ return inventoryPages;
});
return {
evidence,
@@ -89,15 +84,70 @@ export const parseArtifactInventoryEvidence = (
};
};
+/** Ensure both inspection declarations identify the inventory facts it carries. */
+const validateInspectionIdentity = (
+ carrier: Evidence,
+ inspection: ArtifactInspectionResult,
+ manifest: ArtifactInventoryResult["manifest"],
+): void => {
+ if (
+ inspection.subject.manifest_id !== manifest.manifest_id ||
+ inspection.subject.root_artifact_id !== manifest.root_artifact_id ||
+ inspection.subject.root_sha256 !== manifest.root_sha256 ||
+ inspection.subject.root_format !== manifest.root_format
+ )
+ throw new TypeError(
+ "Artifact inspection subject does not match its inventory manifest",
+ );
+ if (
+ carrier.subject === null ||
+ carrier.subject.digest.sha256 !== manifest.root_sha256 ||
+ carrier.subject.format !== manifest.root_format
+ )
+ throw new TypeError(
+ "Artifact inspection Evidence subject does not match its root manifest",
+ );
+};
+
/** Validate one already authenticated parent payload with the same graph proofs. */
export const parseArtifactInventoryResult = (
input: unknown,
subject: Evidence["subject"],
-): InventorySet => {
+): InventorySet =>
+ assembleInventoryResults([parseInventoryPage(input, subject)]);
+
+/** Parse one already authenticated inventory result into its validated facts. */
+const parseInventoryPage = (
+ input: unknown,
+ subject: Evidence["subject"],
+): ArtifactInventoryResult => {
const inventory = artifactInventoryResultSchema.parse(input);
validateArtifactInventorySubject(subject, inventory.manifest);
validateInventoryPage(inventory);
- return assembleInventoryResults([inventory]);
+ return inventory;
+};
+
+/** Parse one authenticated inventory carrier into its validated owner facts. */
+const parseInventoryEvidencePage = (
+ evidence: Evidence,
+): ArtifactInventoryResult => {
+ if (evidence.operation !== "inventory_artifact")
+ throw new TypeError(
+ "Artifact inventory page must be inventory_artifact Evidence",
+ );
+ return parseInventoryPage(evidence.normalized_result, evidence.subject);
+};
+
+/** Authenticate one inspection substep and parse its inventory exactly once. */
+const parseInspectionInventoryPage = (
+ substep: ArtifactInspectionResult["substeps"][number],
+): ArtifactInventoryResult => {
+ const evidence = parseEvidence(substep.evidence);
+ if (substep.evidence_id !== evidence.evidence_id)
+ throw new TypeError(
+ "Artifact inspection substep evidence ID does not match nested Evidence",
+ );
+ return parseInventoryEvidencePage(evidence);
};
/** Both inventory carriers require the same authenticated root identity. */