From efa2f54a5b7fe762193d341677ea2f98b86b9a1a Mon Sep 17 00:00:00 2001 From: Afloat Date: Thu, 19 Nov 2026 02:22:04 +0800 Subject: [PATCH 1/2] fix: validate MCP initialization session metadata --- src/mcp.ts | 5 +- tests/mcp-initialize-metadata.test.ts | 151 ++++++++++++++++++++++++++ 2 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 tests/mcp-initialize-metadata.test.ts diff --git a/src/mcp.ts b/src/mcp.ts index 7f1f3c21a5..2b5e2dbc36 100644 --- a/src/mcp.ts +++ b/src/mcp.ts @@ -350,7 +350,7 @@ async function handleMessage(msg: { id?: JsonRpcId; method?: string; params?: { - _meta?: { ui?: { sessionId?: string }; threadId?: unknown; "claudecode/toolUseId"?: unknown }; + _meta?: { ui?: { sessionId?: unknown }; threadId?: unknown; "claudecode/toolUseId"?: unknown }; [k: string]: unknown; }; }): Promise { @@ -365,7 +365,8 @@ async function handleMessage(msg: { // same time — registering here is still the earliest we can be. // Identity-mode registrations bind on any later request, so the // race only matters for headless mode. - const fromMeta = params._meta?.ui?.sessionId?.trim(); + const metaSessionId = params._meta?.ui?.sessionId; + const fromMeta = typeof metaSessionId === "string" ? metaSessionId.trim() : undefined; if (fromMeta) conversationId ??= fromMeta; initialized = true; if (conversationId && !registeredConversations.has(conversationId)) { diff --git a/tests/mcp-initialize-metadata.test.ts b/tests/mcp-initialize-metadata.test.ts new file mode 100644 index 0000000000..1c394acdf4 --- /dev/null +++ b/tests/mcp-initialize-metadata.test.ts @@ -0,0 +1,151 @@ +import assert from "node:assert/strict"; +import { spawn } from "node:child_process"; +import { once } from "node:events"; +import http from "node:http"; +import { createInterface } from "node:readline"; +import { fileURLToPath } from "node:url"; +import { test, type TestContext } from "node:test"; + +type Reply = { + id: number; + result?: { + protocolVersion?: string; + tools?: Array<{ name: string }>; + content?: Array<{ type: string; text: string }>; + isError?: boolean; + }; +}; + +type Options = { + meta?: Record; + envId?: string | null; + callMeta?: Record; + perCall?: string; + expectedId: string; + expectedRegistration?: string | null; + native?: boolean; +}; + +async function exercise(t: TestContext, options: Options): Promise { + const requests: Array<{ path: string; body?: Record }> = []; + const server = http.createServer(async (req, res) => { + let text = ""; + for await (const chunk of req) text += chunk; + const body = text ? JSON.parse(text) as Record : undefined; + requests.push({ path: req.url ?? "", body }); + res.setHeader("content-type", "application/json"); + if (req.url === "/__bili/plugin/manifest") { + res.end(JSON.stringify({ tools: { anthropic: [{ name: "acp_status", input_schema: { type: "object" } }] } })); + } else if (req.url === "/__bili/plugin/tool") { + res.end(JSON.stringify({ ok: true, result: `status for ${body?.conversationId}`, outcome: "success" })); + } else { + res.end(JSON.stringify({ ok: true })); + } + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address === "object"); + const env: NodeJS.ProcessEnv = { ...process.env, BILI_MCP_PROXY: `http://127.0.0.1:${address.port}` }; + delete env.CLAUDE_CODE_SESSION_ID; + delete env.BILI_CONVERSATION_ID; + const envId = options.envId === undefined ? "stable-env-session" : options.envId; + if (envId !== null) env.BILI_CONVERSATION_ID = envId; + const child = spawn(process.execPath, ["--import", "tsx", fileURLToPath(new URL("../src/mcp.ts", import.meta.url))], { env, stdio: "pipe" }); + const closed = new Promise((resolve) => child.once("close", () => resolve())); + let stderr = ""; + child.stderr.setEncoding("utf8"); + child.stderr.on("data", (text: string) => { stderr += text; }); + const lines = createInterface({ input: child.stdout }); + const iterator = lines[Symbol.asyncIterator](); + t.after(async () => { + if (child.exitCode === null) child.kill(); + await closed; + lines.close(); + server.closeAllConnections(); + await new Promise((resolve) => server.close(() => resolve())); + }); + const call = async (id: number, method: string, params?: Record): Promise => { + child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, ...(params ? { params } : {}) }) + "\n"); + let timer: ReturnType | undefined; + try { + const line = await Promise.race([ + iterator.next(), + new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(`MCP ${method} response timeout: ${stderr}`)), 10_000); + }), + ]); + assert.equal(line.done, false, `MCP closed before ${method} response: ${stderr}`); + const reply = JSON.parse(line.value) as Reply; + assert.equal(reply.id, id); + return reply; + } finally { + clearTimeout(timer); + } + }; + + const initialized = await call(0, "initialize", { + protocolVersion: "2025-06-18", + capabilities: {}, + clientInfo: { name: "metadata-test", version: "1" }, + ...(options.meta === undefined ? {} : { _meta: options.meta }), + }); + assert.equal(initialized.result?.protocolVersion, "2025-06-18"); + child.stdin.write(JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" }) + "\n"); + assert.deepEqual((await call(1, "ping")).result, {}); + assert.equal((await call(2, "tools/list")).result?.tools?.[0]?.name, "acp_status"); + const result = await call(3, "tools/call", { + name: "acp_status", + arguments: { detail: "unchanged", ...(options.perCall ? { conversation_id: options.perCall } : {}) }, + ...(options.callMeta ? { _meta: options.callMeta } : {}), + }); + assert.equal(result.result?.isError, false); + assert.equal(result.result?.content?.[0]?.text, `status for ${options.expectedId}`); + const posts = requests.filter((r) => r.path === "/__bili/plugin/tool"); + assert.equal(posts.length, 1); + assert.equal(posts[0].body?.conversationId, options.expectedId); + assert.deepEqual(posts[0].body?.args, { detail: "unchanged" }); + assert.equal(posts[0].body?.nativeCaller, options.native ? true : undefined); + const registers = requests.filter((r) => r.path === "/__bili/plugin/register"); + const registered = options.expectedRegistration === undefined ? "stable-env-session" : options.expectedRegistration; + assert.equal(registers.length, registered === null ? 0 : 1); + if (registered !== null) assert.deepEqual(registers[0].body, { conversationId: registered, agent: "mcp", identity: false }); + assert.equal(child.exitCode, null, stderr); +} + +for (const [name, sessionId] of [ + ["number", 42], ["zero", 0], ["boolean", false], ["array", ["other-session"]], ["object", { id: "other-session" }], +] as const) { + test(`MCP initialize ignores ${name} session metadata and keeps its tool service alive`, async (t) => { + await exercise(t, { meta: { ui: { sessionId } }, expectedId: "stable-env-session" }); + }); +} + +for (const [name, meta] of [ + ["missing metadata", undefined], ["null ui", { ui: null }], ["primitive ui", { ui: 42 }], + ["null session ID", { ui: { sessionId: null } }], ["blank session ID", { ui: { sessionId: " \t " } }], +] as const) { + test(`MCP initialize retains environment binding with ${name}`, async (t) => { + await exercise(t, { meta, expectedId: "stable-env-session" }); + }); +} + +test("MCP initialize still trims a usable metadata ID without an environment binding", async (t) => { + await exercise(t, { meta: { ui: { sessionId: " metadata-session\t" } }, envId: null, expectedId: "metadata-session", expectedRegistration: "metadata-session" }); +}); + +test("MCP initialize keeps the environment binding ahead of a usable metadata ID", async (t) => { + await exercise(t, { meta: { ui: { sessionId: "other-session" } }, expectedId: "stable-env-session" }); +}); + +test("MCP per-call conversation routing still outranks the initialization binding", async (t) => { + await exercise(t, { meta: { ui: { sessionId: "other-session" } }, perCall: "per-call-session", expectedId: "per-call-session" }); +}); + +test("MCP host-native thread routing still outranks environment and copied arguments", async (t) => { + await exercise(t, { meta: { ui: { sessionId: "other-session" } }, perCall: "copied-session", callMeta: { threadId: " native-session " }, expectedId: "native-session", native: true }); +}); + +test("MCP ignored initialization metadata leaves unbound host-native routing available", async (t) => { + await exercise(t, { meta: { ui: { sessionId: { opaque: true } } }, envId: null, callMeta: { threadId: "native-session" }, expectedId: "native-session", expectedRegistration: null, native: true }); +}); From bec5f08e6f19cc09a2a71b40e2509860efd65274 Mon Sep 17 00:00:00 2001 From: Afloat Date: Thu, 19 Nov 2026 02:37:56 +0800 Subject: [PATCH 2/2] test: register MCP metadata subprocess coverage --- scripts/live-files.mjs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/live-files.mjs b/scripts/live-files.mjs index 30c4a60135..f18663c7ba 100644 --- a/scripts/live-files.mjs +++ b/scripts/live-files.mjs @@ -150,6 +150,9 @@ export const LIVE_FILES = [ // One Linux-only Node child reads fixture system CAs and exits within 10s; // no child ports or network I/O. In-process HTTPS fixtures use listen(0). "tests/upstream-ca-default-trust.test.ts", + // MCP stdio children talk only to each case's listen(0) HTTP fixture; + // teardown reaps each child and closes its listener. No shared port or state. + "tests/mcp-initialize-metadata.test.ts", ]; /** Band width per live file: the zone ladder drifts +1 per EADDRINUSE retry;