fix(mcp): reject unknown MCP tool inputs (#975)

* fix(mcp): reject unknown official tool inputs

* fix(mcp): reject unknown native tool inputs

* test(mcp): follow native contract module move

* fix(mcp): reject unknown remaining tool inputs

* fix(mcp): satisfy strict unknown and browser input schemas

Remove obsolete approval fields from residual-unknown fixtures, project update inputs explicitly, and stop passing target_id to list-targets parsing.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
morluto
2026-10-08 01:10:53 +08:00
committed by GitHub
co-authored by Cursor Agent
parent ae0f725fad
commit fe64fd65d9
18 changed files with 267 additions and 25 deletions
@@ -73,9 +73,12 @@ describeBrowser("CdpBrowserProvider: discovery authorization 1", () => {
const browser = await startFakeCdpBrowser({ urlShapedAllowedTitle });
trackBrowser(browser);
const provider = new CdpBrowserProvider();
const input = {
const listInput = {
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
};
const input = {
...listInput,
target_id: "allowed-page",
};
const authority = browser.allowedOrigin.replace(/^https?:\/\//u, "");
@@ -86,7 +89,7 @@ describeBrowser("CdpBrowserProvider: discovery authorization 1", () => {
? "/app?startup=title-secret#fragment"
: `${prefix}${authority}/app?startup=title-secret#fragment`;
const listed = await provider.listTargets(
listBrowserTargetsInputSchema.parse(input),
listBrowserTargetsInputSchema.parse(listInput),
);
if (!listed.ok) throw listed.error;
expect(listed.value.targets[0]?.title).toBe(expected);
+31
View File
@@ -27,7 +27,9 @@ it("runs every managed static inspection independently of an active native targe
"rea-managed-independent-mcp-",
);
const path = join(directory, "fixture.dll");
const otherPath = join(directory, "other-fixture.dll");
await writeFile(path, buildManagedPeFixture());
await writeFile(otherPath, buildManagedPeFixture({ methodName: "Other" }));
const session = composeBinarySession(
SessionProviderRouter.selectable(new AnalysisProviderRegistry([]), [
new ManagedStaticProvider(),
@@ -54,6 +56,12 @@ it("runs every managed static inspection independently of an active native targe
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const managedMembersTool = (await client.listTools()).tools.find(
({ name }) => name === "inspect_managed_members",
);
expect(managedMembersTool?.inputSchema).toMatchObject({
additionalProperties: false,
});
const artifact = structured(
await client.callTool({
name: "inspect_managed_artifact",
@@ -67,6 +75,11 @@ it("runs every managed static inspection independently of an active native targe
subject: { local_path: path, format: "pe" },
},
});
const misspelledPath = await client.callTool({
name: "inspect_managed_members",
arguments: { pth: otherPath },
});
expect(misspelledPath.isError).toBe(true);
const invalidSelection = await client.callTool({
name: "inspect_managed_artifact",
arguments: { path: join(directory, "missing.dll") },
@@ -82,6 +95,24 @@ it("runs every managed static inspection independently of an active native targe
subject: { local_path: path, format: "pe" },
},
});
const otherArtifact = structured(
await client.callTool({
name: "inspect_managed_artifact",
arguments: { path: otherPath },
}),
);
expect(otherArtifact).toMatchObject({
evidence: { subject: { local_path: otherPath, format: "pe" } },
});
const reusedOtherTarget = structured(
await client.callTool({ name: "inspect_managed_members", arguments: {} }),
);
expect(reusedOtherTarget).toMatchObject({
evidence: {
operation: "inspect_managed_members",
subject: { local_path: otherPath, format: "pe" },
},
});
const boundaries = structured(
await client.callTool({
name: "inspect_managed_native_boundaries",
@@ -0,0 +1,119 @@
import { Ajv2020 } from "ajv/dist/2020.js";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { afterEach, describe, expect, it } from "vitest";
import type { AnalysisOperationPort } from "../../../src/application/AnalysisProvider.js";
import { NATIVE_TOOL_CONTRACTS } from "../../../src/contracts/native/nativeToolContracts.js";
import type { JsonValue } from "../../../src/domain/jsonValue.js";
import { ok } from "../../../src/domain/result.js";
import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js";
import { createServer } from "../../../src/server/createServer.js";
const provider = {
id: "fixture-native",
name: "Fixture native provider",
version: "1",
} as const;
const plistResult = {
format: "xml",
value: {},
bundle: {
identifier: null,
executable: null,
name: null,
version: null,
short_version: null,
},
source_path: "/Applications/Fixture.app/Contents/Info.plist",
provenance: [],
limitations: [],
};
const resources: Array<{ close(): Promise<void> }> = [];
const connect = async (
execute: AnalysisOperationPort["execute"],
): Promise<Client> => {
const server = createServer({ execute });
const client = new Client({ name: "native-contract-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
resources.push(client, server);
await server.connect(serverTransport);
await client.connect(clientTransport);
return client;
};
afterEach(async () => {
await Promise.all(
resources.splice(0).map(async (resource) => resource.close()),
);
});
describe("native MCP input contracts", () => {
it("rejects a misspelled plist path before dispatch and preserves omission", async () => {
const calls: Array<{
readonly operation: string;
readonly parameters: Readonly<Record<string, JsonValue>>;
}> = [];
const client = await connect((operation, parameters) => {
calls.push({ operation, parameters });
return Promise.resolve(
ok(createAnalysisExecution(plistResult, provider)),
);
});
const tools = new Map(
(await client.listTools()).tools.map((tool) => [tool.name, tool]),
);
for (const nativeContract of NATIVE_TOOL_CONTRACTS)
expect(tools.get(nativeContract.name)?.inputSchema).toHaveProperty(
"additionalProperties",
false,
);
const contract = NATIVE_TOOL_CONTRACTS.find(
({ name }) => name === "inspect_plist",
);
const advertised = tools.get("inspect_plist");
if (contract === undefined || advertised === undefined)
throw new Error("inspect_plist was not registered");
const validate = new Ajv2020({
strict: false,
validateFormats: false,
}).compile(advertised.inputSchema);
const typoInput = { pth: "/tmp/Other.plist" };
expect(validate(typoInput)).toBe(false);
expect(contract.inputSchema.safeParse(typoInput).success).toBe(false);
expect(validate({})).toBe(true);
expect(contract.inputSchema.safeParse({}).success).toBe(true);
const defaulted = await client.callTool({
name: "inspect_plist",
arguments: {},
});
expect(defaulted.isError).not.toBe(true);
expect(calls[0]).toEqual({ operation: "inspect_plist", parameters: {} });
const selectedPath = "/tmp/Other.plist";
const explicit = await client.callTool({
name: "inspect_plist",
arguments: { path: selectedPath },
});
expect(explicit.isError).not.toBe(true);
expect(calls[1]).toEqual({
operation: "inspect_plist",
parameters: { path: selectedPath },
});
const beforeTypo = calls.length;
const rejected = await client.callTool({
name: "inspect_plist",
arguments: typoInput,
});
expect(rejected.isError).toBe(true);
expect(calls).toHaveLength(beforeTypo);
});
});
+48
View File
@@ -1,5 +1,6 @@
import { functionDossierSchema } from "../../../src/domain/hopperValues.js";
import { ghidraFunctionDossier } from "../../../src/domain/ghidraValues.fixture.js";
import { Ajv2020 } from "ajv/dist/2020.js";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { afterEach, describe, expect, it } from "vitest";
@@ -277,6 +278,53 @@ const outputFor = (name: string): JsonValue => {
return name.includes("address") ? "0x1000" : "fixture";
};
describe("official tool input contracts", () => {
it("rejects misspelled top-level inputs before dispatch and keeps record keys open", async () => {
const invocations: Invocation[] = [];
const client = await connect({
execute: (name, arguments_) => {
invocations.push({ name, arguments_ });
return Promise.resolve(ok(outputFor(name)));
},
});
const { tools } = await client.listTools();
const advertisedByName = new Map(tools.map((tool) => [tool.name, tool]));
for (const contract of OFFICIAL_TOOL_CONTRACTS) {
expect(advertisedByName.get(contract.name)?.inputSchema).toHaveProperty(
"additionalProperties",
false,
);
}
const advertised = tools.find(({ name }) => name === "address_name");
const recordTool = tools.find(({ name }) => name === "set_addresses_names");
if (advertised === undefined || recordTool === undefined)
throw new Error("Official tools were not advertised");
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const validateAddressName = ajv.compile(advertised.inputSchema);
const validateRecordInput = ajv.compile(recordTool.inputSchema);
const typoInput = { adress: "0x1234" };
expect(advertised.inputSchema).toHaveProperty(
"additionalProperties",
false,
);
expect(validateAddressName(typoInput)).toBe(false);
expect(
OFFICIAL_TOOL_CONTRACTS.find(
({ name }) => name === "address_name",
)?.inputSchema.safeParse(typoInput).success,
).toBe(false);
expect(validateRecordInput({ names: { "0x1000": "entry" } })).toBe(true);
const result = await client.callTool({
name: "address_name",
arguments: typoInput,
});
expect(result.isError).toBe(true);
expect(invocations).toEqual([]);
});
});
describe("official Hopper proxy tools", () => {
it("executes every handler and projects omitted Python optionals to null", async () => {
const invocations: Invocation[] = [];
@@ -28,6 +28,38 @@ function schemaErrors(tools: readonly ToolSchemas[]): string[] {
);
}
function expectStrictInputSchemaParity(
tools: readonly ToolSchemas[],
ajv: Ajv2020,
): void {
const advertised = new Map(tools.map((tool) => [tool.name, tool]));
const names = [
"inspect_managed_artifact",
"inspect_managed_members",
"inspect_managed_native_boundaries",
"list_browser_targets",
"open_binary",
"close_binary",
"binary_session",
"find_changed_behavior",
"build_call_path",
"record_unknown",
"update_unknown",
];
for (const name of names) {
const contract = TOOL_CONTRACTS.find(
({ name: toolName }) => toolName === name,
);
const tool = advertised.get(name);
const example = contract?.examples[0];
if (contract === undefined || tool === undefined || example === undefined)
throw new Error(`${name} did not have an advertised example`);
const malformed = { ...example.input, __unexpected_root_key__: true };
expect(contract.inputSchema.safeParse(malformed).success, name).toBe(false);
expect(ajv.compile(tool.inputSchema)(malformed), name).toBe(false);
}
}
const advertiseAndEnforceProcessEnvironmentKeyConstraint =
async (): Promise<void> => {
const contract = TOOL_CONTRACTS.find(
@@ -190,6 +222,8 @@ describe("MCP JSON Schema validity", () => {
).toBe(true);
}
expectStrictInputSchemaParity(tools, ajv);
const nativeObservation = TOOL_CONTRACTS.find(
({ name }) => name === "observe_native_ui",
);