Chrome DevTools for coding agents
fix: treat local host name and interface addresses as local browser endpoints (#3463)
yulunzcf587b480a00
diff --git a/src/utils/url.ts b/src/utils/url.tsindex b0024d00..2aedeb69 100644--- a/src/utils/url.ts+++ b/src/utils/url.ts@@ -4,6 +4,8 @@* SPDX-License-Identifier: Apache-2.0*/+import os from 'node:os';+// Node 22 has no global URLPattern. The CLI bundle exports Puppeteer's// polyfill, so this module does not need the full third-party bundle.import {URLPattern} from '../third_party/cli.js';@@ -64,12 +66,52 @@ export function isLocalhost(url?: string): boolean {// [::ffff:0.0.0.0] as [::ffff:0:0].const UNSPECIFIED_HOSTNAMES = new Set(['0.0.0.0', '[::]', '[::ffff:0:0]']);+/**+ * Serializes a host name or IP address the way `URL#hostname` does, or returns+ * undefined if it is not a valid host.+ */+function toUrlHostname(host: string): string | undefined {+ try {+ return new URL(`http://${host.includes(':') ? `[${host}]` : host}`)+ .hostname;+ } catch {+ return undefined;+ }+}++/**+ * Checks whether a `URL#hostname` is the host name of the local machine or an+ * address of one of its network interfaces.+ */+function isLocalMachineHostname(hostname: string): boolean {+ const normalized = hostname.replace(/\.$/, '');+ if (normalized === toUrlHostname(os.hostname())) {+ return true;+ }+ let interfaces: ReturnType<typeof os.networkInterfaces>;+ try {+ interfaces = os.networkInterfaces();+ } catch {+ // Some platforms cannot enumerate interfaces; keep the loopback checks.+ return false;+ }+ for (const addresses of Object.values(interfaces)) {+ for (const {address} of addresses ?? []) {+ if (toUrlHostname(address) === normalized) {+ return true;+ }+ }+ }+ return false;+}+/*** Determines whether a browser endpoint URL points to the local machine.** In addition to {@link isLocalhost}, this accepts the unspecified addresses- * 0.0.0.0 and [::]. A connection to an unspecified address reaches the local- * machine.+ * 0.0.0.0 and [::], the local host name, and the addresses of the local+ * network interfaces (for example a LAN or Docker bridge IP). A connection to+ * any of these reaches the local machine.** @param url The browser endpoint URL (for example, a WebSocket endpoint).* @returns true if the endpoint is on the local machine.@@ -87,7 +129,10 @@ export function isLocalBrowserEndpoint(url: string): boolean {if (!ALLOWED_PROTOCOLS.has(parsed.protocol)) {return false;}- return UNSPECIFIED_HOSTNAMES.has(parsed.hostname);+ return (+ UNSPECIFIED_HOSTNAMES.has(parsed.hostname) ||+ isLocalMachineHostname(parsed.hostname)+ );}export interface ValidateUrlOptions {diff --git a/tests/utils/url.test.ts b/tests/utils/url.test.tsindex 3a702b1e..713d5bad 100644--- a/tests/utils/url.test.ts+++ b/tests/utils/url.test.ts@@ -5,8 +5,11 @@*/import assert from 'node:assert';+import os from 'node:os';import {describe, it} from 'node:test';+import sinon from 'sinon';+import {isAllowedUrl,isLocalBrowserEndpoint,@@ -52,6 +55,50 @@ describe('isLocalBrowserEndpoint', () => {assert.strictEqual(isLocalBrowserEndpoint(''), false);assert.strictEqual(isLocalBrowserEndpoint('not a url'), false);});++ it('returns true for endpoints on a local network interface address', () => {+ const ipv4: os.NetworkInterfaceInfoIPv4 = {+ address: '192.168.1.20',+ netmask: '255.255.255.0',+ family: 'IPv4',+ mac: '00:00:00:00:00:01',+ internal: false,+ cidr: '192.168.1.20/24',+ };+ const ipv6: os.NetworkInterfaceInfoIPv6 = {+ address: '2001:db8::20',+ netmask: 'ffff:ffff:ffff:ffff::',+ family: 'IPv6',+ mac: '00:00:00:00:00:01',+ internal: false,+ cidr: '2001:db8::20/64',+ scopeid: 0,+ };+ sinon.stub(os, 'networkInterfaces').returns({eth0: [ipv4, ipv6]});++ assert.strictEqual(isLocalBrowserEndpoint('ws://192.168.1.20:9222'), true);+ assert.strictEqual(+ isLocalBrowserEndpoint('ws://[2001:db8:0::20]:9222'),+ true,+ );+ assert.strictEqual(isLocalBrowserEndpoint('ws://192.168.1.21:9222'), false);+ });++ it('returns true for endpoints on the local host name', () => {+ sinon.stub(os, 'hostname').returns('My-Host');++ assert.strictEqual(isLocalBrowserEndpoint('ws://my-host:9222'), true);+ assert.strictEqual(isLocalBrowserEndpoint('ws://MY-HOST.:9222'), true);+ assert.strictEqual(isLocalBrowserEndpoint('ws://other-host:9222'), false);+ });++ it('treats an endpoint as remote when network interfaces cannot be read', () => {+ sinon.stub(os, 'hostname').returns('my-host');+ sinon.stub(os, 'networkInterfaces').throws(new Error('uv error'));++ assert.strictEqual(isLocalBrowserEndpoint('ws://192.168.1.20:9222'), false);+ assert.strictEqual(isLocalBrowserEndpoint('ws://localhost:9222'), true);+ });});describe('isLocalhost', () => {