Files
reverse-engineering-anything/tests/boundary/mcp/androidBootImageMcp.test.ts
morluto 61b4ada453 fix(errors)!: split reader path refusals from malformed target bytes
ArtifactReaderFailure("format") covered two different facts: target
bytes that violate their format, and a selected path that is not a
regular file. The shared selected-file projections
(projectOwnedArtifactInputFailure and classifyProviderFailure's file
step) mapped both to invalid_request with an invalid_format path
issue, so corrupt BEAM files and PE certificate table bodies were
reported as caller argument errors.

Split the reason at its producers:

- Path-kind refusals now use the existing "path" selection reason:
  regular-file admission, the stable root, directory, ZIP, ASAR and
  PyInstaller entries, dyld inputs, the UEFI flash source and extract
  occurrence selection. They stay invalid_request.
- The BEAM parser and the PE certificate table parser raise the format
  owner's ParseFailureError (malformed or truncated, format "beam" or
  "pe-certificate-table"). A short file that does not start with FOR1
  is malformed, not truncated. A PKCS#7 name encoding the decoder does
  not implement is unsupported, not malformed.
- Short reads inside an admitted range (kernel module, kernel build
  config, ELF unwind ranges) are integrity read-coverage failures,
  because the file changed during the read.
- Internal contract breaks (stream chunk size, a non-Buffer keyed
  archive chunk) are invariant violations; an impossible descriptor
  byte count is an I/O failure.

The shared projections now treat only "path" as a selection error.
They take the malformed-target facts from the reader failure's own
parseFailure, which artifactParseFailure sets once from the owner's
ParseFailureError and rebuilt failures keep through primaryFacts.
ArtifactOperationError carries the same facts, so inspect_artifact
refusals (for example Android boot images) now include
details.format_failure, format, offset and field. Android boot entry
planning raises the format owner's malformed failure, and the
redundant shortcut and API-contract format branches are removed.

BREAKING CHANGE: callers that parse `code` see unsupported_target
instead of invalid_request for malformed or truncated bytes from
inspect_erlang_beam and from the inspect_pe_certificates certificate
table body. Truncated reads of a file that changed during
inspect_kernel_module, inspect_kernel_build_config and
inspect_elf_unwind_ranges now report artifact_changed. inspect_artifact
malformed and truncated refusals gain details.format_failure and
details.format. Existing fields are unchanged.
2026-11-23 20:11:49 -05:00

152 lines
5.6 KiB
TypeScript

import { createHash } from "node:crypto";
import { readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { artifactInspectionResultSchema } from "../../../src/domain/artifactInspection.js";
import { artifactExtractionResultSchema } from "../../../src/domain/artifactGraph.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import {
appendAvbFooter,
androidBootImageRefusals,
buildBootImage,
osVersionRaw,
} from "../../fixtures/androidBootImage.js";
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
it("retains Android boot image facts through compiled MCP", async () => {
const root = await createTestTempDirectory("rea-android-boot-mcp-");
const path = join(root, "boot.img");
const kernel = Buffer.from([0x7f, 0x45, 0x4c, 0x46, 2, 1, 1, 0]);
const ramdisk = Buffer.from([0x1f, 0x8b, 0x08, 0, 0x61]);
const signature = Buffer.from("boot-signature");
const image = buildBootImage({
version: 4,
kernel,
ramdisk,
signature,
cmdline: "console=tty0",
osVersion: osVersionRaw(13, 0, 0, 2024, 11),
});
const bytes = appendAvbFooter(image);
await writeFile(path, bytes);
const { call } = await connectLocalToolsMcp({
REA_MCP_INPUT_SCHEMA_PROFILE: "compact",
});
let outputRoot: string | undefined;
try {
const opened = await call("open_binary", { path });
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
expect(opened.structuredContent).toMatchObject({
result: {
kind: "archive",
format: "android-boot-image",
architecture: null,
},
});
const reply = await call("inspect_artifact", {});
expect(reply.isError, JSON.stringify(reply)).not.toBe(true);
const inspection = artifactInspectionResultSchema.parse(
parseEvidence(reply.structuredContent).normalized_result,
);
const inventory = inspection.substeps[0]!.evidence.normalized_result;
expect(inventory.coverage.status).toBe("complete");
expect(inventory.occurrences).toContainEqual(
expect.objectContaining({
logical_path: ".",
format_metadata: expect.objectContaining({
kind: "android-boot-image",
header_version: 4,
os_version: "13.0.0",
security_patch_level: "2024-11",
command_line: "console=tty0",
signature_size: signature.length,
avb_footer_present: true,
avb_footer_size: 64,
}),
}),
);
expect(inventory.occurrences).toContainEqual(
expect.objectContaining({
logical_path: "ramdisk",
format_metadata: expect.objectContaining({ compression: "gzip" }),
source_location: { offset: 8192, length: ramdisk.length },
}),
);
expect(inventory.occurrences).toContainEqual(
expect.objectContaining({
logical_path: "signature",
source_location: { offset: 12288, length: signature.length },
}),
);
expect(inventory.nodes).toContainEqual(
expect.objectContaining({
sha256: createHash("sha256").update(ramdisk).digest("hex"),
}),
);
const extracted = await call("extract_artifact", {});
expect(extracted.isError, JSON.stringify(extracted)).not.toBe(true);
const result = artifactExtractionResultSchema.parse(
parseEvidence(extracted.structuredContent).normalized_result,
);
outputRoot = result.output_root;
expect(result.containment_verified).toBe(true);
expect(result.cleanup).toMatchObject({
verified: true,
residual_paths: [],
});
expect((await readFile(join(outputRoot, "kernel"))).equals(kernel)).toBe(
true,
);
expect((await readFile(join(outputRoot, "ramdisk"))).equals(ramdisk)).toBe(
true,
);
expect(
(await readFile(join(outputRoot, "signature"))).equals(signature),
).toBe(true);
expect((await readFile(path)).equals(bytes)).toBe(true);
await call("close_binary", {});
} finally {
if (outputRoot !== undefined)
await rm(outputRoot, { recursive: true, force: true });
}
});
it("keeps Android parser refusal kinds and recovers through compiled MCP", async () => {
const root = await createTestTempDirectory("rea-android-boot-mcp-refusals-");
const { call } = await connectLocalToolsMcp({
REA_MCP_INPUT_SCHEMA_PROFILE: "compact",
});
try {
for (const fixture of androidBootImageRefusals()) {
const path = join(root, `${fixture.name}.img`);
await writeFile(path, fixture.bytes);
const opened = await call("open_binary", { path });
expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
const { error } = parseMcpToolError(await call("inspect_artifact", {}));
expect(error).toMatchObject({
code: fixture.code,
category: fixture.category,
details: {
operation: "inspect_artifact",
reason: fixture.kind,
...("format" in fixture
? { format_failure: fixture.kind, format: fixture.format }
: {}),
},
});
expect((await readFile(path)).equals(fixture.bytes)).toBe(true);
expect((await call("close_binary", {})).isError).not.toBe(true);
}
const path = join(root, "valid.img");
await writeFile(path, buildBootImage({ version: 0 }));
expect((await call("open_binary", { path })).isError).not.toBe(true);
expect((await call("inspect_artifact", {})).isError).not.toBe(true);
} finally {
await call("close_binary", {});
}
});