Merge pull request #240 from DeryFerd/fix/mcp-inspect-release-bounds

fix(mcp): honor bla_inspect_release max_files / max_total_bytes bounds
This commit is contained in:
Timo Zhang
2026-11-17 11:46:33 +08:00
committed by GitHub
4 changed files with 77 additions and 3 deletions
+23 -2
View File
@@ -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;
}
+6 -1
View File
@@ -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,
+20
View File
@@ -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");
});
});
+28
View File
@@ -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,
);
});
});