Files
reverse-engineering-anything/tests/acceptance/artifactComparisonEspImage.test.ts
T
morluto 459553b901 fix(evidence)!: require ESP flash_dump and PE unsigned attribute OIDs (#11595)
* fix(evidence)!: require ESP flash_dump and PE unsigned attribute OIDs

Both fields were optional only for Evidence recorded before REA reported
them (6.8.0 and earlier). Every producer writes them: the ESP image reader
and facts builder write `flash_dump` (null unless the file is a flash dump
read from a later address), and the PE certificate parser writes
`unsigned_attribute_oids` (null when the signer has none). They are now
required and nullable, and the inspection hint no longer branches on an
absent `flash_dump`. The ESP acceptance test that read pre-6.9 inventory
Evidence now checks that such Evidence is refused.

A call-path comment called Evidence without relationship coverage "legacy";
providers that report no coverage still produce it, so the comment now says
so.

BREAKING CHANGE: inline inventory Evidence for an ESP image root without
`format_metadata.flash_dump`, and PE certificate Evidence whose signers lack
`unsigned_attribute_oids`, are refused as invalid input. Recreate the
observation with the current REA.

Refs #10510

* test(certificates): refuse a signer without unsigned_attribute_oids; update the guide
2026-11-16 07:53:42 +08:00

123 lines
5.1 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { ESP32C3_APP_SEGMENTS, espImage } from "../fixtures/espImage.js";
import { artifactInventorySummaryViewResultSchema } from "../../src/domain/analysisView/analysisViewResult.js";
import { artifactComparisonResultSchema } from "../../src/domain/artifactComparison.js";
import { artifactInspectionResultSchema } from "../../src/domain/artifactInspection.js";
import { artifactInventoryResultSchema } from "../../src/domain/artifactGraph.js";
import { parseEvidence, type Evidence } from "../../src/domain/evidence.js";
import { historicalInventoryEvidence } from "../fixtures/artifactEvidence.js";
import { connectArtifactComparisonMcp } from "../fixtures/artifactComparisonMcp.js";
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
it("reports an ESP segment whose load address alone changed", async () => {
const root = await createTestTempDirectory("rea-esp-comparison-");
const { inspect: inspectFile, compare } =
await connectArtifactComparisonMcp();
const inspect = async (
name: string,
segments: typeof ESP32C3_APP_SEGMENTS,
): Promise<Evidence> => {
const path = join(root, name);
await writeFile(path, espImage({ segments }));
return inspectFile(path);
};
const segment = (evidence: Evidence, path: string) => {
const occurrence = artifactInventoryResultSchema
.parse(evidence.normalized_result)
.occurrences.find(({ logical_path }) => logical_path === path);
if (occurrence === undefined) throw new Error(`Missing ${path}`);
return occurrence;
};
// The same bytes, with the DRAM segment loading 8 KiB higher.
const moved = ESP32C3_APP_SEGMENTS.map((item, index) =>
index === 1 ? { ...item, address: 0x3fc8_a000 } : item,
);
const left = await inspect("app.bin", ESP32C3_APP_SEGMENTS);
const right = await inspect("moved.bin", moved);
const leftSegment = segment(left, "segment-01.bin");
const rightSegment = segment(right, "segment-01.bin");
expect(rightSegment.artifact_id).toBe(leftSegment.artifact_id);
expect(leftSegment.format_metadata).toMatchObject({ load_addr: 0x3fc8_8000 });
expect(rightSegment.format_metadata).toMatchObject({
load_addr: 0x3fc8_a000,
});
const changed = await compare(left, right);
expect(
changed.changes.filter(({ logical_path }) => logical_path !== "."),
).toEqual([
{
classification: "changed",
logical_path: "segment-01.bin",
dimensions: ["metadata"],
left_occurrence_id: leftSegment.occurrence_id,
right_occurrence_id: rightSegment.occurrence_id,
left_artifact_id: leftSegment.artifact_id,
right_artifact_id: rightSegment.artifact_id,
evidence_links: [left.evidence_id, right.evidence_id],
},
]);
// Equal declarations leave every segment unchanged.
const same = await compare(
left,
await inspect("copy.bin", ESP32C3_APP_SEGMENTS),
);
expect(same.changes).toEqual([]);
});
/** An ESP image inventory without `flash_dump`, as releases before 6.9 wrote it. */
const withoutFlashDump = (source: Evidence): Evidence => {
return historicalInventoryEvidence(
source,
(occurrence) => {
if (occurrence.format_metadata?.kind !== "esp-image") return occurrence;
const { flash_dump: _dump, ...historical } = occurrence.format_metadata;
// The current type requires the field this record omits.
return {
...occurrence,
format_metadata: historical as typeof occurrence.format_metadata,
};
},
"observed",
);
};
// Every ESP image root records `flash_dump` (null when the file is not a
// flash dump read from a later address), so an inventory without it is
// refused rather than read as one with unknown origin.
it("refuses ESP image inventory Evidence without flash_dump", async () => {
const root = await createTestTempDirectory("rea-esp-historical-");
const path = join(root, "app.bin");
await writeFile(path, espImage({ segments: ESP32C3_APP_SEGMENTS }));
const { call } = await connectLocalToolsMcp();
const opened = await call("open_binary", { path });
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
const inspected = await call("inspect_artifact", {});
expect(inspected.isError, JSON.stringify(inspected)).not.toBe(true);
const current = parseEvidence(
artifactInspectionResultSchema
.parse(parseEvidence(inspected.structuredContent).normalized_result)
.substeps.find(({ operation }) => operation === "inventory_artifact")
?.evidence,
);
const rootMetadata = artifactInventoryResultSchema
.parse(current.normalized_result)
.occurrences.find(
({ logical_path }) => logical_path === ".",
)?.format_metadata;
expect(rootMetadata).toMatchObject({ kind: "esp-image", flash_dump: null });
const compared = await call("compare_artifacts", {
left: withoutFlashDump(current),
right: current,
});
expect(compared.isError).toBe(true);
expect(JSON.stringify(compared.content)).toContain("flash_dump");
});