Files
rea/tests/boundary/mcp/absoluteLocalPathSchema.test.ts
T
morluto 88fd8a9df0 test: drop detail requests the budget seed made redundant (#14615)
PR #14252 added explicit complete-detail requests across tests before the
vitest REA_AGENT_RESULT_BUDGET_BYTES seed and the complete-Evidence harness
existed. Restore the pre-#14252 form where the seed or the harness already
delivers complete Evidence.

Keep the requests that are load-bearing: delivery, summary and budget tests,
tools whose omitted detail is summary, and callers that neither the seed nor
the harness reaches (unwrapped SDK clients on servers with an explicit
environment, and in-process CLIs). Those were found by running the touched
files with the shipped budget lowered to its 4 KiB minimum.
2026-11-25 21:08:12 +00:00

456 lines
14 KiB
TypeScript

import { canonicalInputSchema } from "../../fixtures/canonicalOutputSchema.js";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { Ajv2020 } from "ajv/dist/2020.js";
import { resolve } from "node:path";
import { expect, it } from "vitest";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { createServer } from "../../../src/server/createServer.js";
import { silentLogger } from "../../../src/logger.js";
import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js";
interface PathInputCase {
readonly name: string;
readonly field: string;
readonly valid: Readonly<Record<string, unknown>>;
readonly invalid: Readonly<Record<string, unknown>>;
readonly issuePath: readonly string[];
}
const createMcp = async (firmwareCalls: string[], profile?: "compact") => {
const session = createTestBinarySession(() => {
throw new Error("Invalid path input must not acquire a binary provider");
});
const server = createServer(
{ kind: "session", session },
{
logger: silentLogger,
environment: { ...process.env, REA_MCP_INPUT_SCHEMA_PROFILE: profile },
firmwareAnalysis: {
execute: async (request) => {
firmwareCalls.push(request.operation);
throw new Error("Invalid path input reached firmware analysis");
},
close: async () => {},
},
},
);
const client = new Client(
{ name: "absolute-path-schema", version: "1" },
{ jsonSchemaValidator: createMcpSchemaValidation() },
);
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
const close = async (): Promise<void> => {
const failures: unknown[] = [];
for (const release of [
() => client.close(),
() => server.close(),
() => session.close(),
]) {
try {
await release();
} catch (cause) {
failures.push(cause);
}
}
if (failures.length > 0)
throw new AggregateError(failures, "MCP path fixture cleanup failed");
};
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
return { client, close };
} catch (cause: unknown) {
await close();
throw cause;
}
};
const standaloneAnalysisCases = (
absolute: string,
relative: string,
): readonly PathInputCase[] =>
[
{ name: "inspect_go_binary", input: {} },
{ name: "inspect_recorded_crash", input: {} },
{ name: "inspect_binary_layout", input: {} },
{ name: "inspect_rust_binary", input: {} },
{ name: "inspect_evm_interface", input: { encoding: "hex" } },
{ name: "inspect_sqlite_database", input: {} },
{ name: "inspect_pe_resources", input: {} },
{ name: "inspect_native_exports", input: {} },
{ name: "inspect_elf_dependencies", input: {} },
{ name: "inspect_electron_fuses", input: {} },
{ name: "inspect_packet_capture", input: { fields: ["ip.addr"] } },
{
name: "decode_signal_recording",
input: { decoders: "uart:rx=RX:baudrate=9600" },
},
].map(({ name, input }) => ({
name,
field: "path",
valid: { ...input, path: absolute },
invalid: { ...input, path: relative },
issuePath: ["path"],
}));
const inputCases = (
absolute: string,
relative: string,
): readonly PathInputCase[] => {
const pathCase = (
name: string,
field: string,
valid: Readonly<Record<string, unknown>>,
invalid: Readonly<Record<string, unknown>>,
): PathInputCase => ({
name,
field,
valid,
invalid,
issuePath: field.split("."),
});
return [
pathCase("open_binary", "path", { path: absolute }, { path: relative }),
pathCase(
"extract_artifact",
"output_root",
{ output_root: absolute },
{ output_root: relative },
),
pathCase(
"analyze_javascript_application",
"input_path",
{ input_path: absolute, detail: "complete" },
{ input_path: relative, detail: "complete" },
),
pathCase(
"open_binary",
"snapshot_path",
{ path: absolute, snapshot_path: absolute },
{ path: absolute, snapshot_path: relative },
),
pathCase(
"close_binary",
"snapshot_path",
{ snapshot_path: absolute },
{ snapshot_path: relative },
),
pathCase(
"export_evidence_bundle",
"path",
{ path: absolute },
{ path: relative },
),
pathCase(
"import_evidence_bundle",
"path",
{ path: absolute },
{ path: relative },
),
pathCase(
"compare_bundles",
"left_bundle_path",
{ left_bundle_path: absolute, right_bundle_path: absolute },
{ left_bundle_path: relative, right_bundle_path: absolute },
),
pathCase(
"compare_bundles",
"right_bundle_path",
{ left_bundle_path: absolute, right_bundle_path: absolute },
{ left_bundle_path: absolute, right_bundle_path: relative },
),
pathCase(
"binary_session",
"expected_server_path",
{ expected_server_path: absolute },
{ expected_server_path: relative },
),
...[
"inspect_managed_artifact",
"inspect_managed_members",
"inspect_managed_native_boundaries",
].map((name) =>
pathCase(name, "path", { path: absolute }, { path: relative }),
),
pathCase(
"inspect_firmware_regions",
"path",
{ path: absolute },
{ path: relative },
),
pathCase(
"extract_firmware",
"path",
{ path: absolute, output_directory: absolute },
{ path: relative, output_directory: absolute },
),
pathCase(
"extract_firmware",
"output_directory",
{ path: absolute, output_directory: absolute },
{ path: absolute, output_directory: relative },
),
pathCase(
"inspect_analysis_view",
"source.path",
{
source: { kind: "evidence-file", path: absolute },
view: { kind: "summary" },
},
{
source: { kind: "evidence-file", path: relative },
view: { kind: "summary" },
},
),
pathCase(
"capture_browser_scenario",
"browser.executable_path",
{
browser: { mode: "launch", executable_path: absolute },
start_url: { url: "https://example.test/" },
actions: [
{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 },
],
},
{
browser: { mode: "launch", executable_path: relative },
start_url: { url: "https://example.test/" },
actions: [
{ step_id: "wait", action: "wait_for_timeout", duration_ms: 1 },
],
},
),
...standaloneAnalysisCases(absolute, relative),
...["executable_path", "application_path", "application_root"].map(
(field) =>
pathCase(
"capture_electron_scenario",
field,
{
executable_path: absolute,
application_path: absolute,
[field]: absolute,
},
{
executable_path: absolute,
application_path: absolute,
[field]: relative,
},
),
),
];
};
it("advertises portable absolute-path constraints after SDK schema conversion", async () => {
const firmwareCalls: string[] = [];
const { client, close } = await createMcp(firmwareCalls, "compact");
try {
const listed = await client.listTools();
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const absolute = resolve("rea-absolute-input");
const relative = "relative-input";
const schemaCases = [
...inputCases(absolute, relative),
{
name: "inspect_go_binary",
field: "path NUL",
valid: { path: absolute },
invalid: { path: `${absolute}\0` },
},
{
name: "import_evidence_bundle",
field: "path NUL",
valid: { path: absolute },
invalid: { path: `${absolute}\0` },
},
{
name: "compare_bundles",
field: "left_bundle_path empty",
valid: { left_bundle_path: absolute, right_bundle_path: absolute },
invalid: { left_bundle_path: "", right_bundle_path: absolute },
},
{
name: "compare_bundles",
field: "left_bundle_path NUL",
valid: { left_bundle_path: absolute, right_bundle_path: absolute },
invalid: {
left_bundle_path: `${absolute}\0`,
right_bundle_path: absolute,
},
},
];
const checked: string[] = [];
for (const { name, field, valid, invalid } of schemaCases) {
if (!listed.tools.some((tool) => tool.name === name))
throw new Error(`Missing advertised tool ${name}`);
// The canonical projection keeps portable constraints; handlers enforce
// the executing host's stricter path rules.
const validate = ajv.compile(canonicalInputSchema(name));
expect(validate(invalid), `${name}.${field}`).toBe(false);
expect(
validate(valid),
`${name}.${field}: ${JSON.stringify(validate.errors)}`,
).toBe(true);
checked.push(`${name}.${field}`);
}
expect(checked).toEqual(
expect.arrayContaining([
"open_binary.path",
"extract_firmware.output_directory",
"inspect_go_binary.path NUL",
"compare_bundles.left_bundle_path empty",
]),
);
expect(firmwareCalls).toEqual([]);
} finally {
await close();
}
});
const relativeInspectionPaths = [
["inspect_dwarf_units", "program"],
["inspect_elf_dependencies", "program"],
["inspect_elf_notes", "program"],
["inspect_elf_unwind_ranges", "program"],
["inspect_erlang_beam", "app.beam"],
["inspect_kernel_build_config", "vmlinux"],
["inspect_kernel_module", "demo.ko"],
["inspect_maven_gradle_metadata", "pom.xml"],
["inspect_native_initializers", "program"],
["inspect_nuget_nupkg", "pkg.nupkg"],
["inspect_pe_certificates", "signed.exe"],
["inspect_pe_imports", "app.exe"],
["inspect_python_pyc", "sample.pyc"],
["inspect_rust_binary", "sample"],
["inspect_v8_code_cache", "module.jsc"],
] as const;
it("rejects relative paths through in-memory MCP handlers", async () => {
const firmwareCalls: string[] = [];
const { client, close } = await createMcp(firmwareCalls);
try {
const cases = [
...inputCases(resolve("rea-absolute-input"), "relative-input"),
...relativeInspectionPaths.map(([name, path]) => ({
name,
field: "path",
invalid: { path },
issuePath: ["path"],
})),
];
const checked: string[] = [];
for (const { name, field, invalid, issuePath } of cases) {
const result = await client.callTool({ name, arguments: invalid });
expect(result.isError, `${name}.${field}`).toBe(true);
expect(parseMcpToolError(result).error, `${name}.${field}`).toMatchObject(
{
code: "invalid_request",
details: {
operation: name,
issues: [
expect.objectContaining({
path: issuePath,
reason: "invalid_format",
expected: "regex",
}),
],
},
},
);
checked.push(`${name}.${field}`);
}
expect(checked).toEqual(
expect.arrayContaining([
"open_binary.path",
"open_binary.snapshot_path",
"extract_firmware.path",
"extract_firmware.output_directory",
"inspect_analysis_view.source.path",
"capture_browser_scenario.browser.executable_path",
"capture_electron_scenario.application_root",
]),
);
expect(firmwareCalls).toEqual([]);
} finally {
await close();
}
});
// advertisedDialect.test.ts proves no schema profile advertises the
// surrogate escape; this case proves the handlers still refuse such paths.
it("rejects unpaired surrogates through in-memory MCP handlers", async () => {
const firmwareCalls: string[] = [];
const { client, close } = await createMcp(firmwareCalls);
try {
const absolute = resolve("rea-absolute-input");
const cases = inputCases(absolute, `${absolute}\ud800`);
const checked: string[] = [];
for (const { name, field, invalid, issuePath } of cases) {
const result = await client.callTool({ name, arguments: invalid });
expect(result.isError, `${name}.${field}`).toBe(true);
expect(parseMcpToolError(result).error, `${name}.${field}`).toMatchObject(
{
code: "invalid_request",
details: {
operation: name,
issues: [
{
path: issuePath,
reason: "invalid_format",
expected: "regex",
message:
"Local filesystem paths must contain well-formed Unicode",
},
],
},
},
);
checked.push(`${name}.${field}`);
}
expect(checked).toEqual(
expect.arrayContaining([
"open_binary.path",
"open_binary.snapshot_path",
"extract_firmware.path",
"extract_firmware.output_directory",
"inspect_analysis_view.source.path",
"capture_browser_scenario.browser.executable_path",
"capture_electron_scenario.application_root",
]),
);
// Disassembly symbols share the rule and its correction shape.
const symbol = await client.callTool({
name: "inspect_with_objdump",
arguments: {
path: absolute,
operation: "disassemble",
symbol: "main\ud800",
},
});
expect(symbol.isError).toBe(true);
expect(parseMcpToolError(symbol).error).toMatchObject({
code: "invalid_request",
details: {
issues: [
{
path: ["symbol"],
reason: "invalid_format",
expected: "regex",
message: "Symbol must be well-formed Unicode.",
},
],
},
});
expect(firmwareCalls).toEqual([]);
} finally {
await close();
}
});