Merge pull request #225 from DeryFerd/fix/mcp-arg-bounds

fix(mcp): bound string-list arguments and reject fractional count arguments
This commit is contained in:
Timo Zhang
2026-11-16 23:52:29 +08:00
committed by GitHub
3 changed files with 117 additions and 2 deletions
+2
View File
@@ -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.
+41 -2
View File
@@ -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) {
+74
View File
@@ -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<string, unknown>) {
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);
});
});