From 437c7cb6bbde6fcca716e0ddb45f82461fa4b177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=95=E5=BB=B7=E8=99=8E?= Date: Sun, 1 Mar 2026 19:38:53 +0800 Subject: [PATCH] fix(mcp): headerParser truncates header values containing colons The headerParser function used split(':') which splits on ALL colons, causing header values like 'http://example.com' to be truncated to 'http'. This affects --cdp-header CLI flag and PLAYWRIGHT_MCP_CDP_HEADERS env var. Fix by splitting only on the first colon, as per HTTP header spec (RFC 7230). Fixes #1417 --- .../playwright-core/src/mcp/browser/config.ts | 6 +++++- tests/mcp/cdp.spec.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/playwright-core/src/mcp/browser/config.ts b/packages/playwright-core/src/mcp/browser/config.ts index 2ecbf7acbd791..0491cec075ced 100644 --- a/packages/playwright-core/src/mcp/browser/config.ts +++ b/packages/playwright-core/src/mcp/browser/config.ts @@ -521,7 +521,11 @@ export function headerParser(arg: string | undefined, previous?: Record = previous || {}; - const [name, value] = arg.split(':').map(v => v.trim()); + const colonIndex = arg.indexOf(':'); + if (colonIndex === -1) + return result; + const name = arg.substring(0, colonIndex).trim(); + const value = arg.substring(colonIndex + 1).trim(); result[name] = value; return result; } diff --git a/tests/mcp/cdp.spec.ts b/tests/mcp/cdp.spec.ts index f05ea7c306421..d8483511fdac1 100644 --- a/tests/mcp/cdp.spec.ts +++ b/tests/mcp/cdp.spec.ts @@ -110,3 +110,21 @@ test('cdp server with headers', async ({ startClient, server }) => { }); expect(authHeader).toBe('Bearer 1234567890'); }); + +test('cdp server with headers containing colons', async ({ startClient, server }) => { + // Regression test for https://github.com/microsoft/playwright-mcp/issues/1417 + let customHeader = ''; + server.setRoute('/json/version/', (req, res) => { + customHeader = req.headers['x-custom']; + res.end(); + }); + + const { client } = await startClient({ args: [`--cdp-endpoint=${server.PREFIX}`, '--cdp-header', 'X-Custom: http://example.com:8080/path'] }); + expect(await client.callTool({ + name: 'browser_navigate', + arguments: { url: server.HELLO_WORLD }, + })).toHaveResponse({ + isError: true, + }); + expect(customHeader).toBe('http://example.com:8080/path'); +});