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
fix(server): advertise bound IP literal in wsEndpoint and URL prefix (#40675)
Yury Semikhatskyb3691fe13592
diff --git a/packages/playwright-core/src/tools/utils/mcp/http.ts b/packages/playwright-core/src/tools/utils/mcp/http.tsindex c630b8693d..8d6a2fb38d 100644--- a/packages/playwright-core/src/tools/utils/mcp/http.ts+++ b/packages/playwright-core/src/tools/utils/mcp/http.ts@@ -22,6 +22,7 @@ import crypto from 'crypto';import debug from 'debug';import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';+import { urlHostFromAddress } from '@utils/httpServer';import { createHttpServer, startHttpServer } from '@utils/network';import * as mcpServer from './server';@@ -49,7 +50,7 @@ export function addressToString(address: string | net.AddressInfo | null, optionassert(address, 'Could not bind server socket');if (typeof address === 'string')throw new Error('Unexpected address type: ' + address);- let host = address.family === 'IPv4' ? address.address : `[${address.address}]`;+ let host = urlHostFromAddress(address);if (options.normalizeLoopback && (host === '0.0.0.0' || host === '[::]' || host === '[::1]' || host === '127.0.0.1'))host = 'localhost';return `${options.protocol}://${host}:${address.port}`;diff --git a/packages/utils/httpServer.ts b/packages/utils/httpServer.tsindex cf36a2cbd4..020dcfc597 100644--- a/packages/utils/httpServer.ts+++ b/packages/utils/httpServer.ts@@ -169,8 +169,7 @@ export class HttpServer {this._urlPrefixHumanReadable = address;} else {this._port = address.port;- const resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;- this._urlPrefixPrecise = `http://${resolvedHost}:${address.port}`;+ this._urlPrefixPrecise = `http://${urlHostFromAddress(address)}:${address.port}`;this._urlPrefixHumanReadable = `http://${host ?? 'localhost'}:${address.port}`;this._allowedHosts = computeAllowedHosts(host, address.address);}@@ -303,6 +302,11 @@ export function computeAllowedHosts(requested: string | undefined, bound: stringreturn new Set(['localhost', '127.0.0.1', '[::1]']);}+// Bracket IPv6 literals so they can be used as the host part of a URL.+export function urlHostFromAddress(address: { address: string, family: string }): string {+ return address.family === 'IPv6' ? `[${address.address}]` : address.address;+}+export function hostnameFromHostHeader(host: string): string {if (host.startsWith('[')) {const end = host.indexOf(']');diff --git a/packages/utils/wsServer.ts b/packages/utils/wsServer.tsindex 5eb6502aa5..93625ecb60 100644--- a/packages/utils/wsServer.ts+++ b/packages/utils/wsServer.ts@@ -15,7 +15,7 @@*/import { WebSocketServer as wsServer } from 'ws';-import { computeAllowedHosts, hostnameFromHostHeader } from './httpServer';+import { computeAllowedHosts, hostnameFromHostHeader, urlHostFromAddress } from './httpServer';import { createHttpServer } from './network';import { debugLogger } from './debugLogger';@@ -78,11 +78,15 @@ export class WSServer {reject(new Error('Could not bind server socket'));return;}- const urlHost = hostname.includes(':') && !hostname.startsWith('[') ? `[${hostname}]` : hostname;- const wsEndpoint = typeof address === 'string' ? `${address}${path}` : `ws://${urlHost}:${address.port}${path}`;- if (typeof address !== 'string')- this._allowedHosts = computeAllowedHosts(hostname, address.address);- resolve(wsEndpoint);+ if (typeof address === 'string') {+ resolve(`${address}${path}`);+ return;+ }+ // Advertise the bound IP literal in the wsEndpoint so the client connects to+ // the same address family the server bound to. Otherwise the client and+ // server resolvers can disagree on what 'localhost' means (see #40605).+ this._allowedHosts = computeAllowedHosts(hostname, address.address);+ resolve(`ws://${urlHostFromAddress(address)}:${address.port}${path}`);}).on('error', reject);});diff --git a/tests/library/browser-server.spec.ts b/tests/library/browser-server.spec.tsindex 2cbc7c8108..27c62f946a 100644--- a/tests/library/browser-server.spec.ts+++ b/tests/library/browser-server.spec.ts@@ -65,7 +65,7 @@ it('should write descriptor on start and remove on stop', async ({ browser }) =>it('should start ws server with host/port and produce well-formed endpoint', async ({ browserType, browser }) => {const serverInfo = await browser.bind('default', { host: 'localhost', port: 0 });- expect(serverInfo.endpoint).toMatch(/^ws:\/\/localhost:\d+\/[a-f0-9]+$/);+ expect(serverInfo.endpoint).toMatch(/^ws:\/\/(127\.0\.0\.1|\[::1\]):\d+\/[a-f0-9]+$/);const browser2 = await browserType.connect(serverInfo.endpoint);const page = await browser2.newPage();