Files
reverse-engineering-anything/tests/acceptance/devicetreeCli.test.ts
morluto 89c1dd3c32 fix(errors): refuse malformed target bytes as a target, not invalid input (#14195)
* fix(errors): refuse malformed target bytes as a target, not invalid input

parseFailureAnalysisError is the shared owner that turns a byte-format
parser's ParseFailure into a public refusal. It mapped malformed and
truncated bytes to an AnalysisInputError on the path field. The envelope
then reported invalid_request, with inspect_input_contract recovery and
"correct the listed fields". The arguments were valid; the target bytes
were the problem. Readers that wrap the ParseFailureError in an
ArtifactReaderFailure, and ArtifactOperationError reason=malformed, were
classified the same way.

Add AnalysisMalformedTargetError. It keeps the parser's kind, format,
detail, offset and field, and projects as unsupported_target with
details.format_failure. Its recovery is to choose another operation or a
well-formed copy of the target. Route both parser entry points through it
and reclassify ArtifactOperationError reason=malformed. Argument and
selection errors (missing files, directories, symlinks, JSON arguments)
stay invalid_request.

Fixes #14170

* docs(maven): describe duplicate variant names as malformed metadata

Duplicate Gradle variant names go through malformedInput, which now
reports unsupported_target with details.format_failure: malformed.
Point the earlier statement at that section instead of the old
invalid_format code.

* test(errors): expect malformed-target refusals from CLI byte-format readers

Truncated IPS, ELF, PE and shortcut bytes now project as unsupported_target with format_failure=malformed. Update the CLI acceptance cases that still expected invalid_request.

* fix(errors): keep a cancelled read a cancellation whatever its abort reason

projectOwnedArtifactInputFailure read the reader failure's cause without
checking the reader's own reason. A cancelled read carries the caller's
opaque abort reason as that cause, so aborting with a ParseFailureError
turned the cancellation into a non-retryable malformed-target refusal
before the shared cancellation projection ran. Only a reader whose own
reason is malformed or truncated now yields a malformed target.

The ELF dependency checkpoint cancellation test now also aborts with a
parser failure as the reason, through the public provider read.

* test(pe): expect malformed-target refusals from the PE filesystem boundaries

Short non-PE bytes now refuse the target rather than the request field.
The missing-file, directory, symlink and child-path controls keep their
caller-input expectations.

* fix(elf): refuse malformed ELF note bytes as a target, not invalid input

inspect_elf_notes shared this PR's root cause: its parser reported
structural violations as a reader "format" failure, which the shared
stable-artifact projection treats as a request-field error, so truncated
or non-ELF bytes still returned invalid_request. The parser now raises
malformed or truncated ParseFailureErrors, which the shared projection
reports as a malformed target with format_failure and format facts.

Consumers that interpreted the old reader reason now use the parser's
facts: debug-link build-id observation keys on ParseFailureError kinds,
and binary hardening drops its rewrap of note-parser format failures
into BinaryFormatFailure, which the parser's own failure supersedes.

* test(errors): own the byte readers' malformed-target projection in one CLI table

The PR repeated the same malformed -> unsupported_target mapping in a
dedicated CLI test per command and again in each provider's filesystem
boundary test. The stable-artifact parser-failure CLI table now owns it:
every byte reader it covers (now including ELF dependencies, debug link,
unwind ranges and native initializers) refuses a truncated ELF header as
a malformed target with operation, path, format_failure, retained file
identity and no argument recovery.

Removed as redundant with that table: the truncated or non-ELF/non-PE
CLI tests for ELF debug link, dependencies, unwind ranges and notes,
native initializers, and PE imports and certificates; and the
malformed-file blocks in thirteen filesystem boundary tests, which keep
their directory, symlink, missing-file and child-path controls. Format
specific refusals (a truncated IPS body, a wrong shortcut CLSID, DWARF
units) keep their own tests, as do the owner tests in
parseFailureAnalysisError and ArtifactOperationFailure.

* test(errors): expect malformed target bytes as unsupported_target in acceptance tests

The first full acceptance run of this PR found five expectations still
asserting invalid_request for target-byte failures: the timing test's
truncated ELF, a truncated DTB, duplicate Rust dependency JSON, and PE32
and PE64 load-config images without an MZ header. Assert the
unsupported_target code, with format_failure malformed where the
non-MZ PE path matches the already-updated PE imports test.
2026-11-24 00:44:40 +00:00

483 lines
16 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { z } from "zod";
import { fitFixture } from "../../src/artifacts/fit/FitImage.fixture.js";
import { toolContract } from "../../src/contracts/toolContracts.js";
import { createEvidence } from "../../src/domain/evidence.js";
import type { Devicetree } from "../../src/domain/firmware/devicetree.js";
import {
cells,
devicetreeFixture,
emptyStringsAtReservationBoundaryFixture,
} from "../../src/domain/firmware/devicetree.fixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
import { cliTest } from "../support/cli/cliFixture.js";
const paddingDiagnostic = /FDT structure padding contains/iu;
cliTest(
"retains DTB facts and reports both nonzero padding placements",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-dtb-padding-");
const path = join(root, "board.dtb");
const zeroPadded = devicetreeFixture({
name: "",
properties: { model: Buffer.from([0x78, 0]) },
children: [{ name: "a" }],
});
const nonzeroPadded = Buffer.from(zeroPadded);
const start = nonzeroPadded.readUInt32BE(8);
// Two-byte model value rounded up to four, then a child named "a".
const propertyPadding = start + 22;
nonzeroPadded[propertyPadding] = 1;
// libfdt's in-place rename from "abc" to "a" leaves the former 'c'.
nonzeroPadded[start + 30] = 0x63;
const inspect = async (bytes: Buffer) => {
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-devicetree", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
return toolContract("inspect_devicetree").outputSchema.parse(result.json);
};
const control = await inspect(zeroPadded);
expect(control.limitations).not.toEqual(
expect.arrayContaining([expect.stringMatching(paddingDiagnostic)]),
);
const observed = await inspect(nonzeroPadded);
const diagnostic = `FDT structure padding contains 2 nonzero bytes. The first is at file offset ${String(propertyPadding)}.`;
expect(observed.limitations).toContain(diagnostic);
expect(observed.normalized_result).toMatchObject({
summary: { model: "x" },
nodes: expect.arrayContaining([expect.objectContaining({ path: "/a" })]),
coverage: {
status: "complete",
examined_nodes: 2,
examined_properties: 1,
},
limitations: expect.arrayContaining([diagnostic]),
});
const { call } = await connectLocalToolsMcp();
const reply = await call("inspect_devicetree", { path });
expect(reply.isError).not.toBe(true);
const mcp = toolContract("inspect_devicetree").outputSchema.parse(
reply.structuredContent,
);
expect(mcp.limitations).toContain(diagnostic);
expect(mcp.normalized_result).toEqual(observed.normalized_result);
expect((await readFile(path)).equals(nonzeroPadded)).toBe(true);
},
);
cliTest(
"reports FIT structure padding without losing image inventory",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-fit-padding-");
const path = join(root, "boot.itb");
const bytes = fitFixture({
images: [{ name: "kernel", data: Buffer.from([1, 2, 3, 4]) }],
});
const inspect = () =>
cli.run({ arguments: ["inspect-artifact", path, "--json"] });
await writeFile(path, bytes);
const control = await inspect();
expect(control.exitCode, control.stderr).toBe(0);
expect(control.json).not.toMatchObject({
limitations: expect.arrayContaining([
expect.stringMatching(paddingDiagnostic),
]),
});
const rootNamePadding = bytes.readUInt32BE(8) + 5;
bytes[rootNamePadding] = 1;
await writeFile(path, bytes);
const observed = await inspect();
expect(observed.exitCode, observed.stderr).toBe(0);
expect(observed.json).toMatchObject({
limitations: expect.arrayContaining([
`FDT structure padding contains 1 nonzero byte. The first is at file offset ${String(rootNamePadding)}.`,
]),
normalized_result: {
coverage: { status: "complete-within-substeps" },
observations: expect.arrayContaining([
expect.objectContaining({
kind: "root-manifest",
value: expect.objectContaining({
root_format: "fit",
occurrence_count: 2,
}),
}),
]),
},
});
expect((await readFile(path)).equals(bytes)).toBe(true);
},
);
const contradictoryReports = (report: Devicetree, nodeIndex: number) => {
const duplicate = (first: boolean) => {
const changed = structuredClone(report);
changed.nodes.splice(first ? nodeIndex : changed.nodes.length, 0, {
...report.nodes[nodeIndex]!,
properties: [],
});
changed.coverage.examined_nodes = changed.nodes.length;
return {
changed,
issuePath: [
"nodes",
first ? nodeIndex + 1 : changed.nodes.length - 1,
"path",
],
message: "Duplicate Devicetree node path.",
collection: first ? ("properties" as const) : ("nodes" as const),
adapter: first ? "cli" : "mcp",
};
};
const wrongGeometry = structuredClone(report);
wrongGeometry.nodes[nodeIndex]!.properties[0]!.offset =
report.header.totalsize;
const wrongCoverage = {
...report,
coverage: { ...report.coverage, examined_nodes: report.nodes.length + 1 },
};
return [
duplicate(true),
duplicate(false),
{
changed: wrongGeometry,
issuePath: ["nodes", nodeIndex, "properties", 0, "offset"],
message: "The property range exceeds the declared Devicetree extent.",
collection: "properties",
adapter: "mcp",
},
{
changed: wrongCoverage,
issuePath: ["coverage", "examined_nodes"],
message: "Examined node count disagrees with the listed nodes.",
collection: "nodes",
adapter: "cli",
},
] as const;
};
cliTest(
"rejects contradictory Devicetree reports in authenticated Evidence",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-dtb-duplicate-path-");
const path = join(root, "board.dtb");
const bytes = devicetreeFixture();
await writeFile(path, bytes);
const original = await cli.run({
arguments: ["inspect-devicetree", path, "--json"],
});
expect(original.exitCode, original.stderr).toBe(0);
const evidence = toolContract("inspect_devicetree").outputSchema.parse(
original.json,
);
const report = evidence.normalized_result;
if (!("nodes" in report)) throw new Error("Expected complete DTB report");
const nodePath = "/serial@1000";
const index = report.nodes.findIndex((node) => node.path === nodePath);
const node = report.nodes[index];
if (node === undefined) throw new Error("Expected serial node");
const requestPath = join(root, "view.json");
const request = (parent: unknown, collection: "nodes" | "properties") => ({
source: { kind: "inline", evidence: parent },
view: { kind: "devicetree", collection, path: nodePath },
});
const inspect = async (input: unknown) => {
await writeFile(requestPath, JSON.stringify(input));
return cli.run({
arguments: ["inspect-analysis-view", requestPath, "--json"],
});
};
const control = await inspect(request(evidence, "properties"));
expect(control.exitCode, control.stderr).toBe(0);
expect(control.json).toMatchObject({
normalized_result: {
items: [
{ name: "reg" },
{ name: "phandle" },
{ name: "interrupt-parent" },
],
},
});
const { call } = await connectLocalToolsMcp();
const withReport = (changed: Devicetree) =>
createEvidence(
{ path, sha256: report.artifact.sha256, format: "file" },
evidence.provider,
{
operation: evidence.operation,
parameters: evidence.parameters,
result: changed,
confidence: evidence.confidence,
limitations: evidence.limitations,
locations: evidence.locations,
},
);
for (const {
changed,
issuePath,
message,
collection,
adapter,
} of contradictoryReports(report, index)) {
const input = request(withReport(changed), collection);
const error = {
code: "invalid_request",
details: {
issues: [
{
path: ["source", "normalized_result", ...issuePath],
reason: "invalid_value",
message,
},
],
},
};
if (adapter === "cli") {
const reply = await inspect(input);
expect(reply.exitCode, reply.stderr + reply.stdout).toBe(1);
expect(reply.json).toMatchObject(error);
} else {
const reply = await call("inspect_analysis_view", input);
expect(reply.isError).toBe(true);
expect(parseMcpToolError(reply).error).toMatchObject(error);
}
}
expect((await readFile(path)).equals(bytes)).toBe(true);
},
);
cliTest("inspects a relative DTB and preserves the source", async ({ cli }) => {
const root = await createTestTempDirectory("rea-dtb-cli-");
const path = join(root, "board.dtb"),
bytes = devicetreeFixture();
await writeFile(path, bytes);
const result = await cli.run({
arguments: [
"inspect-devicetree",
"board.dtb",
"--max-file-bytes",
"4096",
"--max-depth",
"8",
"--max-properties",
"64",
"--json",
],
cwd: root,
});
expect(result.exitCode, result.stderr).toBe(0);
const report = toolContract("inspect_devicetree").outputSchema.parse(
result.json,
).normalized_result;
expect(report).toMatchObject({
artifact: { path },
summary: { model: "Test board" },
coverage: { examined_nodes: 5 },
});
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
});
for (const version of [16, 17] as const) {
cliTest(
`inspects a v${version} DTB with empty strings at the reservation boundary`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-dtb-empty-strings-");
const path = join(root, "board.dtb");
const bytes = emptyStringsAtReservationBoundaryFixture(version);
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-devicetree", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const report = toolContract("inspect_devicetree").outputSchema.parse(
result.json,
).normalized_result;
expect(report).toMatchObject({
header: {
version,
off_dt_strings: 56,
size_dt_strings: 0,
size_dt_struct: version === 16 ? null : 16,
},
nodes: [{ path: "/" }],
reservations: [],
});
expect((await readFile(path)).equals(bytes)).toBe(true);
},
);
}
cliTest(
"keeps references unresolved when either declared phandle is malformed",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-dtb-cli-phandle-");
const path = join(root, "board.dtb");
await writeFile(
path,
devicetreeFixture({
name: "",
children: [
{
name: "bad_canonical",
properties: { phandle: Buffer.alloc(0), "linux,phandle": cells(3) },
},
{
name: "bad_legacy",
properties: { phandle: cells(4), "linux,phandle": cells(1, 2) },
},
{
name: "legacy_only",
properties: { "linux,phandle": Buffer.from([0, 0]) },
},
{
name: "canonical_consumer",
properties: { "interrupt-parent": cells(3) },
},
{
name: "legacy_consumer",
properties: { "interrupt-parent": cells(4) },
},
],
}),
);
const result = await cli.run({
arguments: ["inspect-devicetree", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const report = toolContract("inspect_devicetree").outputSchema.parse(
result.json,
).normalized_result;
if (!("references" in report)) throw new Error("Expected complete report");
expect(report.references).toMatchObject([
{ source: "/canonical_consumer", phandle: 3, target: null },
{ source: "/legacy_consumer", phandle: 4, target: null },
]);
expect(report.nodes[1]?.properties[0]).toMatchObject({
name: "phandle",
bytes: 0,
hex: "",
derived: { cells: null },
});
expect(report.nodes[2]?.properties[1]).toMatchObject({
name: "linux,phandle",
bytes: 8,
hex: cells(1, 2).toString("hex"),
derived: { cells: null },
});
expect(report.limitations).toEqual(
expect.arrayContaining([
expect.stringMatching(
/phandle.*\/bad_canonical.*not a single cell.*unresolved/iu,
),
expect.stringMatching(
/linux,phandle.*\/bad_legacy.*not a single cell.*unresolved/iu,
),
expect.stringMatching(
/linux,phandle.*\/legacy_only.*not a single cell.*unresolved/iu,
),
]),
);
},
);
cliTest(
"retains complete DTB Evidence when delivering a summary",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-dtb-cli-summary-");
const path = join(root, "board.dtb"),
output = join(root, "evidence.json");
await writeFile(path, devicetreeFixture());
const result = await cli.run({
arguments: [
"inspect-devicetree",
path,
"--detail",
"summary",
"--evidence_output",
output,
"--json",
],
});
expect(result.exitCode, result.stderr).toBe(0);
expect(
toolContract("inspect_devicetree").outputSchema.parse(
z.object({ summary: z.unknown() }).parse(result.json).summary,
).normalized_result,
).toMatchObject({
kind: "summary",
summary: { unresolved_reference_count: 1 },
});
const bundle = z
.object({ records: z.array(z.unknown()).length(1) })
.parse(JSON.parse(await readFile(output, "utf8")));
const retained = toolContract("inspect_devicetree").outputSchema.parse(
bundle.records[0],
).normalized_result;
if (!("nodes" in retained)) throw new Error("Expected complete evidence");
expect(retained.nodes).toEqual(
expect.arrayContaining([
expect.objectContaining({
path: "/serial@1000",
properties: expect.arrayContaining([
expect.objectContaining({
name: "reg",
derived: expect.objectContaining({
reg: [{ address: "0x1000", size: "0x100" }],
}),
}),
]),
}),
]),
);
},
);
for (const [name, build, flags, code] of [
[
"malformed",
() => devicetreeFixture().subarray(0, 30),
[],
"unsupported_target",
],
[
"unsupported",
() => devicetreeFixture(undefined, 18),
[],
"unsupported_target",
],
[
"exhausted",
() => devicetreeFixture(),
["--max-nodes", "1"],
"resource_constraint",
],
] as const) {
cliTest(`reports ${name} DTB input`, async ({ cli }) => {
const root = await createTestTempDirectory("rea-dtb-cli-errors-");
const path = join(root, "board.dtb");
await writeFile(path, build());
const result = await cli.run({
arguments: ["inspect-devicetree", path, ...flags, "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({ code });
if (name === "exhausted")
expect(result.json).toMatchObject({
details: {
reported_limits: { boundary: "max_nodes", maximum: 1 },
reason: expect.stringContaining("raise that caller-selected limit"),
},
retryable: false,
});
});
}