mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
* perf(tests): give MCP test clients the shared lazy schema validation The MCP SDK client compiles every listed tool's output schema on its first tool call. With the SDK's default Ajv options, REA's catalog costs gigabytes of allocation per client (inspect_analysis_view alone: 1.7 s and ~500 MiB retained), so native MCP files peaked at 1-2 GiB per worker. tests/fixtures/mcpSchemaValidation already owns lazy, reference-preserving validation. Route the native MCP, process-boundary and shared harness clients through it, share compiled validators across clients in a worker (bounded LRU keyed by schema text), and add a counted architecture guard so default-validation test clients cannot spread; the remaining copies are the baseline. * fix(guards): read call arguments past parentheses inside string literals * perf(tests): give MCP boundary test clients the shared schema validation (#11022) * perf(tests): give MCP boundary test clients the shared schema validation * perf(tests): give the remaining MCP test clients the shared schema validation (#11025) * fix(guards): read an MCP client's validator option from parsed code The default-validation counter searched each new Client(...) call's text for jsonSchemaValidator, so an inline comment, a string literal or a client name naming the option hid a default client. Parse the source with @babel/parser and count NewExpressions of Client whose object-literal arguments have no jsonSchemaValidator property; a source that does not parse counts every textual call. The guard reads raw test sources, and the generated-child-script allowlist entry, which no longer counts as a call, is removed. * fix(guards): read an MCP client's options only from a whole object-literal argument The default-validation counter skipped files without the text 'new Client(', so a comment between the tokens hid a client, and it took any depth-two key as an option, so 'enabled ? { jsonSchemaValidator } : {}' passed while its false branch uses the SDK default. The skip now only requires the name Client, and the validator counts only when it is a key of an object literal that is a whole argument; anything else counts as the default.
428 lines
13 KiB
TypeScript
428 lines
13 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_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 full host-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}`);
|
|
// Path rules belong to the canonical input contract.
|
|
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();
|
|
}
|
|
});
|
|
|
|
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");
|
|
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();
|
|
}
|
|
});
|