From 15bb942b3968f05ffa8cb85c123854d75dfa3334 Mon Sep 17 00:00:00 2001 From: DeryFerd Date: Sun, 15 Nov 2026 22:31:52 +0700 Subject: [PATCH] fix(mcp): bound string-list arguments and reject fractional count arguments validateMcpToolCall checked array items but never their count, and accepted 1.5 for count arguments, so a single tools/call could hand the engine an unbounded list or a fractional cap. String lists (wasm_preserve_exports, domains) are now capped at 1024 items and count fields (max_artifacts, max_files, max_total_bytes, max_targets, offset) declare integer: true, which the validator enforces with the existing -32602 contract. Fractional drift thresholds stay accepted: same_threshold / different_threshold are drift scores, not counts, and rejecting them would refuse a legitimate call. --- docs/cli-and-mcp.md | 2 + src/mcp/server.ts | 43 ++++++++++++++++++++- tests/mcp-arg-bounds.test.ts | 74 ++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 tests/mcp-arg-bounds.test.ts diff --git a/docs/cli-and-mcp.md b/docs/cli-and-mcp.md index 53e1a77..91676ef 100644 --- a/docs/cli-and-mcp.md +++ b/docs/cli-and-mcp.md @@ -49,6 +49,8 @@ manifest. ## MCP Tools (`bla` Server) +Argument bounds are enforced before any tool runs: count arguments (`max_artifacts`, `max_files`, `max_total_bytes`, `max_targets`, `offset`) must be integers, and string-list arguments (`wasm_preserve_exports`, `domains`) hold at most 1024 items. A violation is a `-32602` error naming the argument, and fractional drift thresholds (`same_threshold` / `different_threshold`) stay allowed because drift scores are not counts. + - `bla_harden`: Preferred one-sentence harden of a project directory (`directory`, `intensity`, `emit_manifest` default false, `llm_directives` default false, `weave_decoys` default false, `self_seal` default false, `wasm_preserve_exports`, `allow_lane_independent`, `allow_plaintext_seal`, `allow_wasm_clear_params`, `allow_stable_panel_derive`, `seed`, `dry_run` default false, `max_artifacts` default 64, `confirm_overwrite` default false, `output_directory` optional). The report carries `dryRun`, `truncated`, `maxArtifacts`, `wouldOverwrite`, and `outputDirectory`. - `bla_discover`: Read-only inventory using the same discovery as `bla_harden` (`directory`, `max_artifacts`, `offset`) → `{ directory, artifacts[{path, relativePath, format}], truncated, maxArtifacts, offset, nextOffset? }`. When `truncated`, pass `nextOffset` as the next `offset` to page 64+ trees without losing the tail. Writes nothing. - `bla_init`: Writes a starter `bla.config.json` with a fresh per-project seed (`directory`, `overwrite` default false) so zero-argument `bla defend` has a config. Writes one file only. diff --git a/src/mcp/server.ts b/src/mcp/server.ts index 9276708..81ffb98 100644 --- a/src/mcp/server.ts +++ b/src/mcp/server.ts @@ -142,6 +142,10 @@ export interface McpPropertySchema { readonly enum?: readonly string[]; /** Present when type is "array" (JSON Schema items). */ readonly items?: { readonly type: "string" | "boolean" | "number" }; + /** Present when type is "array": upper bound on item count (JSON Schema `maxItems`). */ + readonly maxItems?: number; + /** Present for count-like numbers: reject fractional values. */ + readonly integer?: boolean; } export interface McpToolAnnotations { @@ -229,6 +233,14 @@ function withToolAnnotations(tool: McpToolDefinition): McpToolDefinition { }; } +/** + * Upper bound for string-array tool arguments. Every array argument is a short + * user-authored list (WASM export names, allowed hostnames, glob patterns), so + * the cap is far above real use while still refusing an unbounded or buggy + * client the ability to push one call into unbounded work. + */ +export const MAX_STRING_ARRAY_ITEMS = 1024; + const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ { name: "bla_harden", @@ -285,6 +297,7 @@ const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ wasm_preserve_exports: { type: "array", items: { type: "string" }, + maxItems: MAX_STRING_ARRAY_ITEMS, description: "WASM export names to keep as the public ABI. At maximum/bankrupt every other export is renamed by default — pass explicitly to keep mix/seal-style names (issue #40.3 / red-blue round-1).", }, @@ -349,6 +362,7 @@ const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ }, max_artifacts: { type: "number", + integer: true, description: "Optional cap on discovered artifacts (default 64); the report sets truncated=true when more exist.", }, confirm_overwrite: { @@ -378,10 +392,12 @@ const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ }, max_artifacts: { type: "number", + integer: true, description: "Optional page size / cap (default 64); truncated=true when more exist beyond this page.", }, offset: { type: "number", + integer: true, description: "Zero-based offset into the stable discovery order (use prior nextOffset to page). Default 0.", }, }, @@ -447,6 +463,7 @@ const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ wasm_preserve_exports: { type: "array", items: { type: "string" }, + maxItems: MAX_STRING_ARRAY_ITEMS, description: "WASM export names to keep as the public ABI. At maximum/bankrupt every other export is renamed by default — pass explicitly to keep mix/seal-style names (issue #40.3 / red-blue round-1).", }, @@ -573,6 +590,7 @@ const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ wasm_preserve_exports: { type: "array", items: { type: "string" }, + maxItems: MAX_STRING_ARRAY_ITEMS, description: "WASM export names to keep as the public ABI. At maximum/bankrupt every other export is renamed by default — pass explicitly to keep mix/seal-style names (issue #40.3 / red-blue round-1).", }, @@ -778,10 +796,12 @@ const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ }, max_files: { type: "number", + integer: true, description: "Optional maximum number of files to scan", }, max_total_bytes: { type: "number", + integer: true, description: "Optional maximum total bytes to scan", }, }, @@ -822,6 +842,7 @@ const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ }, max_targets: { type: "number", + integer: true, description: "Optional cap on files to classify (default 48)", }, }, @@ -842,6 +863,7 @@ const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ }, max_targets: { type: "number", + integer: true, description: "Optional cap on files to verify (default 48)", }, }, @@ -1223,6 +1245,7 @@ const RAW_BLA_MCP_TOOLS: readonly McpToolDefinition[] = [ domains: { type: "array", items: { type: "string" }, + maxItems: MAX_STRING_ARRAY_ITEMS, description: "Allowed hostnames or *.suffix globs (public)", }, valid_from: { @@ -1841,11 +1864,19 @@ export function validateMcpToolCall( } else if (propSchema.type === "number") { const allowZero = key === "offset" || key.endsWith("_threshold") || key === "fail_under"; - if (typeof val !== "number" || !Number.isFinite(val) || (allowZero ? val < 0 : val <= 0)) { + const integerOnly = propSchema.integer === true; + if ( + typeof val !== "number" || + !Number.isFinite(val) || + (integerOnly && !Number.isInteger(val)) || + (allowZero ? val < 0 : val <= 0) + ) { return { ok: false, code: -32602, - message: `Invalid params for '${tool.name}': argument '${key}' must be a ${allowZero ? "non-negative" : "positive"} finite number`, + message: `Invalid params for '${tool.name}': argument '${key}' must be a ${allowZero ? "non-negative" : "positive"} ${ + integerOnly ? "integer" : "finite number" + }`, }; } } else if (propSchema.type === "array") { @@ -1856,6 +1887,14 @@ export function validateMcpToolCall( message: `Invalid params for '${tool.name}': argument '${key}' must be an array, received ${val === null ? "null" : typeof val}`, }; } + const maxItems = propSchema.maxItems ?? MAX_STRING_ARRAY_ITEMS; + if (val.length > maxItems) { + return { + ok: false, + code: -32602, + message: `Invalid params for '${tool.name}': argument '${key}' must hold at most ${maxItems} items, received ${val.length}`, + }; + } if (propSchema.items?.type === "string") { for (let i = 0; i < val.length; i++) { if (typeof val[i] !== "string" || (val[i] as string).trim().length === 0) { diff --git a/tests/mcp-arg-bounds.test.ts b/tests/mcp-arg-bounds.test.ts new file mode 100644 index 0000000..b4d3c84 --- /dev/null +++ b/tests/mcp-arg-bounds.test.ts @@ -0,0 +1,74 @@ +import { describe, expect, it } from "vitest"; +import { + MAX_STRING_ARRAY_ITEMS, + validateMcpToolCall, +} from "../src/mcp/server.js"; + +function validate(name: string, args: Record) { + return validateMcpToolCall({ name, arguments: args }); +} + +describe("MCP argument bounds", () => { + it("rejects a string array larger than the declared cap", () => { + const oversized = Array.from( + { length: MAX_STRING_ARRAY_ITEMS + 1 }, + (_, i) => `export_${i}`, + ); + const res = validate("bla_harden", { + directory: ".", + wasm_preserve_exports: oversized, + }); + expect(res.ok).toBe(false); + if (res.ok) return; + expect(res.code).toBe(-32602); + expect(res.message).toContain(`at most ${MAX_STRING_ARRAY_ITEMS}`); + }); + + it("accepts a string array exactly at the cap", () => { + const atCap = Array.from( + { length: MAX_STRING_ARRAY_ITEMS }, + (_, i) => `export_${i}`, + ); + expect( + validate("bla_harden", { directory: ".", wasm_preserve_exports: atCap }).ok, + ).toBe(true); + }); + + it("bounds the domains list on bla_bind_scope too", () => { + const domains = Array.from( + { length: MAX_STRING_ARRAY_ITEMS + 1 }, + (_, i) => `host${i}.example.com`, + ); + const res = validate("bla_bind_scope", { directory: ".", domains }); + expect(res.ok).toBe(false); + if (res.ok) return; + expect(res.message).toContain(`at most ${MAX_STRING_ARRAY_ITEMS}`); + }); + + it("rejects fractional count arguments", () => { + const res = validate("bla_harden", { directory: ".", max_artifacts: 1.5 }); + expect(res.ok).toBe(false); + if (res.ok) return; + expect(res.code).toBe(-32602); + expect(res.message).toContain("integer"); + }); + + it("still accepts integer counts and a zero offset", () => { + expect( + validate("bla_harden", { directory: ".", max_artifacts: 8 }).ok, + ).toBe(true); + expect( + validate("bla_discover", { directory: ".", max_artifacts: 8, offset: 0 }).ok, + ).toBe(true); + }); + + it("keeps fractional drift thresholds accepted", () => { + const res = validate("bla_polymorph_report", { + dir_a: "a", + dir_b: "b", + different_threshold: 0.25, + same_threshold: 0.01, + }); + expect(res.ok).toBe(true); + }); +}); \ No newline at end of file