Files
reverse-engineering-anything/tests/boundary/firmware/firmwareIntegration.test.ts
T
morluto 1f24b3fa57 feat(evidence)!: name the Evidence subject by its target identity (format revision 2) (#11416)
* feat(evidence)!: name the Evidence subject by its target identity (format revision 2)

The Evidence subject carries subject.identity (kind, sha256, format,
architecture, selection) and the Evidence ID commits to it, so raw/DOL
entry points and ESP flash offsets of the same bytes are different
subjects. Every record declares evidence_format: 2; revision 1 bundles,
standalone records and snapshots are refused with the recreate guidance.
binaryTargetIdentity is the one projection of an open target, used by
Evidence and snapshot comparison.

BREAKING CHANGE: Evidence format revision 2. Evidence IDs commit to the
subject's target identity (subject.identity, including the raw/DOL entry
point or ESP flash offsets), and every record declares evidence_format: 2.
Evidence bundles, records and analysis snapshots from earlier releases are
refused; recreate the observations with this release.

* refactor(analysis-view): read parent subjects through the Evidence identity accessors (#11423)

Analysis views compare their parent's file identity (evidenceFileTarget)
and take path and digest from evidenceSubjectTarget instead of reading
the subject's digest, format and architecture members, which duplicate
subject.identity until they are removed. A counted guard
(evidence-subject-declaration-read) keeps new direct reads out and
records the 20 remaining files.

* fix(evidence): carry format refusals through Evidence readers and bind target selections

- safeParseEvidence/safeParseImmutableEvidence return the format refusal as
  an expected failure; every consumer maps it to the one integrity refusal
  with the recreate guidance. An unmarked object is an earlier record only
  when it has every member records have always carried.
- ArtifactProvider inventory roots and provider subjects that name the
  target's own bytes carry the target's selection (ESP flash offsets).
- Snapshot rows bind only Evidence whose identity, selection included,
  matches the snapshot target.
- Entry-point selections use the raw-binary entry point form.

* fix(evidence): refuse unauthenticated or earlier-format records instead of reading them as absent

- recognizeEvidence (evidenceValidation) replaces tryParseEvidence: input
  without an evidence_id is not Evidence (ok(undefined)); a record that names
  one but fails authentication or is from another format revision is an
  EvidenceIntegrityError carrying the recreate guidance, never undefined.
- Direct analysis retains that refusal as a secondary failure instead of
  silently skipping source capture; cleanup projection, source-closure
  annotation and the flutter, debugger and release-exposure recoveries use
  the same owner (the last three previously read the unauthenticated schema).
- A desktop capture.json from an earlier format is refused with the recreate
  guidance instead of "not desktop application capture Evidence".

* fix(evidence): advertise revision 2 ledger examples and rank delivery omissions by serialized size

- The obligation ledger and reconstruction coverage examples named
  evidence_format 1, which revision 2 refuses; they now use
  EVIDENCE_FORMAT_REVISION.
- MCP delivery chose which unretained recovery to omit by a parsed-storage
  lower bound clipped at the response budget. Once revision 2 subjects made
  the source record's estimate cross the budget, both candidates tied at the
  clip and the smaller source was omitted ahead of the larger completed
  observation. Candidates are now ranked by serialized size, which is what
  the budget counts.

* refactor(native): compare function snapshots by their Evidence target identity (#11450)

Function snapshots carry the subject's file target identity instead of the
duplicated subject declaration. Call paths and function comparisons compare
targets through sameTargetIdentity, so the same bytes opened at a different
raw entry point or ESP flash offset no longer count as one coordinate space:
build_call_path refuses the mix, and compare_functions no longer matches
entries by address across selections. Structural candidates and native
function matching read the architecture and digest through
evidenceFileTarget.

Refs #11417

* refactor(artifacts): compare artifact and WASM summaries by their Evidence target identity (#11461)

Artifact comparison authenticates a summary against its retained inventory
through sameTargetIdentity and rebinds the alias with the whole target,
selection included, so a flash-selected inventory summary taken under an
alias path is no longer refused. WASM comparison, the analysis view
artifact projection and the inventory root check read the digest, format
and display path through the Evidence identity accessors.

Refs #11417

* refactor(evidence): read record subjects through the identity accessors in JS, managed, ledger and bundle readers (#11470)

JavaScript application graphs, runtime reconciliation, managed inspection
and native verification sources, the record summary filter, the ledger's
residual-unknown target and bundle scope selection read the subject digest,
format and display path through evidenceFileTarget and
evidenceSubjectDisplay. No comparison changes: each reader matches one
record's facts against its own payload or a caller filter.

Refs #11417

* refactor(evidence)!: name the Evidence subject only by its target identity (#11497)

* refactor(evidence)!: name the Evidence subject only by its target identity

The subject keeps `identity` and the display facts `name` and `local_path`.
The duplicated `digest`, `format` and `architecture` members are removed; the
format-owned declaration now validates the identity itself. Readers the type
checker found move to the owner accessors; untyped reads in verify scripts,
the efficiency harness and test matchers move to `subject.identity`. The
`evidence-subject-declaration-read` text guard and its baseline are deleted.

Fixes #11473
Refs #11417

BREAKING CHANGE: the Evidence subject no longer has `digest`, `format` or
`architecture`; read `subject.identity.sha256`, `subject.identity.format` and
`subject.identity.architecture`. A record that still carries them is refused.

* test(evidence): give the Flutter verifier fixture the identity subject and check the DOL declaration without the ID

* test(evidence): drop the removed subject members from the declaration matrix and tamper fixtures

* feat(evidence)!: give every Evidence record an explicit subject identity (#11536)

* feat(evidence)!: give every Evidence record an explicit subject identity

A producer that cannot name a file passes an `unknown` target identity
with a closed reason (derived-from-linked-evidence, not-observed,
unsupported-subject) instead of no subject. The subject is non-null;
unknown subjects have no display facts. The generic missing-subject
limitation and `subjectUnavailableReason` are removed; producer-specific
explanations are ordinary limitations. Comparison identity requires the
derived unknown, and null-subject consumers ask the owner accessors.

Refs #11417

BREAKING CHANGE: every Evidence record has a subject. A record without a
file target names `subject.identity` `{ "kind": "unknown", "reason": … }`
with `name` and `local_path` null, instead of `subject: null` and the
limitation "Artifact identity is unavailable for this observation.".
Their Evidence IDs change.

* fix(evidence): name explicit unknown subjects in JavaScript callers and keep a managed graph's file subject

The verification scripts are not type-checked, so five createEvidence
callers still passed undefined (or a missing first target) after the subject
became required. They now pass unknownTarget("not-observed").

A managed application graph chose the first present input subject. Every
subject is now present, so an unknown artifact subject hid a file subject on
the member or boundary Evidence. The graph now takes the first input subject
that names a file.

* test(evidence): assert the subject kind instead of a presence the type now guarantees

Every Evidence record has a subject after this change, so test guards and
assertions that checked it for null always passed. Each now checks that the
fixture subject names a file, or is removed where a later fileIdentity,
fileSubjectTarget or fileSubjectDisplay call already requires that. Non-null
assertions on Evidence subjects are removed; analysis-view inputs, whose
subject is optional, keep an undefined check.

* test(evidence): expect the Evidence subject format under its identity

Two boundary tests still matched subject.format, which slice 2e moved into
subject.identity. toMatchObject is not type-checked against the Evidence
shape, so they failed only when run (the signature test on macOS).
2026-11-16 06:28:01 +08:00

1042 lines
36 KiB
TypeScript

import { fileIdentity } from "../../fixtures/evidenceSubject.js";
import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server";
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { expect, it as test, vi } from "vitest";
import {
firmwareFixture,
assertFirmwareCleanup,
} from "../../fixtures/firmware/provider.js";
import { firmwareResultSchemas } from "../../../src/domain/firmware/firmwareAnalysis.js";
import { FirmwareAnalysisService } from "../../../src/application/firmware/FirmwareAnalysisService.js";
import { FIRMWARE_LIMITS } from "../../../src/firmware/FirmwareRelease.js";
import { FirmwareProvider } from "../../../src/firmware/FirmwareProvider.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { AnalysisError } from "../../../src/domain/analysisErrorBase.js";
import { toolContract } from "../../../src/contracts/toolContracts.js";
import { createToolResultDelivery } from "../../../src/server/toolResult.js";
import { SafeOutputTree } from "../../../src/resources/SafeOutputTree.js";
import { SafeOutputTreeCreationFailure } from "../../../src/resources/SafeOutputTreeCreationFailure.js";
// Extraction-stage assertions use producer-reported gaps. Unclassified byte
// coverage has its own oracle in the selected-interval workflow below.
const stageGapsWithContinuations = (
result: ReturnType<typeof firmwareResultSchemas.extract_firmware.parse>,
) => {
const instructions = new Map(
result.extraction_continuations.map(({ id, instruction }) => [
id,
instruction,
]),
);
return result.extraction_gaps
.filter(({ kind }) => kind !== "unclassified-range")
.map(({ continuation_id, ...gap }) => {
const instruction = instructions.get(continuation_id);
if (instruction === undefined)
throw new Error("Missing recovery instruction");
return { ...gap, continuation: instruction };
});
};
const directoryCloseFailure = vi.hoisted(() => ({
mode: "off" as "off" | "transient" | "persistent",
attempts: 0,
parent: "",
}));
vi.mock("node:fs/promises", async (importOriginal) => {
const filesystem = await importOriginal<typeof import("node:fs/promises")>();
return {
...filesystem,
open: async (...args: Parameters<typeof filesystem.open>) => {
const handle = await filesystem.open(...args);
if (
directoryCloseFailure.mode !== "off" &&
String(args[0]) === directoryCloseFailure.parent &&
(await handle.stat()).isDirectory()
) {
const close = handle.close.bind(handle);
let attempted = false;
handle.close = async () => {
directoryCloseFailure.attempts += 1;
if (
directoryCloseFailure.mode === "persistent" ||
(directoryCloseFailure.mode === "transient" && !attempted)
) {
attempted = true;
throw new Error("injected published directory close failure");
}
await close();
};
}
return handle;
},
};
});
const delivery = createToolResultDelivery(STDIO_DEFAULT_MAX_BUFFER_SIZE);
const it = test.skipIf(process.platform !== "linux");
it("keeps cleanup-failure state local to the selected factory instance", async () => {
const failed = await firmwareFixture("cleanup-failure");
const healthy = await firmwareFixture();
const failure = await failed.service.execute("inspect_firmware_regions", {
path: failed.path,
});
expect(failure).toMatchObject({
ok: false,
error: { cleanupIncomplete: true },
});
const result = await healthy.service.execute("inspect_firmware_regions", {
path: healthy.path,
});
expect(result.ok).toBe(true);
await assertFirmwareCleanup(healthy.launches);
});
it("binds region observations to exact bytes and preserves provider-reported uncertainty", async () => {
const fixture = await firmwareFixture();
const result = await fixture.service.execute("inspect_firmware_regions", {
path: fixture.path,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.inspect_firmware_regions.parse(
result.value.normalized_result,
);
expect(normalized.engine).toMatchObject({
name: "binwalk",
version: "3.1.0",
executable_path: fixture.commands.binwalk,
});
expect(normalized.regions[0]).toMatchObject({
offset: 2,
reported_size: 4,
size_basis: "provider_reported_validation_unknown",
});
expect(result.value.subject?.local_path).toBe(fixture.path);
expect(fileIdentity(result.value.subject).format).toBe("file");
expect(result.value.raw_result).toMatchObject({
report: [{ Analysis: { file_map: [{ id: "hit" }] } }],
});
expect(fixture.launches[1]?.args).toEqual(
expect.arrayContaining([
`--as=${String(FIRMWARE_LIMITS.addressSpaceBytes)}:${String(FIRMWARE_LIMITS.addressSpaceBytes)}`,
"--threads",
"1",
]),
);
await assertFirmwareCleanup(fixture.launches);
});
it.each(["wrong-input", "bounds"])(
"rejects a mismatched or out-of-bounds inspection: %s",
async (mode) => {
const fixture = await firmwareFixture(mode);
const result = await fixture.service.execute("inspect_firmware_regions", {
path: fixture.path,
});
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisOutputError" },
});
await assertFirmwareCleanup(fixture.launches);
},
);
it("extracts a selected interval with verified file identity and parent-byte lineage", async () => {
const fixture = await firmwareFixture();
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
range: { offset: 2, length: 4 },
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(normalized.engine).toMatchObject({
name: "unblob",
version: "26.6.4",
executable_path: fixture.commands.unblob,
});
expect(normalized.selection).toMatchObject({ offset: 2, length: 4 });
const unclassified = normalized.extraction_gaps.filter(
({ kind }) => kind === "unclassified-range",
);
expect(unclassified).toEqual([
expect.objectContaining({
input_path: "$output/rootfs/config",
range: { offset: 0, length: 14 },
root_file_range: null,
}),
]);
expect(
normalized.extraction_continuations.find(
({ id }) => id === unclassified[0]?.continuation_id,
)?.instruction,
).toContain("published child");
expect(normalized.files[0]).toMatchObject({
relative_path: "rootfs/config",
size: 14,
original_file_range: null,
runtime_address: null,
});
expect(normalized.chunks[0]?.root_file_range).toEqual({
offset: 2,
length: 4,
});
expect(normalized.derivations[0]).toMatchObject({
parent_path: "$input",
child_path: "$output/rootfs/config",
handler: "gzip",
parent_file_range: { offset: 0, length: 4 },
});
expect(await readFile(normalized.files[0]?.path ?? "", "utf8")).toBe(
"firmware=true\n",
);
expect(fixture.launches[1]?.args).toEqual(
expect.arrayContaining(["--process-num", "1", "--randomness-depth", "0"]),
);
expect(fixture.launches[1]?.args).not.toContain("--no-sandbox");
await assertFirmwareCleanup(fixture.launches);
});
it("retains a setup-failed publication tree for provider cleanup retry", async () => {
const fixture = await firmwareFixture();
const createTree = SafeOutputTree.create.bind(SafeOutputTree);
let cleanupAttempts = 0;
let retained: AnalysisError | undefined;
const create = vi
.spyOn(SafeOutputTree, "create")
.mockImplementationOnce(async (outputRoot, platform) => {
const tree = await createTree(outputRoot, platform);
const rollbackTree = tree.rollback.bind(tree);
vi.spyOn(tree, "rollback").mockImplementation(async () => {
cleanupAttempts += 1;
if (cleanupAttempts === 1)
throw new Error("injected first cleanup failure");
return rollbackTree();
});
throw new SafeOutputTreeCreationFailure(
new Error("injected post-acquisition setup failure"),
tree,
);
});
try {
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
range: { offset: 2, length: 4 },
});
expect(result).toMatchObject({
ok: false,
error: {
cleanupIncomplete: true,
cleanupResources: expect.arrayContaining([fixture.output]),
},
});
if (result.ok) throw new Error("Expected retained publication cleanup");
retained = result.error;
expect(projectAnalysisError(retained).code).toBe("cleanup_incomplete");
expect(JSON.stringify(projectAnalysisError(retained))).toContain(
"injected post-acquisition setup failure",
);
await expect(access(fixture.output)).resolves.toBeUndefined();
} finally {
create.mockRestore();
}
await fixture.close();
expect(cleanupAttempts).toBe(2);
expect(retained?.cleanupIncomplete).toBe(false);
await expect(access(fixture.output)).rejects.toMatchObject({
code: "ENOENT",
});
});
it("returns firmware output after a transient published-directory close failure", async () => {
const fixture = await firmwareFixture();
const closeFailure = injectPublishedDirectoryCloseFailure(false);
try {
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
range: { offset: 2, length: 4 },
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(normalized.output_directory).toBe(fixture.output);
expect(await readFile(normalized.files[0]?.path ?? "", "utf8")).toBe(
"firmware=true\n",
);
expect(closeFailure.attempts()).toBeGreaterThanOrEqual(2);
} finally {
closeFailure.restore();
}
});
it("retains the complete firmware result when published output cleanup stays uncertain", async () => {
const fixture = await firmwareFixture();
let retained: AnalysisError | undefined;
const closeFailure = injectPublishedDirectoryCloseFailure(true);
try {
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
range: { offset: 2, length: 4 },
});
expect(result).toMatchObject({
ok: false,
error: {
cleanupIncomplete: true,
partialObservation: {
kind: "firmware",
result: {
output_directory: fixture.output,
files: [
expect.objectContaining({
relative_path: "rootfs/config",
size: 14,
}),
],
},
},
},
});
if (result.ok) throw new Error("Expected unresolved descriptor cleanup");
expect(result.error).toBeInstanceOf(AnalysisError);
retained = result.error;
expect(projectAnalysisError(result.error)).toMatchObject({
code: "cleanup_incomplete",
details: {
partial_observation: {
kind: "firmware",
result: { output_directory: fixture.output },
},
},
});
expect(await readFile(`${fixture.output}/rootfs/config`, "utf8")).toBe(
"firmware=true\n",
);
expect(closeFailure.attempts()).toBeGreaterThanOrEqual(2);
} finally {
closeFailure.restore();
}
await fixture.close();
expect(retained?.cleanupIncomplete).toBe(false);
expect(await readFile(`${fixture.output}/rootfs/config`, "utf8")).toBe(
"firmware=true\n",
);
});
it("shares recovery prose across distinct unknown chunk identities", async () => {
const fixture = await firmwareFixture("many-unknown");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
const gaps = normalized.extraction_gaps.filter(
({ kind }) => kind === "unknown-chunk-handler",
);
expect(gaps).toHaveLength(1000);
expect(new Set(gaps.map(({ provider_id }) => provider_id)).size).toBe(1000);
const ids = new Set(gaps.map(({ continuation_id }) => continuation_id));
expect(ids.size).toBe(1);
const instruction = normalized.extraction_continuations.find(({ id }) =>
ids.has(id),
);
expect(instruction?.instruction).toContain("inspect_firmware_regions");
});
it("retains a task dependency failure when the producer names no executable", async () => {
const fixture = await firmwareFixture("unnamed-dependency");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(stageGapsWithContinuations(normalized)).toEqual([
{
kind: "missing-extractor-dependency",
input_path: "$input",
relative_path: null,
handler: null,
provider_id: null,
range: null,
root_file_range: null,
dependencies: [],
continuation: expect.stringContaining("identity is unavailable"),
},
]);
});
it("preserves an unnamed dependency failure on its owning chunk", async () => {
const fixture = await firmwareFixture("unnamed-chunk-dependency");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(stageGapsWithContinuations(normalized)).toEqual([
{
kind: "missing-extractor-dependency",
input_path: "$input",
relative_path: null,
handler: "gzip",
provider_id: "gzip-id",
range: { offset: 0, length: 18 },
root_file_range: { offset: 0, length: 18 },
dependencies: [],
continuation: expect.stringContaining("identity is unavailable"),
},
]);
});
it("routes a nested unknown chunk through its published child", async () => {
const fixture = await firmwareFixture("nested-unknown");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(stageGapsWithContinuations(normalized)).toEqual(
expect.arrayContaining([
expect.objectContaining({
kind: "unknown-chunk-handler",
input_path: "$output/rootfs/config",
provider_id: "unknown-id",
range: { offset: 0, length: 14 },
root_file_range: null,
continuation: expect.stringContaining(
"published child named by this gap's input_path",
),
}),
]),
);
expect(stageGapsWithContinuations(normalized)[0]?.continuation).toContain(
"only if Binwalk is configured with REA_BINWALK_COMMAND",
);
expect(stageGapsWithContinuations(normalized)[0]?.continuation).toContain(
"unavailable without configured Binwalk",
);
});
it("retains every eligible child in a maximum-depth continuation", async () => {
const fixture = await firmwareFixture("depth-reentry");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
max_depth: 10,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
const gap = stageGapsWithContinuations(normalized).find(
({ kind, input_path }) =>
kind === "depth-limited" && input_path === "$output/rootfs/config",
);
expect(gap?.continuation).toContain(
JSON.stringify(`${fixture.output}/rootfs/config/inner.gz`),
);
expect(gap?.continuation).toContain(
JSON.stringify(`${fixture.output}/rootfs/config/second.gz`),
);
});
it("withholds the nested rerun advice when the published child exceeds the input limit", async () => {
const fixture = await firmwareFixture("nested-unknown-oversized");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(stageGapsWithContinuations(normalized)).toEqual([
{
kind: "unknown-chunk-handler",
input_path: "$output/rootfs/config",
relative_path: null,
handler: null,
provider_id: "unknown-id",
range: { offset: 0, length: 14 },
root_file_range: null,
dependencies: [],
continuation: expect.stringContaining(
"cannot accept it as a fresh input",
),
},
]);
});
it.each([
["depth", 10, "rerun extract_firmware"],
["depth-orphan", 10, "cannot be restarted"],
] as const)(
"bounds the maximum-depth continuation by a usable published child (%s)",
async (mode, maxDepth, expected) => {
const fixture = await firmwareFixture(mode);
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
max_depth: maxDepth,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(normalized.depth_limited_paths.length).toBeGreaterThan(0);
expect(
stageGapsWithContinuations(normalized).filter(
(gap) => gap.kind === "depth-limited",
),
).toEqual(
expect.arrayContaining([
expect.objectContaining({
kind: "depth-limited",
continuation: expect.stringContaining(expected),
}),
]),
);
},
);
it("reports a failed multi-file stage as an extractor-failure gap", async () => {
const fixture = await firmwareFixture("multi-file-failure");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(stageGapsWithContinuations(normalized)).toEqual(
expect.arrayContaining([
expect.objectContaining({
kind: "extractor-failure",
input_path: "$input",
handler: "tar",
provider_id: "mf-id",
range: null,
root_file_range: null,
}),
]),
);
});
it.each(["task-error", "task-warning"])(
"preserves standalone diagnostic recovery without inventing stage ownership: %s",
async (mode) => {
const fixture = await firmwareFixture(mode);
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
const failures = stageGapsWithContinuations(normalized).filter(
(gap) => gap.kind === "extractor-failure",
);
expect(normalized.diagnostics).toContainEqual(
expect.objectContaining({
input_path: "$input",
report: expect.objectContaining({
__typename__:
mode === "task-error"
? "UnknownError"
: "ExtractCommandFailedReport",
}),
}),
);
if (mode === "task-error") {
expect(failures).toEqual([
expect.objectContaining({
input_path: "$input",
handler: null,
provider_id: null,
range: null,
root_file_range: null,
dependencies: [],
continuation: expect.stringContaining("diagnostics"),
}),
]);
} else expect(failures).toEqual([]);
await assertFirmwareCleanup(fixture.launches);
},
);
it("retains independent failures beside dependencies at each extraction owner", async () => {
const fixture = await firmwareFixture("mixed-failures");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
const gaps = stageGapsWithContinuations(normalized);
expect(gaps).toHaveLength(4);
for (const provider_id of ["gzip-id", "mixed-id"]) {
const owned = gaps.filter((gap) => gap.provider_id === provider_id);
expect(owned.map((gap) => gap.kind).sort()).toEqual([
"extractor-failure",
"missing-extractor-dependency",
]);
for (const gap of owned) {
expect(gap.input_path).toBe("$input");
expect(gap.range).toEqual(
provider_id === "mixed-id" ? null : normalized.chunks[0]?.range,
);
}
}
expect(normalized.chunks[0]?.extraction_failed).toBe(true);
await assertFirmwareCleanup(fixture.launches);
});
it("carries the owning multi-file stage on a task-level dependency gap", async () => {
const fixture = await firmwareFixture("multi-file-dependency");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(stageGapsWithContinuations(normalized)).toEqual([
{
kind: "missing-extractor-dependency",
input_path: "$input",
relative_path: null,
handler: "squashfs",
provider_id: "sq-id",
range: null,
root_file_range: null,
dependencies: ["sasquatch"],
continuation: expect.stringContaining("installs nothing"),
},
]);
expect(normalized.diagnostics).toEqual([
expect.objectContaining({ stage: "squashfs" }),
]);
});
const injectPublishedDirectoryCloseFailure = (persistent: boolean) => {
directoryCloseFailure.attempts = 0;
const commit = SafeOutputTree.prototype.commit;
const spy = vi
.spyOn(SafeOutputTree.prototype, "commit")
.mockImplementation(async function (this: SafeOutputTree) {
directoryCloseFailure.mode = persistent ? "persistent" : "transient";
// Only the parent descriptor closes after the publication cutoff. A
// child/root close failure aborts sync before output becomes published.
directoryCloseFailure.parent = dirname(this.outputRoot);
await commit.call(this);
});
return {
attempts: () => directoryCloseFailure.attempts,
restore: () => {
directoryCloseFailure.mode = "off";
directoryCloseFailure.parent = "";
spy.mockRestore();
},
};
};
it.each(["depth", "dependency", "link"])(
"reports partial extraction for %s without losing inline results",
async (mode) => {
const fixture = await firmwareFixture(mode);
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!result.ok) throw result.error;
const normalized = firmwareResultSchemas.extract_firmware.parse(
result.value.normalized_result,
);
expect(normalized.coverage).toBe("partial");
expect(normalized.files).toHaveLength(1);
if (mode === "depth")
expect(normalized.depth_limited_paths).toEqual(["$output/rootfs/config"]);
if (mode === "depth")
expect(stageGapsWithContinuations(normalized)).toEqual([
{
kind: "depth-limited",
input_path: "$output/rootfs/config",
relative_path: null,
handler: null,
provider_id: null,
range: null,
root_file_range: null,
dependencies: [],
continuation: expect.stringMatching(
/max_depth.*fresh output_directory/u,
),
},
]);
if (mode === "dependency")
expect(normalized.diagnostics).toEqual([
{
input_path: "$input",
stage: "gzip",
missing_dependencies: ["sasquatch"],
report: {
__typename__: "ExtractorDependencyNotFoundReport",
severity: "ERROR",
dependencies: ["sasquatch"],
},
},
]);
if (mode === "dependency")
expect(stageGapsWithContinuations(normalized)).toEqual([
{
kind: "missing-extractor-dependency",
input_path: "$input",
relative_path: null,
handler: "gzip",
provider_id: "gzip-id",
range: { offset: 0, length: 18 },
root_file_range: { offset: 0, length: 18 },
dependencies: ["sasquatch"],
continuation: expect.stringContaining("installs nothing"),
},
]);
const linkLimitation = expect.stringContaining(
"Unblob rewrites absolute targets relative to the extraction root",
);
if (mode === "link")
expect(result.value.limitations).toContainEqual(linkLimitation);
else expect(result.value.limitations).not.toContainEqual(linkLimitation);
if (mode === "link") {
expect(normalized.unpublished_entries).toEqual([
{
relative_path: "escape",
kind: "symlink",
link_target: "/etc/passwd",
link_target_hex: Buffer.from("/etc/passwd", "utf8").toString("hex"),
},
]);
expect(stageGapsWithContinuations(normalized)).toEqual([
{
kind: "unpublished-entry",
input_path: "$output/escape",
relative_path: "escape",
handler: null,
provider_id: null,
range: null,
root_file_range: null,
dependencies: [],
continuation: expect.stringContaining("metadata"),
},
]);
await expect(access(`${fixture.output}/escape`)).rejects.toMatchObject({
code: "ENOENT",
});
}
await assertFirmwareCleanup(fixture.launches);
},
);
it("distinguishes a real output file named $input from the selected original", async () => {
const fixture = await firmwareFixture("reserved-name");
const outcome = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (!outcome.ok) throw outcome.error;
const result = firmwareResultSchemas.extract_firmware.parse(
outcome.value.normalized_result,
);
expect(result.files[0]?.relative_path).toBe("$input");
expect(result.derivations[0]).toMatchObject({
parent_path: "$input",
child_path: "$output/$input",
});
expect(await readFile(`${fixture.output}/$input`, "utf8")).toBe(
"firmware=true\n",
);
await assertFirmwareCleanup(fixture.launches);
});
it.each(["hash", "malformed", "budget", "missing-file"])(
"rejects invalid or over-budget extraction and rolls back output: %s",
async (mode) => {
const fixture = await firmwareFixture(mode);
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
...(mode === "budget" ? { max_output_bytes: 1000 } : {}),
});
expect(result.ok).toBe(false);
await expect(access(fixture.output)).rejects.toMatchObject({
code: "ENOENT",
});
await assertFirmwareCleanup(fixture.launches);
},
);
it("preserves existing output and rejects invalid intervals before invoking extraction", async () => {
const fixture = await firmwareFixture();
await mkdir(fixture.output);
await writeFile(`${fixture.output}/existing`, "keep");
const existing = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
expect(existing.ok).toBe(false);
expect(await readFile(`${fixture.output}/existing`, "utf8")).toBe("keep");
const previous = fixture.launches.length;
const invalid = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: `${fixture.output}-invalid-range`,
range: { offset: 100, length: 1 },
});
expect(invalid).toMatchObject({
ok: false,
error: { _tag: "AnalysisInputError" },
});
if (invalid.ok) throw new Error("Expected out-of-file-range rejection");
expect(projectAnalysisError(invalid.error)).toMatchObject({
details: { issues: [{ path: ["range"], reason: "out_of_range" }] },
});
expect(fixture.launches).toHaveLength(previous);
await assertFirmwareCleanup(fixture.launches);
});
it("retains startup stderr when Unblob exits 1 without a report", async () => {
const fixture = await firmwareFixture("startup");
const result = await fixture.service.execute("extract_firmware", {
path: fixture.path,
output_directory: fixture.output,
});
if (result.ok) throw new Error("Expected startup failure");
const details = {
diagnostics: {
stderr: expect.stringContaining("Landlock sandbox is not available"),
exit_code: 1,
},
};
expect(projectAnalysisError(result.error)).toMatchObject({ details });
expect(
delivery.toCallToolResult(result, toolContract("extract_firmware"))
.structuredContent,
).toMatchObject({ error: { details } });
expect(result).toMatchObject({
ok: false,
error: {
_tag: "ProviderAdapterError",
diagnostics: {
exit_code: 1,
stderr: expect.stringContaining("Landlock sandbox is not available"),
report_state: "missing",
},
},
});
await expect(access(fixture.output)).rejects.toMatchObject({
code: "ENOENT",
});
await assertFirmwareCleanup(fixture.launches);
});
it.each([
{ operation: "inspect_firmware_regions", engine: "binwalk" },
{ operation: "extract_firmware", engine: "unblob" },
] as const)(
"keeps $engine's missing report after exit 0 distinct from an empty result",
async ({ operation, engine }) => {
const fixture = await firmwareFixture("missing-report");
const result = await fixture.service.execute(operation, {
path: fixture.path,
...(operation === "extract_firmware"
? { output_directory: fixture.output }
: {}),
});
if (result.ok) throw new Error("Expected missing-report failure");
const error = {
code: "unreadable_output",
retryable: false,
message: expect.stringContaining("not an empty or complete result"),
details: {
reason: `${engine} exited 0 without a usable report: Report was not created`,
diagnostics: {
report_state: "missing",
report_path: expect.stringMatching(/report\.json$/),
command: expect.any(String),
args: expect.any(Array),
exit_code: 0,
signal: null,
stderr: "Producer forgot its report\n",
},
},
remediation: {
action: expect.stringContaining("did not retry or substitute"),
},
};
expect(projectAnalysisError(result.error)).toMatchObject(error);
expect(projectAnalysisError(result.error).message).not.toContain(
"rea doctor",
);
expect(
delivery.toCallToolResult(result, toolContract(operation))
.structuredContent,
).toMatchObject({ error });
await assertFirmwareCleanup(fixture.launches);
},
);
it("cancels an owned active process and permits the next queued operation", async () => {
const fixture = await firmwareFixture("stall");
const controller = new AbortController();
const pending = fixture.service.execute(
"inspect_firmware_regions",
{ path: fixture.path },
{ signal: controller.signal },
);
await expect.poll(() => fixture.launches.length).toBe(2);
controller.abort();
expect(await pending).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError" },
});
const cancelled = await fixture.service.execute(
"inspect_firmware_regions",
{ path: fixture.path },
{ signal: AbortSignal.abort() },
);
expect(cancelled).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError" },
});
expect(fixture.launches).toHaveLength(2);
await assertFirmwareCleanup(fixture.launches);
});
it("cancels a waiting request promptly while keeping later launches behind the active operation", async () => {
const fixture = await firmwareFixture("stall");
const activeController = new AbortController();
const active = fixture.service.execute(
"inspect_firmware_regions",
{ path: fixture.path },
{ signal: activeController.signal },
);
await expect.poll(() => fixture.launches.length).toBe(2);
const queuedController = new AbortController();
const queued = fixture.service.execute(
"inspect_firmware_regions",
{ path: fixture.path },
{ signal: queuedController.signal },
);
queuedController.abort();
expect(await queued).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError" },
});
expect(fixture.launches).toHaveLength(2);
activeController.abort();
expect((await active).ok).toBe(false);
await assertFirmwareCleanup(fixture.launches);
});
it("retains an uncertain worker's workspace and blocks later launches", async () => {
const fixture = await firmwareFixture("cleanup-failure");
const first = await fixture.service.execute("inspect_firmware_regions", {
path: fixture.path,
});
expect(first).toMatchObject({
ok: false,
error: { cleanupIncomplete: true },
});
const workspace = fixture.launches[0]?.cwd;
expect(workspace).toBeDefined();
if (workspace === undefined) throw new Error("Expected retained workspace");
await access(workspace);
const originalLaunches = [...fixture.launches];
const next = await fixture.service.execute("inspect_firmware_regions", {
path: fixture.path,
});
expect(next).toMatchObject({ ok: false, error: { cleanupIncomplete: true } });
expect(fixture.launches).toEqual(originalLaunches);
await access(workspace);
fixture.restoreCleanup();
const resumed = await fixture.service.execute("inspect_firmware_regions", {
path: fixture.path,
});
if (!resumed.ok) throw resumed.error;
expect(resumed.value.subject?.local_path).toBe(fixture.path);
await assertFirmwareCleanup(fixture.launches);
});
it("accepts another build on the verified release line and reports it", async () => {
const fixture = await firmwareFixture("normal", "binwalk 3.1.2");
const result = await fixture.service.execute("inspect_firmware_regions", {
path: fixture.path,
});
if (!result.ok) throw result.error;
expect(result.value.provider.version).toBe("3.1.2");
expect(result.value.limitations.join("\n")).toContain(
"verified against 3.1.0",
);
expect(result.value.normalized_result).toMatchObject({
engine: { version: "3.1.2" },
});
await assertFirmwareCleanup(fixture.launches);
});
it("names the accepted release line when the banner is another line", async () => {
const fixture = await firmwareFixture("normal", "binwalk 3.2.0");
const result = await fixture.service.execute("inspect_firmware_regions", {
path: fixture.path,
});
expect(result).toMatchObject({
ok: false,
error: {
_tag: "AnalysisCapabilityUnavailableError",
reason: expect.stringContaining("Binwalk 3.1.x"),
},
});
await assertFirmwareCleanup(fixture.launches);
});
it("rejects absent or unsupported engines without installing anything", async () => {
const missing = await new FirmwareAnalysisService(
new FirmwareProvider({}),
).execute("inspect_firmware_regions", { path: "/tmp/unused" });
expect(missing).toMatchObject({
ok: false,
error: { _tag: "AnalysisCapabilityUnavailableError" },
});
const fixture = await firmwareFixture("version");
const result = await fixture.service.execute("inspect_firmware_regions", {
path: fixture.path,
});
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisCapabilityUnavailableError" },
});
await assertFirmwareCleanup(fixture.launches);
});