From 1401fbe167b2e73e706392cd2fbe3fa5f1aace4e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 3 Dec 2026 20:58:24 +0100 Subject: [PATCH] Fail-close VOIDBOT_MCP_TOOL_ALLOWLIST to non-Discord source retrieval --- apps/worker/src/mcp-server-tools.ts | 21 ++- scripts/test-mcp-tool-allowlist.ts | 203 ++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+), 6 deletions(-) create mode 100644 scripts/test-mcp-tool-allowlist.ts diff --git a/apps/worker/src/mcp-server-tools.ts b/apps/worker/src/mcp-server-tools.ts index 3c30942..fb08461 100644 --- a/apps/worker/src/mcp-server-tools.ts +++ b/apps/worker/src/mcp-server-tools.ts @@ -82,28 +82,37 @@ const READ_ONLY_ANNOTATIONS = { const DEFAULT_ODIN_STORE_PATH = path.resolve(process.cwd(), "..", "Odin", "scratch", "odin", "odin.ccmp"); const ODIN_SURFACE_KEY = "surface:gamecult.network.status"; -function isMcpToolAllowed(name: string): boolean { - const raw = process.env.VOIDBOT_MCP_TOOL_ALLOWLIST?.trim(); +export const DEFAULT_MCP_TOOL_ALLOWLIST: ReadonlySet = new Set([ + "list_indexed_repos", + "search_sources", + "get_source_context", + "get_exact_source_document", +]); + +export function resolveMcpToolAllowlist( + rawAllowlist: string | undefined = process.env.VOIDBOT_MCP_TOOL_ALLOWLIST, +): ReadonlySet { + const raw = rawAllowlist?.trim(); if (!raw) { - return true; + return DEFAULT_MCP_TOOL_ALLOWLIST; } - const allowed = new Set( + return new Set( raw .split(",") .map((entry) => entry.trim()) .filter(Boolean), ); - return allowed.has(name); } export function registerVoidbotTools( server: McpServer, context: VoidbotMcpContext, ): void { + const allowedTools = resolveMcpToolAllowlist(); const registerIfAllowed = (...args: any[]): void => { const [name] = args; - if (!isMcpToolAllowed(name)) { + if (!allowedTools.has(name)) { return; } (server.registerTool as any)(...args); diff --git a/scripts/test-mcp-tool-allowlist.ts b/scripts/test-mcp-tool-allowlist.ts new file mode 100644 index 0000000..6304c99 --- /dev/null +++ b/scripts/test-mcp-tool-allowlist.ts @@ -0,0 +1,203 @@ +import assert from "node:assert/strict"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; +import { + FileSourceDocumentArchiveRepository, + SourceDocumentIngester, +} from "@voidbot/rag"; + +import { type VoidbotMcpContext } from "../apps/worker/src/mcp-server-context"; +import { createVoidbotMcpServer } from "../apps/worker/src/mcp-server-factory"; +import { DEFAULT_MCP_TOOL_ALLOWLIST } from "../apps/worker/src/mcp-server-tools"; + +const EXPECTED_DEFAULT_TOOLS = [ + "get_exact_source_document", + "get_source_context", + "list_indexed_repos", + "search_sources", +].sort(); + +const BLOCKED_TOOL_CALLS: Array<{ name: string; arguments: Record }> = [ + { + name: "post_discord_message", + arguments: { channelId: "123", content: "should be refused" }, + }, + { + name: "post_repo_identity_message", + arguments: { identity: "nibu", channelId: "123", content: "should be refused" }, + }, + { + name: "notify_owner", + arguments: { message: "should be refused" }, + }, + { + name: "apply_repo_face_state_operation", + arguments: { identity: "nibu", operation: { type: "noop" } }, + }, + { + name: "create_shared_document", + arguments: { documentId: "doc", title: "Doc", body: "Body" }, + }, + { + name: "update_shared_document", + arguments: { documentId: "doc", title: "Doc", body: "Body", expectedVersion: 1 }, + }, + { + name: "invoke_odin_interface_command", + arguments: { providerId: "voidbot.swarm", command: "set_heat" }, + }, + { + name: "search_history", + arguments: { query: "discord taint check" }, + }, + { + name: "get_message_context", + arguments: { messageId: "999" }, + }, +]; + +async function withConnectedClient( + context: VoidbotMcpContext, + run: (client: Client) => Promise, +): Promise { + const server = createVoidbotMcpServer(context); + const client = new Client({ name: "mcp-allowlist-test", version: "1.0.0" }); + const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); + await Promise.all([server.connect(serverTransport), client.connect(clientTransport)]); + try { + await run(client); + } finally { + await client.close(); + await server.close(); + } +} + +async function assertToolRefused( + client: Client, + toolCall: { name: string; arguments: Record }, +): Promise { + let outcome: "threw" | "error_result" | "succeeded" = "succeeded"; + try { + const result = await client.callTool(toolCall); + if (result.isError) { + outcome = "error_result"; + } + } catch { + outcome = "threw"; + } + assert.notEqual( + outcome, + "succeeded", + `Expected unallowed MCP tool "${toolCall.name}" to be refused, but callTool succeeded.`, + ); +} + +async function assertDefaultFailClosedBehavior( + context: VoidbotMcpContext, + label: string, +): Promise { + await withConnectedClient(context, async (client) => { + const listed = await client.listTools(); + const names = listed.tools.map((tool) => tool.name).sort(); + assert.deepEqual( + names, + EXPECTED_DEFAULT_TOOLS, + `[${label}] Expected only non-Discord read-only source tools, got: ${names.join(", ")}`, + ); + + const exact = await client.callTool({ + name: "get_exact_source_document", + arguments: { sourceId: "ExampleRepo:README.md" }, + }); + assert.equal(exact.isError, undefined, `[${label}] Allowed source tool should succeed`); + assert.equal( + (exact.structuredContent as Record | undefined)?.content, + "Read-only source witness.", + ); + + for (const blocked of BLOCKED_TOOL_CALLS) { + await assertToolRefused(client, blocked); + } + }); +} + +async function main(): Promise { + const originalAllowlist = process.env.VOIDBOT_MCP_TOOL_ALLOWLIST; + const root = await mkdtemp(join(tmpdir(), "voidbot-mcp-allowlist-test-")); + + try { + assert.deepEqual( + [...DEFAULT_MCP_TOOL_ALLOWLIST].sort(), + EXPECTED_DEFAULT_TOOLS, + "DEFAULT_MCP_TOOL_ALLOWLIST must match expected non-Discord read-only source tools", + ); + + const sources = new FileSourceDocumentArchiveRepository(join(root, "sources.json")); + await sources.syncRepoDocuments("ExampleRepo", [ + { + id: "ExampleRepo:README.md", + repoName: "ExampleRepo", + path: "README.md", + content: "Read-only source witness.", + language: "markdown", + title: "Example README", + }, + ]); + + const context = { + sourceArchiveRepository: sources, + sourceDocumentIngester: new SourceDocumentIngester(), + } as VoidbotMcpContext; + + delete process.env.VOIDBOT_MCP_TOOL_ALLOWLIST; + await assertDefaultFailClosedBehavior(context, "unset"); + + process.env.VOIDBOT_MCP_TOOL_ALLOWLIST = ""; + await assertDefaultFailClosedBehavior(context, "empty"); + + process.env.VOIDBOT_MCP_TOOL_ALLOWLIST = " "; + await assertDefaultFailClosedBehavior(context, "whitespace"); + + process.env.VOIDBOT_MCP_TOOL_ALLOWLIST = ","; + await withConnectedClient(context, async (client) => { + const listed = await client.listTools(); + assert.deepEqual(listed.tools.map((tool) => tool.name), [], "[comma-only] Expected 0 tools"); + await assertToolRefused(client, { + name: "get_exact_source_document", + arguments: { sourceId: "ExampleRepo:README.md" }, + }); + }); + + process.env.VOIDBOT_MCP_TOOL_ALLOWLIST = "list_indexed_repos,get_exact_source_document"; + await withConnectedClient(context, async (client) => { + const listed = await client.listTools(); + assert.deepEqual( + listed.tools.map((tool) => tool.name).sort(), + ["get_exact_source_document", "list_indexed_repos"], + "[explicit] Expected only explicitly allowlisted tools", + ); + await assertToolRefused(client, { + name: "search_sources", + arguments: { query: "witness" }, + }); + }); + + console.log("MCP tool allowlist fail-closed behavioral tests passed (5 scenarios)."); + } finally { + if (originalAllowlist === undefined) { + delete process.env.VOIDBOT_MCP_TOOL_ALLOWLIST; + } else { + process.env.VOIDBOT_MCP_TOOL_ALLOWLIST = originalAllowlist; + } + await rm(root, { recursive: true, force: true }); + } +} + +void main().catch((error) => { + console.error(error); + process.exitCode = 1; +});