From 6cdaf8f2b3be16c38f3205ee5d4482d7b373fc3d Mon Sep 17 00:00:00 2001 From: N0zoM1z0 <1705004398@qq.com> Date: Tue, 14 Jul 2026 21:01:11 +0000 Subject: [PATCH] fix(application): verify replay inventory controls --- .../CrossVersionInvestigationReplay.ts | 142 +++++++++++++--- tests/investigationWorkspace.test.ts | 158 +++++++++++++++++- 2 files changed, 276 insertions(+), 24 deletions(-) diff --git a/src/application/CrossVersionInvestigationReplay.ts b/src/application/CrossVersionInvestigationReplay.ts index fa153c8ce..2e9b2f43f 100644 --- a/src/application/CrossVersionInvestigationReplay.ts +++ b/src/application/CrossVersionInvestigationReplay.ts @@ -2,6 +2,8 @@ import { changedBehaviorResultSchema, type ChangedBehaviorResult, } from "../domain/changedBehavior.js"; +import { artifactInventoryResultSchema } from "../domain/artifactGraph.js"; +import { parseArtifactInventoryEvidence } from "../domain/artifactInventoryEvidence.js"; import type { Evidence } from "../domain/evidence.js"; import { EvidenceIntegrityError, @@ -53,40 +55,134 @@ const replayRequestMatches = ( run.integrity_policy === input.integrity_policy && run.integrity_continue_approved === input.integrity_continue_approved && run.max_integrity_mismatches === input.max_integrity_mismatches && - inventoryEvidenceMatches( - workspace, - run.left_inventory_evidence_ids, - input.left_path, - run.left, - ) && - inventoryEvidenceMatches( - workspace, - run.right_inventory_evidence_ids, - input.right_path, - run.right, - ); + inventoryEvidenceMatches(workspace, run.left_inventory_evidence_ids, { + path: input.left_path, + target: run.left, + input, + }) && + inventoryEvidenceMatches(workspace, run.right_inventory_evidence_ids, { + path: input.right_path, + target: run.right, + input, + }); + +interface InventoryReplayExpectation { + readonly path: string; + readonly target: InvestigationRun["left"]; + readonly input: CrossVersionInvestigationInput; +} const inventoryEvidenceMatches = ( workspace: InvestigationWorkspace, evidenceIds: readonly string[], - path: string, - target: InvestigationRun["left"], + expectation: InventoryReplayExpectation, ): boolean => { const byId = new Map( workspace.bundle.records.map((record) => [record.evidence_id, record]), ); - return evidenceIds.every((evidenceId) => { - const evidence = byId.get(evidenceId); - return ( - evidence?.operation === "inventory_artifact" && - providerMatches(evidence.provider, ARTIFACT_GRAPH_PROVIDER) && - evidence.subject?.local_path === path && - evidence.subject.digest.sha256 === target.root_sha256 && - evidence.subject.format === target.format - ); + const evidence = evidenceIds.flatMap((evidenceId) => { + const record = byId.get(evidenceId); + return record === undefined ? [] : [record]; }); + if ( + evidence.length !== evidenceIds.length || + !evidence.every((record, index) => + inventoryPageMatches(record, index, expectation), + ) + ) + return false; + try { + const inventory = parseArtifactInventoryEvidence(evidence).inventory; + return ( + inventory.complete && + inventory.manifest.root_sha256 === expectation.target.root_sha256 && + inventory.manifest.graph_sha256 === expectation.target.graph_sha256 && + inventory.manifest.manifest_id === expectation.target.manifest_id && + inventory.manifest.root_format === expectation.target.format + ); + } catch { + return false; + } }; +const inventoryPageMatches = ( + evidence: Evidence, + index: number, + expectation: InventoryReplayExpectation, +): boolean => { + const page = artifactInventoryResultSchema.safeParse( + evidence.normalized_result, + ); + if (!page.success) return false; + const offset = index * expectation.input.options.page_size; + return ( + inventoryEvidenceMetadataMatches(evidence, expectation) && + inventoryParametersMatch(evidence.parameters, offset, expectation.input) && + inventoryResultPageMatches(page.data, offset, expectation.input) + ); +}; + +const inventoryEvidenceMetadataMatches = ( + evidence: Evidence, + expectation: InventoryReplayExpectation, +): boolean => + evidence.operation === "inventory_artifact" && + evidence.predicate_type === "rea.analysis/v2" && + providerMatches(evidence.provider, ARTIFACT_GRAPH_PROVIDER) && + evidence.subject?.local_path === expectation.path && + evidence.subject.digest.sha256 === expectation.target.root_sha256 && + evidence.subject.format === expectation.target.format && + evidence.confidence === "observed" && + evidence.authority === "shipped-artifact" && + evidence.raw_result === null && + evidence.environment === null && + evidence.evidence_links.length === 0; + +const inventoryResultPageMatches = ( + page: ReturnType, + offset: number, + input: CrossVersionInvestigationInput, +): boolean => + page.nodes.offset === offset && + page.nodes.limit === input.options.page_size && + page.occurrences.offset === offset && + page.occurrences.limit === input.options.page_size && + page.edges.offset === offset && + page.edges.limit === input.options.page_size && + valuesMatch(page.limits, traversalLimits(input)); + +const inventoryParametersMatch = ( + parameters: Readonly>, + offset: number, + input: CrossVersionInvestigationInput, +): boolean => + Object.keys(parameters).length === 15 && + parameters.node_offset === offset && + parameters.node_limit === input.options.page_size && + parameters.occurrence_offset === offset && + parameters.occurrence_limit === input.options.page_size && + parameters.edge_offset === offset && + parameters.edge_limit === input.options.page_size && + parameters.max_entries === input.options.max_entries && + parameters.max_total_bytes === input.options.max_total_bytes && + parameters.max_entry_bytes === input.options.max_entry_bytes && + parameters.max_compression_ratio === input.options.max_compression_ratio && + parameters.max_depth === input.options.max_depth && + parameters.max_path_bytes === input.options.max_path_bytes && + parameters.integrity_policy === input.integrity_policy && + parameters.integrity_continue_approved === + input.integrity_continue_approved && + parameters.max_integrity_mismatches === input.max_integrity_mismatches; + +const traversalLimits = (input: CrossVersionInvestigationInput) => ({ + max_entries: input.options.max_entries, + max_total_bytes: input.options.max_total_bytes, + max_entry_bytes: input.options.max_entry_bytes, + max_compression_ratio: input.options.max_compression_ratio, + max_depth: input.options.max_depth, + max_path_bytes: input.options.max_path_bytes, +}); + const validateCompletedReplayEvidence = ( workspace: InvestigationWorkspace, run: InvestigationRun, diff --git a/tests/investigationWorkspace.test.ts b/tests/investigationWorkspace.test.ts index 0029ed14b..ab37acf87 100644 --- a/tests/investigationWorkspace.test.ts +++ b/tests/investigationWorkspace.test.ts @@ -21,16 +21,23 @@ import { readInvestigationWorkspace, writeInvestigationWorkspace, } from "../src/application/InvestigationWorkspaceStore.js"; -import { changedBehaviorResultSchema } from "../src/domain/changedBehavior.js"; +import { compareArtifacts } from "../src/domain/artifactComparison.js"; +import { + changedBehaviorResultSchema, + findChangedBehavior, +} from "../src/domain/changedBehavior.js"; +import { createEvidence, type Evidence } from "../src/domain/evidence.js"; import { createEvidenceBundle } from "../src/domain/evidenceBundle.js"; import type { EvidenceFilePolicy } from "../src/domain/evidenceBundle.js"; import { createInvestigationWorkspace, crossVersionInvestigationInputSchema, investigationRunSchema, + investigationRunSummarySchema, parseInvestigationWorkspace, serializeInvestigationWorkspace, } from "../src/domain/investigationWorkspace.js"; +import type { JsonValue } from "../src/domain/jsonValue.js"; import { ok } from "../src/domain/result.js"; const digestCanonical = (value: unknown): string => { @@ -55,6 +62,41 @@ const policy = (root: string): EvidenceFilePolicy => ({ maxNodes: 1_000_000, }); +const recreateEvidence = ( + evidence: Evidence, + overrides: { + readonly parameters?: Readonly>; + readonly result?: JsonValue; + readonly evidenceLinks?: readonly string[]; + }, +): Evidence => + createEvidence( + evidence.subject === null + ? undefined + : { + path: evidence.subject.local_path, + sha256: evidence.subject.digest.sha256, + format: evidence.subject.format, + ...(evidence.subject.architecture === null + ? {} + : { architecture: evidence.subject.architecture }), + }, + evidence.provider, + { + predicateType: evidence.predicate_type, + operation: evidence.operation, + parameters: overrides.parameters ?? evidence.parameters, + result: overrides.result ?? evidence.normalized_result, + rawResult: evidence.raw_result, + confidence: evidence.confidence, + authority: evidence.authority, + environment: evidence.environment, + limitations: evidence.limitations, + locations: evidence.locations, + evidenceLinks: overrides.evidenceLinks ?? evidence.evidence_links, + }, + ); + const fixture = async () => { directory = await mkdtemp(join(tmpdir(), "rea-workspace-")); const left = join(directory, "left"); @@ -289,6 +331,120 @@ describe("persistent cross-version investigation workspace", () => { }, }); expect(authorizeInputRead).not.toHaveBeenCalled(); + + const byId = new Map( + completed.value.workspace.bundle.records.map((record) => [ + record.evidence_id, + record, + ]), + ); + const leftPages = run.left_inventory_evidence_ids.map((id) => byId.get(id)); + const rightPages = run.right_inventory_evidence_ids.map((id) => + byId.get(id), + ); + if ( + leftPages.some((record) => record === undefined) || + rightPages.some((record) => record === undefined) + ) + throw new Error("missing inventory fixture Evidence"); + const forgedFirstPage = recreateEvidence(leftPages[0]!, { + parameters: { + ...leftPages[0]!.parameters, + max_entries: input.options.max_entries - 1, + }, + }); + const forgedLeftPages = [forgedFirstPage, ...leftPages.slice(1)]; + const comparison = byId.get(run.comparison_evidence_id ?? ""); + const result = byId.get(run.result_evidence_id ?? ""); + if (comparison === undefined || result === undefined) + throw new Error("missing completed fixture Evidence"); + const comparisonResult = compareArtifacts( + forgedLeftPages, + rightPages, + 0, + run.options.change_limit, + ); + const forgedComparison = recreateEvidence(comparison, { + parameters: { + left_evidence_ids: forgedLeftPages.map(({ evidence_id: id }) => id), + right_evidence_ids: rightPages.map(({ evidence_id: id }) => id), + offset: 0, + limit: run.options.change_limit, + }, + result: comparisonResult, + evidenceLinks: [ + ...forgedLeftPages.map(({ evidence_id: id }) => id), + ...rightPages.map(({ evidence_id: id }) => id), + ], + }); + const resultLimit = Math.min(run.options.change_limit, 100); + const changed = findChangedBehavior([forgedComparison], 0, resultLimit); + const summary = investigationRunSummarySchema.parse({ + schema_version: 1, + workspace_id: completed.value.workspace.workspace_id, + run_id: run.run_id, + left_manifest_id: run.left.manifest_id, + right_manifest_id: run.right.manifest_id, + inventory_evidence_count: forgedLeftPages.length + rightPages.length, + comparison_evidence_id: forgedComparison.evidence_id, + limitations: run.limitations, + }); + const forgedResult = recreateEvidence(result, { + parameters: { + workspace_id: completed.value.workspace.workspace_id, + run_id: run.run_id, + comparison_evidence_ids: [forgedComparison.evidence_id], + offset: 0, + limit: resultLimit, + }, + result: { ...changed, investigation_run: summary }, + evidenceLinks: changed.evidence_links, + }); + const forgedRun = investigationRunSchema.parse({ + ...run, + left_inventory_evidence_ids: forgedLeftPages.map( + ({ evidence_id: id }) => id, + ), + comparison_evidence_id: forgedComparison.evidence_id, + result_evidence_id: forgedResult.evidence_id, + }); + const forgedWorkspace = createInvestigationWorkspace( + input.workspace_name, + createEvidenceBundle([ + ...completed.value.workspace.bundle.records, + forgedFirstPage, + forgedComparison, + forgedResult, + ]), + [forgedRun], + ); + const forgedPath = join(directory, "forged-controls.json"); + expect( + await writeInvestigationWorkspace( + forgedWorkspace, + forgedPath, + null, + policy(directory), + ), + ).toMatchObject({ ok: true }); + await expect( + runCrossVersionInvestigation( + { + ...input, + workspace_path: forgedPath, + replay_run_id: run.run_id, + }, + policy(directory), + { inputRoots: [], authorizeInputRead }, + ), + ).resolves.toMatchObject({ + ok: false, + error: { + _tag: "InvestigationWorkspaceError", + reason: "revision-conflict", + }, + }); + expect(authorizeInputRead).not.toHaveBeenCalled(); }); it("enforces shared JSON depth limits while reading workspaces", async () => {