Skip to content

Chrome DevTools for coding agents

fix: url pattern partial ipv4 (#2859)

Natasha Gorshunovac87d190b2e26
diff --git a/docs/configuration.md b/docs/configuration.md
index 5c4e096e..6558bec8 100644
--- a/docs/configuration.md
+++ b/docs/configuration.md
@@ -122,11 +122,11 @@ The Chrome DevTools MCP server supports the following configuration option:
- **Default:** `false`
- **`--blockedUrlPattern`/ `--blocked-url-pattern`**
- Restricts browser's network access by blocking specified URL patterns (uses https://urlpattern.spec.whatwg.org/). Silently detaches from targets with blocked URLs upon connection, and blocks runtime requests (including navigations and subresources). Accepts an array of patterns. A pattern that uses a regexp group or a named group (`:name`) in any component (for example `(127\.\d+\.\d+\.\d+)` in the hostname or `*://127.0.0.1::port/*`) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*` wildcard instead.
+ Restricts browser's network access by blocking specified URL patterns (uses https://urlpattern.spec.whatwg.org/). Silently detaches from targets with blocked URLs upon connection, and blocks runtime requests (including navigations and subresources). Accepts an array of patterns. A pattern that uses a regexp group, a named group (`:name`), or a partial IPv4 wildcard (for example `(127\.\d+\.\d+\.\d+)`, `*://127.0.0.1::port/*`, or `*://127.0.0.*/*`) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*` wildcard instead.
- **Type:** array
- **`--allowedUrlPattern`/ `--allowed-url-pattern`**
- Restricts browser's network access by allowing only specified URL patterns (uses https://urlpattern.spec.whatwg.org/). Requires Chrome 149+. Silently detaches from targets with unallowed URLs upon connection, and blocks runtime requests (including navigations and subresources). Accepts an array of patterns. A pattern that uses a regexp group or a named group (`:name`) in any component (for example `(127\.\d+\.\d+\.\d+)` in the hostname or `*://127.0.0.1::port/*`) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*` wildcard instead.
+ Restricts browser's network access by allowing only specified URL patterns (uses https://urlpattern.spec.whatwg.org/). Requires Chrome 149+. Silently detaches from targets with unallowed URLs upon connection, and blocks runtime requests (including navigations and subresources). Accepts an array of patterns. A pattern that uses a regexp group, a named group (`:name`), or a partial IPv4 wildcard (for example `(127\.\d+\.\d+\.\d+)`, `*://127.0.0.1::port/*`, or `*://127.0.0.*/*`) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*` wildcard instead.
- **Type:** array
- **`--screenshotFormat`/ `--screenshot-format`**
diff --git a/src/config/puppeteer-options.ts b/src/config/puppeteer-options.ts
index 4edddcff..fb07c10e 100644
--- a/src/config/puppeteer-options.ts
+++ b/src/config/puppeteer-options.ts
@@ -5,7 +5,7 @@
*/
import type {YargsOptions} from '../third_party/index.js';
-import {findUnenforceablePattern} from '../utils/url.js';
+import {validateUrlPatterns} from '../utils/url.js';
/**
* Options that are applied through Puppeteer when launching or connecting to
@@ -46,17 +46,12 @@ export const puppeteerOptions = {
type: 'array',
string: true,
describe:
- "Restricts browser's network access by blocking specified URL patterns (uses https://urlpattern.spec.whatwg.org/). Silently detaches from targets with blocked URLs upon connection, and blocks runtime requests (including navigations and subresources). Accepts an array of patterns. A pattern that uses a regexp group or a named group (`:name`) in any component (for example `(127\\.\\d+\\.\\d+\\.\\d+)` in the hostname or `*://127.0.0.1::port/*`) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*` wildcard instead.",
+ "Restricts browser's network access by blocking specified URL patterns (uses https://urlpattern.spec.whatwg.org/). Silently detaches from targets with blocked URLs upon connection, and blocks runtime requests (including navigations and subresources). Accepts an array of patterns. A pattern that uses a regexp group, a named group (`:name`), or a partial IPv4 wildcard (for example `(127\\.\\d+\\.\\d+\\.\\d+)`, `*://127.0.0.1::port/*`, or `*://127.0.0.*/*`) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*` wildcard instead.",
coerce: (arg: string[] | undefined) => {
if (arg === undefined || arg.length === 0) {
return undefined;
}
- const pattern = findUnenforceablePattern(arg);
- if (pattern) {
- throw new Error(
- `Invalid --blockedUrlPattern "${pattern}": a regexp group or a ":name" named group is not enforced on redirects or subresources. Use an exact value or a "*" wildcard instead.`,
- );
- }
+ validateUrlPatterns(arg, '--blockedUrlPattern');
return arg;
},
},
@@ -64,7 +59,7 @@ export const puppeteerOptions = {
type: 'array',
string: true,
describe:
- "Restricts browser's network access by allowing only specified URL patterns (uses https://urlpattern.spec.whatwg.org/). Requires Chrome 149+. Silently detaches from targets with unallowed URLs upon connection, and blocks runtime requests (including navigations and subresources). Accepts an array of patterns. A pattern that uses a regexp group or a named group (`:name`) in any component (for example `(127\\.\\d+\\.\\d+\\.\\d+)` in the hostname or `*://127.0.0.1::port/*`) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*` wildcard instead.",
+ "Restricts browser's network access by allowing only specified URL patterns (uses https://urlpattern.spec.whatwg.org/). Requires Chrome 149+. Silently detaches from targets with unallowed URLs upon connection, and blocks runtime requests (including navigations and subresources). Accepts an array of patterns. A pattern that uses a regexp group, a named group (`:name`), or a partial IPv4 wildcard (for example `(127\\.\\d+\\.\\d+\\.\\d+)`, `*://127.0.0.1::port/*`, or `*://127.0.0.*/*`) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*` wildcard instead.",
coerce: (arg: string[] | undefined) => {
if (arg === undefined) {
return undefined;
@@ -74,12 +69,7 @@ export const puppeteerOptions = {
'Invalid --allowedUrlPattern: at least one pattern is required.',
);
}
- const pattern = findUnenforceablePattern(arg);
- if (pattern) {
- throw new Error(
- `Invalid --allowedUrlPattern "${pattern}": a regexp group or a ":name" named group is not enforced on redirects or subresources. Use an exact value or a "*" wildcard instead.`,
- );
- }
+ validateUrlPatterns(arg, '--allowedUrlPattern');
return arg;
},
},
diff --git a/src/utils/url.ts b/src/utils/url.ts
index 95f35079..08aa7953 100644
--- a/src/utils/url.ts
+++ b/src/utils/url.ts
@@ -122,21 +122,58 @@ function hasNamedGroup(component: string): boolean {
}
/**
- * Finds the first pattern that Chrome can't enforce on redirects or subresources:
- * - Any pattern containing a regexp group (for example `(foo|bar)`).
- * - Any pattern containing a named group (`:name`, for example `*://127.0.0.1::port/*`).
+ * Checks whether a URLPattern string has a partial IPv4 wildcard in the
+ * hostname (for example `127.0.0.*`, `*.0.0.1`, `*1*`, or `{127.0.0.*}`),
+ * which Chrome cannot enforce on redirects or subresources.
+ */
+function hasPartialIpv4Hostname(pattern: string): boolean {
+ const authority = /:\/\/([^/?#]+)/.exec(pattern)?.[1] ?? '';
+ const hostPort = authority.slice(authority.lastIndexOf('@') + 1);
+ if (hostPort.startsWith('[')) {
+ return false;
+ }
+ const hostname = hostPort.replace(/:.*$/, '');
+ if (!hostname.includes('*')) {
+ return false;
+ }
+ const stripped = hostname.replaceAll(/[{}*\\]/g, '').replace(/\.+$/, '');
+ const lastLabel = stripped.split('.').at(-1) ?? '';
+ return /^(?:\d+|0[xX][0-9a-fA-F]*)$/.test(lastLabel);
+}
+
+/**
+ * Validates that every pattern can be enforced by Chrome on redirects and subresources:
+ * - Rejects patterns with a partial IPv4 wildcard in the hostname (for example `127.0.0.*`).
+ * - Rejects patterns containing a regexp group (for example `(foo|bar)`).
+ * - Rejects patterns containing a named group (`:name`, for example `*://127.0.0.1::port/path`).
*
* @param patterns The `--blockedUrlPattern`/`--allowedUrlPattern` values to check.
- * @returns The first unenforceable pattern, or undefined if all are valid.
- * @throws Error if a pattern's syntax is invalid (via `new URLPattern`).
+ * @param flagName The CLI flag name used in error messages.
+ * @throws Error if a pattern's syntax is invalid (via `new URLPattern`) or uses an unenforceable construct.
*/
-export function findUnenforceablePattern(
+export function validateUrlPatterns(
patterns: string[],
-): string | undefined {
+ flagName: string,
+): void {
for (const raw of patterns) {
- const parsed = new URLPattern(raw);
+ if (hasPartialIpv4Hostname(raw)) {
+ throw new Error(
+ `Invalid ${flagName} "${raw}": a partial IPv4 wildcard is not enforced on redirects or subresources. Use an exact IPv4 address or a "*" wildcard instead.`,
+ );
+ }
+ let parsed: URLPattern;
+ try {
+ parsed = new URLPattern(raw);
+ } catch (error) {
+ const message = error instanceof Error ? error.message : String(error);
+ throw new Error(`Invalid ${flagName} "${raw}": ${message}`, {
+ cause: error,
+ });
+ }
if (parsed.hasRegExpGroups) {
- return raw;
+ throw new Error(
+ `Invalid ${flagName} "${raw}": a regexp group is not enforced on redirects or subresources. Use an exact value or a "*" wildcard instead.`,
+ );
}
const components = [
parsed.protocol,
@@ -149,10 +186,11 @@ export function findUnenforceablePattern(
parsed.hash,
];
if (components.some(hasNamedGroup)) {
- return raw;
+ throw new Error(
+ `Invalid ${flagName} "${raw}": a ":name" named group is not enforced on redirects or subresources. Use an exact value or a "*" wildcard instead.`,
+ );
}
}
- return undefined;
}
/**
diff --git a/tests/cli.test.ts b/tests/cli.test.ts
index 80c7a7be..7ab90c77 100644
--- a/tests/cli.test.ts
+++ b/tests/cli.test.ts
@@ -572,13 +572,13 @@ describe('cli args parsing', () => {
]);
});
- it('rejects url-pattern with a regexp group or named group', async () => {
+ it('rejects url-pattern with a regexp group, named group, or partial IPv4 wildcard', async () => {
assert.throws(
() =>
parseConfig([
String.raw`--blockedUrlPattern=*://(127\.\d+\.\d+\.\d+):*/*`,
]),
- /Invalid --blockedUrlPattern .*is not enforced/,
+ /Invalid --blockedUrlPattern .*a regexp group is not enforced/,
);
assert.throws(
@@ -586,17 +586,27 @@ describe('cli args parsing', () => {
parseConfig([
String.raw`--allowedUrlPattern=*://(127\.\d+\.\d+\.\d+):*/*`,
]),
- /Invalid --allowedUrlPattern .*is not enforced/,
+ /Invalid --allowedUrlPattern .*a regexp group is not enforced/,
);
assert.throws(
() => parseConfig(['--blockedUrlPattern=*://127.0.0.1::port/secret']),
- /Invalid --blockedUrlPattern .*is not enforced/,
+ /Invalid --blockedUrlPattern .*a ":name" named group is not enforced/,
);
assert.throws(
() => parseConfig(['--allowedUrlPattern=*://127.0.0.1::port/secret']),
- /Invalid --allowedUrlPattern .*is not enforced/,
+ /Invalid --allowedUrlPattern .*a ":name" named group is not enforced/,
+ );
+
+ assert.throws(
+ () => parseConfig(['--blockedUrlPattern=*://127.0.0.*/*']),
+ /Invalid --blockedUrlPattern .*a partial IPv4 wildcard is not enforced/,
+ );
+
+ assert.throws(
+ () => parseConfig(['--allowedUrlPattern=*://127.0.0.*/*']),
+ /Invalid --allowedUrlPattern .*a partial IPv4 wildcard is not enforced/,
);
});
@@ -607,7 +617,7 @@ describe('cli args parsing', () => {
'--blocked-url-pattern=https://a.com/*',
String.raw`--blocked-url-pattern=*://example.com/(foo|bar)`,
]),
- /Invalid --blockedUrlPattern .*is not enforced/,
+ /Invalid --blockedUrlPattern .*a regexp group is not enforced/,
);
assert.throws(
() =>
@@ -615,16 +625,20 @@ describe('cli args parsing', () => {
'--allowed-url-pattern=https://a.com/*',
String.raw`--allowed-url-pattern=*://example.com/(foo|bar)`,
]),
- /Invalid --allowedUrlPattern .*is not enforced/,
+ /Invalid --allowedUrlPattern .*a regexp group is not enforced/,
);
});
it('rejects url-pattern with invalid syntax', async () => {
- assert.throws(() =>
- parseConfig(['--blocked-url-pattern=*://example.com/(unterminated']),
+ assert.throws(
+ () =>
+ parseConfig(['--blocked-url-pattern=*://example.com/(unterminated']),
+ /Invalid --blockedUrlPattern "\*:\/\/example\.com\/\(unterminated":/,
);
- assert.throws(() =>
- parseConfig(['--allowed-url-pattern=*://example.com/(unterminated']),
+ assert.throws(
+ () =>
+ parseConfig(['--allowed-url-pattern=*://example.com/(unterminated']),
+ /Invalid --allowedUrlPattern "\*:\/\/example\.com\/\(unterminated":/,
);
});
diff --git a/tests/utils/url.test.ts b/tests/utils/url.test.ts
index b5eeeb5b..37077b30 100644
--- a/tests/utils/url.test.ts
+++ b/tests/utils/url.test.ts
@@ -8,10 +8,10 @@ import assert from 'node:assert';
import {describe, it} from 'node:test';
import {
- findUnenforceablePattern,
isAllowedUrl,
isLocalhost,
validateUrl,
+ validateUrlPatterns,
} from '../../src/utils/url.js';
describe('isLocalhost', () => {
@@ -565,40 +565,52 @@ describe('isAllowedUrl', () => {
});
});
-describe('findUnenforceablePattern', () => {
- it('flags a regexp group', () => {
+describe('validateUrlPatterns', () => {
+ it('rejects a regexp group with a specific error message', () => {
for (const pattern of [
String.raw`*://(127\.\d+\.\d+\.\d+):*/*`,
'*://example.com/(foo|bar)',
'(http|https)://example.com/*',
]) {
- assert.strictEqual(findUnenforceablePattern([pattern]), pattern);
+ assert.throws(
+ () => validateUrlPatterns([pattern], '--blockedUrlPattern'),
+ new Error(
+ `Invalid --blockedUrlPattern "${pattern}": a regexp group is not enforced on redirects or subresources. Use an exact value or a "*" wildcard instead.`,
+ ),
+ );
}
});
- it('returns the first offending pattern among several', () => {
- assert.strictEqual(
- findUnenforceablePattern([
- '*://127.0.0.1:*/*',
- '*://*.example.com/*',
- String.raw`*://(localhost|127\.0\.0\.1):*/*`,
- ]),
- String.raw`*://(localhost|127\.0\.0\.1):*/*`,
+ it('throws on the first offending pattern among several', () => {
+ const offending = String.raw`*://(localhost|127\.0\.0\.1):*/*`;
+ assert.throws(
+ () =>
+ validateUrlPatterns(
+ ['*://127.0.0.1:*/*', '*://*.example.com/*', offending],
+ '--allowedUrlPattern',
+ ),
+ new Error(
+ `Invalid --allowedUrlPattern "${offending}": a regexp group is not enforced on redirects or subresources. Use an exact value or a "*" wildcard instead.`,
+ ),
);
});
it('allows plain wildcard patterns', () => {
- assert.strictEqual(
- findUnenforceablePattern([
- '*://127.0.0.1:*/*',
- '*://*.example.com/*',
- 'https://127.0.0.1:8080/secret',
- ]),
- undefined,
+ assert.doesNotThrow(() =>
+ validateUrlPatterns(
+ [
+ '*://127.0.0.1:*/*',
+ '*://127.0.0.1/*',
+ '*://*.example.com/*',
+ '*://*.123.example.com/*',
+ 'https://127.0.0.1:8080/secret',
+ ],
+ '--blockedUrlPattern',
+ ),
);
});
- it('flags a named group in any component', () => {
+ it('rejects a named group in any component with a specific error message', () => {
for (const pattern of [
'*://127.0.0.1::port/secret',
'*://127.0.0.1:{:port}/secret',
@@ -614,38 +626,78 @@ describe('findUnenforceablePattern', () => {
'*://example.com/path?token=:secret',
'*://example.com/*#:hash',
]) {
- assert.strictEqual(findUnenforceablePattern([pattern]), pattern);
+ assert.throws(
+ () => validateUrlPatterns([pattern], '--blockedUrlPattern'),
+ new Error(
+ `Invalid --blockedUrlPattern "${pattern}": a ":name" named group is not enforced on redirects or subresources. Use an exact value or a "*" wildcard instead.`,
+ ),
+ );
+ }
+ });
+
+ it('rejects a partial IPv4 wildcard in the hostname with a specific error message', () => {
+ for (const pattern of [
+ '*://127.0.0.*/*',
+ '*://127.*:*/*',
+ '*://*.0.0.1/*',
+ '*://127.*.0.1/*',
+ '*://127.0.0*/*',
+ '*://127.0.0.{*}/*',
+ '*://{127.0.0.*}/*',
+ '*://{*.0.0.1}/*',
+ '*://*1*/*',
+ '*://0x7f.0.0.*/*',
+ '*://0x7f.*/*',
+ '*://user:[email protected].*/*',
+ '*://user:[email protected].*:8080/*',
+ String.raw`*://127.0.0.*\:8080/*`,
+ String.raw`*://127.0.\0.*/*`,
+ ]) {
+ assert.throws(
+ () => validateUrlPatterns([pattern], '--blockedUrlPattern'),
+ new Error(
+ `Invalid --blockedUrlPattern "${pattern}": a partial IPv4 wildcard is not enforced on redirects or subresources. Use an exact IPv4 address or a "*" wildcard instead.`,
+ ),
+ );
}
});
it('allows escaped colons unless an unescaped named group is also present', () => {
- assert.strictEqual(
- findUnenforceablePattern([
- String.raw`http://[\:\:1]:8080/*`,
- String.raw`*://example.com/path?foo=a\:b#bar\:c`,
- String.raw`*://example.com/path?foo=a\\\:b#bar\\\:c`,
- ]),
- undefined,
+ assert.doesNotThrow(() =>
+ validateUrlPatterns(
+ [
+ String.raw`http://[\:\:1]:8080/*`,
+ String.raw`*://example.com/path?foo=a\:b#bar\:c`,
+ String.raw`*://example.com/path?foo=a\\\:b#bar\\\:c`,
+ ],
+ '--blockedUrlPattern',
+ ),
);
- assert.strictEqual(
- findUnenforceablePattern([
- String.raw`*://example.com/path?foo=a\:b&bar=:baz`,
- ]),
+ for (const pattern of [
String.raw`*://example.com/path?foo=a\:b&bar=:baz`,
- );
- assert.strictEqual(
- findUnenforceablePattern([String.raw`*://example.com/path?foo\\:bar`]),
String.raw`*://example.com/path?foo\\:bar`,
- );
+ ]) {
+ assert.throws(
+ () => validateUrlPatterns([pattern], '--blockedUrlPattern'),
+ new Error(
+ `Invalid --blockedUrlPattern "${pattern}": a ":name" named group is not enforced on redirects or subresources. Use an exact value or a "*" wildcard instead.`,
+ ),
+ );
+ }
});
it('throws for a pattern that fails to construct', () => {
- assert.throws(() =>
- findUnenforceablePattern(['*://example.com/(unterminated']),
+ assert.throws(
+ () =>
+ validateUrlPatterns(
+ ['*://example.com/(unterminated'],
+ '--blockedUrlPattern',
+ ),
+ /Invalid --blockedUrlPattern "\*:\/\/example\.com\/\(unterminated":/,
);
});
- it('returns undefined for an empty list', () => {
- assert.strictEqual(findUnenforceablePattern([]), undefined);
+ it('allows an empty list', () => {
+ assert.doesNotThrow(() => validateUrlPatterns([], '--blockedUrlPattern'));
});
});