Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(mcp): support wildcard port in allowedOrigins and blockedOrigins (#38944)
Saarya Berlingere7c89ef0bee5
diff --git a/packages/playwright/src/mcp/browser/context.ts b/packages/playwright/src/mcp/browser/context.tsindex 2afec4d974..89361e1adc 100644--- a/packages/playwright/src/mcp/browser/context.ts+++ b/packages/playwright/src/mcp/browser/context.ts@@ -329,6 +329,11 @@ function allRootPaths(clientInfo: ClientInfo): string[] {function originOrHostGlob(originOrHost: string) {+ // Support wildcard port patterns like "http://localhost:*" or "https://example.com:*"+ const wildcardPortMatch = originOrHost.match(/^(https?:\/\/[^/:]+):\*$/);+ if (wildcardPortMatch)+ return `${wildcardPortMatch[1]}:*/**`;+try {const url = new URL(originOrHost);// localhost:1234 will parse as protocol 'localhost:' and 'null' origin.diff --git a/packages/playwright/src/mcp/config.d.ts b/packages/playwright/src/mcp/config.d.tsindex 002766ddbb..0c49e5131b 100644--- a/packages/playwright/src/mcp/config.d.ts+++ b/packages/playwright/src/mcp/config.d.ts@@ -181,11 +181,19 @@ export type Config = {network?: {/*** List of origins to allow the browser to request. Default is to allow all. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.+ *+ * Supported formats:+ * - Full origin: `https://example.com:8080` - matches only that origin+ * - Wildcard port: `http://localhost:*` - matches any port on localhost with http protocol*/allowedOrigins?: string[];/*** List of origins to block the browser to request. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.+ *+ * Supported formats:+ * - Full origin: `https://example.com:8080` - matches only that origin+ * - Wildcard port: `http://localhost:*` - matches any port on localhost with http protocol*/blockedOrigins?: string[];};diff --git a/tests/mcp/request-blocking.spec.ts b/tests/mcp/request-blocking.spec.tsindex a2ca31879b..298d78117e 100644--- a/tests/mcp/request-blocking.spec.ts+++ b/tests/mcp/request-blocking.spec.ts@@ -125,3 +125,37 @@ test('blocked without allowed allows non-explicitly specified origins (origin)',const result = await fetchPage(client, server.PREFIX + '/ppp');expect(result).toContain('content:PPP');});++test('allowed works (wildcard port)', async ({ server, startClient }) => {+ server.setContent('/ppp', 'content:PPP', 'text/html');+ const { client } = await startClient({+ args: ['--allowed-origins', 'http://localhost:*']+ });+ const result = await fetchPage(client, server.PREFIX + '/ppp');+ expect(result).toContain('content:PPP');+});++test('allowed blocks different hostname (wildcard port)', async ({ startClient }) => {+ const { client } = await startClient({+ args: ['--allowed-origins', 'http://localhost:*'],+ });+ const result = await fetchPage(client, 'http://example.com/');+ expect(result).toMatch(BLOCK_MESSAGE);+});++test('allowed blocks different protocol (wildcard port)', async ({ startClient }) => {+ const { client } = await startClient({+ args: ['--allowed-origins', 'http://localhost:*'],+ });+ const result = await fetchPage(client, 'https://localhost:8080/');+ expect(result).toMatch(BLOCK_MESSAGE);+});++test('blocked works (wildcard port)', async ({ server, startClient }) => {+ server.setContent('/ppp', 'content:PPP', 'text/html');+ const { client } = await startClient({+ args: ['--blocked-origins', `http://${new URL(server.PREFIX).host.split(':')[0]}:*`]+ });+ const result = await fetchPage(client, server.PREFIX + '/ppp');+ expect(result).toMatch(BLOCK_MESSAGE);+});