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
@@ -43,7 +43,6 @@ const input = (
overrides: Partial<RecordUnknownInput> = {},
): RecordUnknownInput =>
recordUnknownInputSchema.parse({
approved: true,
question,
severity: "high",
domain: "protocol",
@@ -60,7 +59,6 @@ const update = (
overrides: Readonly<Record<string, unknown>> = {},
) =>
updateUnknownInputSchema.parse({
approved: true,
unknown_id: unknown.unknown_id,
expected_revision: unknown.revision,
status: "investigating",
@@ -129,7 +129,6 @@ describe("evidence ledger metadata imports", () => {
});
const created = ledger.recordUnknown(
recordUnknownInputSchema.parse({
approved: true,
question: "Which path is current?",
severity: "high",
domain: "fixture",
@@ -241,7 +240,6 @@ describe("evidence bundle imports", () => {
result: { action: "record" },
});
const unknown = recordUnknownInputSchema.parse({
approved: true,
question: "What remains unresolved?",
severity: "high",
domain: "atomic-test",
@@ -101,9 +101,18 @@ describe("investigation record ownership", () => {
);
if (!created.ok) throw created.error;
const input = updateUnknownInputSchema.parse({
...created.value,
unknown_id: created.value.unknown_id,
expected_revision: created.value.revision,
status: "investigating",
severity: created.value.severity,
supporting_evidence_ids: created.value.supporting_evidence_ids,
contradicting_evidence_ids: created.value.contradicting_evidence_ids,
required_authority: created.value.required_authority,
required_confidence: created.value.required_confidence,
required_environment: created.value.required_environment,
recommended_probes: created.value.recommended_probes,
relationships: created.value.relationships,
resolution: created.value.resolution,
});
const updated = records.updateUnknown(input, target);
expect(updated).toMatchObject({
@@ -7,7 +7,7 @@ import { requireOutputSchema } from "../toolOutputSchemaPrimitives.js";
import { isAbsoluteLocalPath } from "../../domain/localPath.js";
/** Exact caller boundary for execution-free PE/CLI triage and identity. */
const managedTargetInputSchema = z.object({
const managedTargetInputSchema = z.strictObject({
path: z
.string()
.min(1)
+3 -2
View File
@@ -30,12 +30,13 @@ const native = <Name extends string, Schema extends z.ZodObject>(
inputSchema: Schema,
) => {
const outputSchema = requireOutputSchema(nativeOutputSchemas, name);
const strictInputSchema = inputSchema.strict();
return {
name,
...toolContractMetadata(name),
description,
kind: "native-provider",
inputSchema,
inputSchema: strictInputSchema,
outputSchema,
examples: [
{
@@ -45,7 +46,7 @@ const native = <Name extends string, Schema extends z.ZodObject>(
.parse(examples[name] ?? {}),
},
],
} satisfies ToolContract<Name, Schema, typeof outputSchema>;
} satisfies ToolContract<Name, typeof strictInputSchema, typeof outputSchema>;
};
/** Ordered single-line symbols; `swift-demangle` ends each result with LF. */
+7 -5
View File
@@ -24,16 +24,18 @@ const official = <Name extends string, Schema extends z.ZodObject>(
name: Name,
description: string,
inputSchema: Schema,
) =>
({
) => {
const strictInputSchema = inputSchema.strict();
return {
name,
...toolContractMetadata(name),
description,
kind: "official-proxy",
inputSchema,
inputSchema: strictInputSchema,
outputSchema: requireOutputSchema(officialOutputSchemas, name),
examples: examplesFor(name, inputSchema),
}) satisfies ToolContract<Name, Schema>;
examples: examplesFor(name, strictInputSchema),
} satisfies ToolContract<Name, typeof strictInputSchema>;
};
/** Bridge operations exposed without additional application composition. */
export const OFFICIAL_TOOL_CONTRACTS = [
+2 -2
View File
@@ -28,7 +28,7 @@ const binaryTargetPathSchema = z
);
/** Input contract for opening a target with an optional staged snapshot. */
export const openBinaryInputSchema = z.object({
export const openBinaryInputSchema = z.strictObject({
path: binaryTargetPathSchema,
format: executableFormatHintSchema
.optional()
@@ -40,7 +40,7 @@ export const openBinaryInputSchema = z.object({
});
/** Input contract for closing a target after an optional atomic snapshot. */
export const closeBinaryInputSchema = z.object({
export const closeBinaryInputSchema = z.strictObject({
snapshot_path: snapshotPathSchema.optional(),
overwrite: z.boolean().default(false),
});
+1 -1
View File
@@ -3,7 +3,7 @@ import { z } from "zod";
import { isAbsoluteLocalPath } from "../domain/localPath.js";
/** Optional expectations used to compare the live session with its caller. */
export const binarySessionInputSchema = z.object({
export const binarySessionInputSchema = z.strictObject({
expected_package_version: z.string().min(1).optional(),
expected_catalog_digest: z
.string()
+1 -1
View File
@@ -94,7 +94,7 @@ const browserInput = {
};
/** Public input for complete discovery, optionally filtered by exact origin. */
export const listBrowserTargetsInputSchema = z.object({
export const listBrowserTargetsInputSchema = z.strictObject({
...browserInput,
});
+1 -1
View File
@@ -24,7 +24,7 @@ export const parseCallPathAddress = (input: unknown): string =>
inputAddressSchema.parse(input);
/** Strict input for explicit call-path reconstruction from complete dossiers. */
export const callPathInputSchema = z.object({
export const callPathInputSchema = z.strictObject({
functions: z.array(evidenceSchema).min(1),
start: z.object({ address: inputAddressSchema }).strict(),
goal: z.object({ address: inputAddressSchema }).strict(),
+1 -1
View File
@@ -19,7 +19,7 @@ import { prefixedDigestSchema } from "./../domain/digests.js";
const evidenceIdSchema = prefixedDigestSchema("ev");
/** Input for aggregating existing comparison Evidence. */
export const changedBehaviorInputSchema = z.object({
export const changedBehaviorInputSchema = z.strictObject({
comparisons: z.array(evidenceSchema).default([]),
});
-2
View File
@@ -21,7 +21,6 @@ const makeUnknown = (
dependency?: string,
) => {
const input = recordUnknownInputSchema.parse({
approved: true,
question,
severity: "medium",
domain: "bundle-projection",
@@ -95,7 +94,6 @@ describe("evidenceBundleForTarget", () => {
const unknowns = [foreignRoot.unknown, middle.unknown];
for (let index = 0; index < 125_000; index += 1) {
const input = recordUnknownInputSchema.parse({
approved: true,
question: `Target child ${index}`,
severity: "medium",
domain: "bundle-projection",
+3 -2
View File
@@ -167,12 +167,13 @@ export const recordUnknownInputSchema = residualUnknownObjectSchema
.extend({
supporting_evidence_ids: z.array(evidenceIdSchema).default([]),
contradicting_evidence_ids: z.array(evidenceIdSchema).default([]),
});
})
.strict();
export type RecordUnknownInput = z.infer<typeof recordUnknownInputSchema>;
/** Explicit optimistic-concurrency update command for one unknown. */
export const updateUnknownInputSchema = z.object({
export const updateUnknownInputSchema = z.strictObject({
unknown_id: unknownIdSchema,
expected_revision: z.number().int().min(1),
status: residualUnknownObjectSchema.shape.status,
@@ -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",
);