Fail-close VOIDBOT_MCP_TOOL_ALLOWLIST to non-Discord source retrieval
This commit is contained in:
parent
18e1461017
commit
1401fbe167
2 changed files with 218 additions and 6 deletions
|
|
@ -82,28 +82,37 @@ const READ_ONLY_ANNOTATIONS = {
|
||||||
const DEFAULT_ODIN_STORE_PATH = path.resolve(process.cwd(), "..", "Odin", "scratch", "odin", "odin.ccmp");
|
const DEFAULT_ODIN_STORE_PATH = path.resolve(process.cwd(), "..", "Odin", "scratch", "odin", "odin.ccmp");
|
||||||
const ODIN_SURFACE_KEY = "surface:gamecult.network.status";
|
const ODIN_SURFACE_KEY = "surface:gamecult.network.status";
|
||||||
|
|
||||||
function isMcpToolAllowed(name: string): boolean {
|
export const DEFAULT_MCP_TOOL_ALLOWLIST: ReadonlySet<string> = new Set([
|
||||||
const raw = process.env.VOIDBOT_MCP_TOOL_ALLOWLIST?.trim();
|
"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<string> {
|
||||||
|
const raw = rawAllowlist?.trim();
|
||||||
if (!raw) {
|
if (!raw) {
|
||||||
return true;
|
return DEFAULT_MCP_TOOL_ALLOWLIST;
|
||||||
}
|
}
|
||||||
|
|
||||||
const allowed = new Set(
|
return new Set(
|
||||||
raw
|
raw
|
||||||
.split(",")
|
.split(",")
|
||||||
.map((entry) => entry.trim())
|
.map((entry) => entry.trim())
|
||||||
.filter(Boolean),
|
.filter(Boolean),
|
||||||
);
|
);
|
||||||
return allowed.has(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function registerVoidbotTools(
|
export function registerVoidbotTools(
|
||||||
server: McpServer,
|
server: McpServer,
|
||||||
context: VoidbotMcpContext,
|
context: VoidbotMcpContext,
|
||||||
): void {
|
): void {
|
||||||
|
const allowedTools = resolveMcpToolAllowlist();
|
||||||
const registerIfAllowed = (...args: any[]): void => {
|
const registerIfAllowed = (...args: any[]): void => {
|
||||||
const [name] = args;
|
const [name] = args;
|
||||||
if (!isMcpToolAllowed(name)) {
|
if (!allowedTools.has(name)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
(server.registerTool as any)(...args);
|
(server.registerTool as any)(...args);
|
||||||
|
|
|
||||||
203
scripts/test-mcp-tool-allowlist.ts
Normal file
203
scripts/test-mcp-tool-allowlist.ts
Normal file
|
|
@ -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<string, unknown> }> = [
|
||||||
|
{
|
||||||
|
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<void>,
|
||||||
|
): Promise<void> {
|
||||||
|
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<string, unknown> },
|
||||||
|
): Promise<void> {
|
||||||
|
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<void> {
|
||||||
|
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<string, unknown> | undefined)?.content,
|
||||||
|
"Read-only source witness.",
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const blocked of BLOCKED_TOOL_CALLS) {
|
||||||
|
await assertToolRefused(client, blocked);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main(): Promise<void> {
|
||||||
|
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;
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue