From e52c0a72c59c740315e89149881a27ce4d89a11c Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:54:26 +0800 Subject: [PATCH] fix(browser): retain incomplete WebMCP replay coverage Mark malformed WebMCP event envelopes, tool lists and entries as invalid protocol values while preserving valid siblings and out-of-scope classifications. Verify toolsAdded and toolsRemoved replay uncertainty through CDP discovery and capture comparison. --- src/browser/CdpWebMcpDiscovery.ts | 51 +++++++-- ...cdpBrowserProvider.document_script.test.ts | 104 +++++++++++++++++- 2 files changed, 143 insertions(+), 12 deletions(-) diff --git a/src/browser/CdpWebMcpDiscovery.ts b/src/browser/CdpWebMcpDiscovery.ts index 1cdbd3334..dd4b3fb6c 100644 --- a/src/browser/CdpWebMcpDiscovery.ts +++ b/src/browser/CdpWebMcpDiscovery.ts @@ -365,20 +365,30 @@ const ingestWebMcpEvent = (options: WebMcpIngestOptions): void => { transientFrames, resolvedTransientFrames, } = options; + if ( + event.method !== "WebMCP.toolsAdded" && + event.method !== "WebMCP.toolsRemoved" + ) + return; const params = recordValue(event.params); - if (params === undefined) return; + if (params === undefined) { + completeness.exclude("webmcp_tools", "invalid_protocol_value"); + return; + } if (event.method === "WebMCP.toolsRemoved") { - for (const removed of recordsValue(params.tools)) { + for (const removed of webMcpEventTools(params.tools, completeness)) { const name = cdpStringValue(removed.name); const frameId = cdpStringValue(removed.frameId); - if (name === undefined || frameId === undefined) continue; + if (name === undefined || frameId === undefined) { + completeness.exclude("webmcp_tools", "invalid_protocol_value"); + continue; + } for (const [key, tool] of tools) if (tool.name === name && tool.frame_id === frameId) tools.delete(key); } return; } - if (event.method !== "WebMCP.toolsAdded") return; - for (const declared of recordsValue(params.tools)) { + for (const declared of webMcpEventTools(params.tools, completeness)) { const frameId = cdpStringValue(declared.frameId); if (frameId !== undefined && transientFrames.has(frameId)) { if (!resolvedTransientFrames.has(frameId)) continue; @@ -391,6 +401,26 @@ const ingestWebMcpEvent = (options: WebMcpIngestOptions): void => { } }; +const webMcpEventTools = ( + value: unknown, + completeness: CdpCaptureCompleteness, +): UnknownRecord[] => { + if (!Array.isArray(value)) { + completeness.exclude("webmcp_tools", "invalid_protocol_value"); + return []; + } + const records: UnknownRecord[] = []; + for (const item of value) { + const record = recordValue(item); + if (record === undefined) { + completeness.exclude("webmcp_tools", "invalid_protocol_value"); + continue; + } + records.push(record); + } + return records; +}; + const normalizeTool = ( value: UnknownRecord, input: DiscoverWebMcpToolsInput, @@ -403,12 +433,11 @@ const normalizeTool = ( const frameId = cdpStringValue(value.frameId); const name = cdpStringValue(value.name); const frame = frameId === undefined ? undefined : frames.get(frameId); - if ( - frameId === undefined || - name === undefined || - frame?.origin === null || - frame === undefined - ) { + if (frameId === undefined || name === undefined) { + completeness.exclude("webmcp_tools", "invalid_protocol_value"); + return undefined; + } + if (frame?.origin === null || frame === undefined) { completeness.exclude("webmcp_tools", "out_of_target_scope"); return undefined; } diff --git a/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts b/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts index b74c60081..5fd1e061b 100644 --- a/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts +++ b/tests/boundary/browser/cdpBrowserProvider.document_script.test.ts @@ -294,6 +294,15 @@ describe("CdpBrowserProvider: WebMCP discovery and completeness", () => { ], }, }); + expect(result.value.completeness.excluded).toContainEqual( + expect.objectContaining({ + section: "webmcp_tools", + reason: "out_of_target_scope", + }), + ); + expect(result.value.completeness.unavailable_sections).not.toContain( + "webmcp_tools", + ); const serialized = JSON.stringify(result.value); expect(serialized).toContain("tool-secret"); expect(serialized).toContain("tool-source-secret"); @@ -354,7 +363,9 @@ describe("CdpBrowserProvider: WebMCP discovery and completeness", () => { "WebMCP.invokeTool", ); }); +}); +describe("CdpBrowserProvider: WebMCP replay inventory", () => { it("returns every WebMCP registration in the replay", async () => { const browser = await startFakeCdpBrowser({ webMcpTools: true, @@ -410,7 +421,98 @@ describe("CdpBrowserProvider: WebMCP discovery and completeness", () => { }); }); -describe("CdpBrowserProvider WebMCP inventory completeness", () => { +describe("CdpBrowserProvider WebMCP malformed replay completeness", () => { + it.each(["toolsAdded", "toolsRemoved"] as const)( + "keeps malformed %s replay entries from proving WebMCP equality", + async (malformedEvent) => { + let malformed = false; + const registration = { + name: "read_item", + frameId: "frame-main", + inputSchema: { type: "object" }, + }; + const browser = await startFakeCdpBrowser({ + commandEvents: (command) => { + if (command.method !== "WebMCP.enable") return undefined; + const events: { + readonly method: string; + readonly params: unknown; + readonly sessionId?: string; + }[] = [ + { + method: "WebMCP.toolsAdded", + ...(command.sessionId === undefined + ? {} + : { sessionId: command.sessionId }), + params: { + tools: + malformed && malformedEvent === "toolsAdded" + ? [registration, null] + : [registration], + }, + }, + ]; + if (malformed && malformedEvent === "toolsRemoved") + events.push({ + method: "WebMCP.toolsRemoved", + ...(command.sessionId === undefined + ? {} + : { sessionId: command.sessionId }), + params: { tools: [{ name: registration.name }, null] }, + }); + return events; + }, + }); + trackBrowser(browser); + const provider = new CdpBrowserProvider(); + const input = { + cdp_endpoint: browser.endpoint, + allowed_origins: [browser.allowedOrigin], + target_id: "allowed-page", + observation_ms: 0, + }; + const inspection = await provider.inspectPage( + inspectWebPageInputSchema.parse(input), + ); + if (!inspection.ok) throw inspection.error; + const discover = async () => { + const result = await provider.discoverWebMcpTools( + discoverWebMcpToolsInputSchema.parse(input), + ); + if (!result.ok) throw result.error; + return result.value; + }; + const before = await discover(); + expect(before.tools.items).toHaveLength(1); + expect(before.completeness.unavailable_sections).not.toContain( + "webmcp_tools", + ); + + malformed = true; + const after = await discover(); + expect(after.tools.items).toHaveLength(1); + expect(after.completeness.unavailable_sections).toContain("webmcp_tools"); + expect(after.completeness.excluded).toContainEqual( + expect.objectContaining({ + section: "webmcp_tools", + reason: "invalid_protocol_value", + }), + ); + const comparison = compareWebCaptures( + compareWebCapturesInputSchema.parse({ + before: { inspection: inspection.value, webmcp: before }, + after: { inspection: inspection.value, webmcp: after }, + }), + ); + expect(comparison.dimensions.webmcp).toMatchObject({ + status: "unknown", + total_changes: 0, + }); + }, + ); +}); + +describe("CdpBrowserProvider WebMCP schema comparison", () => { it("compares full untrusted declarations independently of object-key order", async () => { const declaration: JsonValue = { type: "object",