From 5e96f8f1dd5a9bc275af0d70a931488a3a09845b Mon Sep 17 00:00:00 2001 From: DeryFerd Date: Tue, 17 Nov 2026 00:32:57 +0700 Subject: [PATCH] fix(mcp): honor bla_inspect_release max_files / max_total_bytes bounds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tool schema advertised optional max_files and max_total_bytes caps, but the handler passed only the path and inspectReleaseExposure had no options parameter at all — the bounds were dead code and an MCP client could trigger an unbounded scan. Plumb both caps through as optional overrides that only tighten the built-in limits, and stop scanning once the file-count cap is reached (limitation: file-count-limit). --- src/core/release-exposure.ts | 25 +++++++++++++++++++++++-- src/mcp/server.ts | 7 ++++++- tests/mcp-param-alignment.test.ts | 20 ++++++++++++++++++++ tests/release-exposure.test.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/core/release-exposure.ts b/src/core/release-exposure.ts index 469cb98..384b784 100644 --- a/src/core/release-exposure.ts +++ b/src/core/release-exposure.ts @@ -58,11 +58,28 @@ function isSameRealPath(path: string): boolean { } /** Read-only bounded inspection of a stable local release tree. */ -export function inspectReleaseExposure(target: string): ReleaseExposureReport { +export function inspectReleaseExposure( + target: string, + options: { readonly maxFiles?: number; readonly maxTotalBytes?: number } = {}, +): ReleaseExposureReport { const root = resolve(target); if (!isSameRealPath(root) || lstatSync(root).isSymbolicLink()) { throw new Error("Release inspection requires a non-symlink target path and ancestors."); } + // Caller-supplied caps (MCP max_files / max_total_bytes) may only tighten the + // built-in limits — they never widen them. + const maxFiles = + typeof options.maxFiles === "number" && + Number.isFinite(options.maxFiles) && + options.maxFiles > 0 + ? Math.floor(options.maxFiles) + : Number.POSITIVE_INFINITY; + const maxTotalBytes = + typeof options.maxTotalBytes === "number" && + Number.isFinite(options.maxTotalBytes) && + options.maxTotalBytes > 0 + ? Math.min(Math.floor(options.maxTotalBytes), MAX_TOTAL_BYTES) + : MAX_TOTAL_BYTES; const report: ReleaseExposureReport = { evidenceBasis: "static-file-observation", status: "complete", @@ -147,7 +164,11 @@ export function inspectReleaseExposure(target: string): ReleaseExposureReport { limit(path, "non-regular-file"); return; } - if (stat.size > MAX_FILE_BYTES || report.bytesRead + stat.size + 1 > MAX_TOTAL_BYTES) { + if (report.filesInspected >= maxFiles) { + limit(path, "file-count-limit"); + return; + } + if (stat.size > MAX_FILE_BYTES || report.bytesRead + stat.size + 1 > maxTotalBytes) { limit(path, "read-size-limit"); return; } diff --git a/src/mcp/server.ts b/src/mcp/server.ts index 9d8548e..fbab285 100644 --- a/src/mcp/server.ts +++ b/src/mcp/server.ts @@ -2480,7 +2480,12 @@ export async function handleMcpRequest( typeof args.home_dir === "string" ? args.home_dir : undefined, ); } else if (name === "bla_inspect_release") { - payload = handlers.inspectReleaseExposure(args.path as string); + const maxFiles = optionalCount(args.max_files); + const maxTotalBytes = optionalCount(args.max_total_bytes); + payload = handlers.inspectReleaseExposure(args.path as string, { + ...(maxFiles !== undefined ? { maxFiles } : {}), + ...(maxTotalBytes !== undefined ? { maxTotalBytes } : {}), + }); } else if (name === "bla_init") { payload = handlers.initBlaConfig( args.directory as string, diff --git a/tests/mcp-param-alignment.test.ts b/tests/mcp-param-alignment.test.ts index 496eafd..fc5c8b6 100644 --- a/tests/mcp-param-alignment.test.ts +++ b/tests/mcp-param-alignment.test.ts @@ -177,3 +177,23 @@ describe("MCP bla_verify honesty metadata passthrough", () => { ); }); }); + +describe("MCP bla_inspect_release bounds forwarding", () => { + it("forwards max_files / max_total_bytes to the engine scan", async () => { + const dir = mkdtempSync(join(tmpdir(), "bla-inspect-bounds-")); + tempDirs.push(dir); + for (const name of ["a.js", "b.js", "c.js"]) { + writeFileSync(join(dir, name), "const x = 1;\n", "utf8"); + } + const full = await call("bla_inspect_release", { path: dir }); + expect(full.isError).toBe(false); + expect(full.structuredContent.filesInspected).toBe(3); + const capped = await call("bla_inspect_release", { + path: dir, + max_files: 1, + }); + expect(capped.isError).toBe(false); + expect(capped.structuredContent.filesInspected).toBe(1); + expect(capped.structuredContent.status).toBe("partial"); + }); +}); diff --git a/tests/release-exposure.test.ts b/tests/release-exposure.test.ts index 8d17a92..08f87bc 100644 --- a/tests/release-exposure.test.ts +++ b/tests/release-exposure.test.ts @@ -208,4 +208,32 @@ describe("read-only release exposure observations", () => { writeFileSync(file, "invalid"); expect(await runCli(["inspect-release", file])).toBe(2); }); + + it("honors the optional maxFiles bound", () => { + const dir = temp(); + for (const name of ["a.js", "b.js", "c.js"]) { + writeFileSync(join(dir, name), "const x = 1;\n"); + } + expect(inspectReleaseExposure(dir).filesInspected).toBe(3); + const capped = inspectReleaseExposure(dir, { maxFiles: 1 }); + expect(capped.filesInspected).toBe(1); + expect(capped.status).toBe("partial"); + expect(capped.limitations.some((l) => l.reason === "file-count-limit")).toBe( + true, + ); + }); + + it("honors the optional maxTotalBytes bound", () => { + const dir = temp(); + const body = `//# sourceMappingURL=x.map;${"x".repeat(600)}`; + writeFileSync(join(dir, "big1.js"), body); + writeFileSync(join(dir, "big2.js"), body); + const capped = inspectReleaseExposure(dir, { maxTotalBytes: 700 }); + expect(capped.filesInspected).toBe(1); + expect(capped.bytesRead).toBeLessThanOrEqual(700); + expect(capped.status).toBe("partial"); + expect(capped.limitations.some((l) => l.reason === "read-size-limit")).toBe( + true, + ); + }); });