Skip to content

Chrome DevTools for coding agents

fix: reject named groups in URL patterns (#2857)

Natasha Gorshunova5b2c6f97a7f7
diff --git a/docs/configuration.md b/docs/configuration.md
index 822b9729..67389cf0 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 in any component (for example `(127\.\d+\.\d+\.\d+)` in the hostname) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*`/`:name` 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 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.
- **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 in any component (for example `(127\.\d+\.\d+\.\d+)` in the hostname) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*`/`:name` 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 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.
- **Type:** array
- **`--screenshotFormat`/ `--screenshot-format`**
diff --git a/src/config/puppeteer-options.ts b/src/config/puppeteer-options.ts
index 21eefd7e..cf6891de 100644
--- a/src/config/puppeteer-options.ts
+++ b/src/config/puppeteer-options.ts
@@ -39,7 +39,7 @@ 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 in any component (for example `(127\\.\\d+\\.\\d+\\.\\d+)` in the hostname) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*`/`:name` 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 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.",
coerce: (arg: string[] | undefined) => {
if (arg === undefined || arg.length === 0) {
return undefined;
@@ -47,7 +47,7 @@ export const puppeteerOptions = {
const pattern = findUnenforceablePattern(arg);
if (pattern) {
throw new Error(
- `Invalid --blockedUrlPattern "${pattern}": a regexp group is not enforced on redirects or subresources. Use an exact value or a "*"/":name" wildcard instead.`,
+ `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.`,
);
}
return arg;
@@ -57,7 +57,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 in any component (for example `(127\\.\\d+\\.\\d+\\.\\d+)` in the hostname) is rejected, because it is not enforced on redirects or subresources; use an exact value or a `*`/`:name` 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 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.",
coerce: (arg: string[] | undefined) => {
if (arg === undefined) {
return undefined;
@@ -70,7 +70,7 @@ export const puppeteerOptions = {
const pattern = findUnenforceablePattern(arg);
if (pattern) {
throw new Error(
- `Invalid --allowedUrlPattern "${pattern}": a regexp group is not enforced on redirects or subresources. Use an exact value or a "*"/":name" wildcard instead.`,
+ `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.`,
);
}
return arg;
diff --git a/src/utils/url.ts b/src/utils/url.ts
index 602c6c00..95f35079 100644
--- a/src/utils/url.ts
+++ b/src/utils/url.ts
@@ -114,18 +114,20 @@ export function isAllowedUrl(
const DISALLOWED_PROTOCOLS = new Set(['javascript:', 'data:', 'vbscript:']);
/**
- * Finds the first pattern that uses a URLPattern regexp group (for example
- * `(127\.\d+\.\d+\.\d+)`) in any component -- protocol, username, password,
- * hostname, port, pathname, search, or hash. Chromium's
- * `SimpleUrlPatternMatcher::Component::Create` rejects any component whose
- * `HasRegexGroups()` is true and silently drops the rule, so
- * `Network.emulateNetworkConditionsByRule` does not enforce these patterns
- * on redirects or subresources, unlike the initial navigation check. A plain
- * wildcard (`*`) or named group (`:name`) has no regexp group and is
- * unaffected.
+ * Checks whether a compiled URLPattern component contains an unescaped `:`
+ * (which always represents a `:name` group in URLPattern syntax).
+ */
+function hasNamedGroup(component: string): boolean {
+ return component.replaceAll('\\\\', '').replaceAll('\\:', '').includes(':');
+}
+
+/**
+ * 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/*`).
*
* @param patterns The `--blockedUrlPattern`/`--allowedUrlPattern` values to check.
- * @returns The first unenforceable pattern, or undefined if all are safe.
+ * @returns The first unenforceable pattern, or undefined if all are valid.
* @throws Error if a pattern's syntax is invalid (via `new URLPattern`).
*/
export function findUnenforceablePattern(
@@ -136,6 +138,19 @@ export function findUnenforceablePattern(
if (parsed.hasRegExpGroups) {
return raw;
}
+ const components = [
+ parsed.protocol,
+ parsed.username,
+ parsed.password,
+ parsed.hostname,
+ parsed.port,
+ parsed.pathname,
+ parsed.search,
+ parsed.hash,
+ ];
+ if (components.some(hasNamedGroup)) {
+ return raw;
+ }
}
return undefined;
}
diff --git a/tests/cli.test.ts b/tests/cli.test.ts
index 9d0b589e..82e73714 100644
--- a/tests/cli.test.ts
+++ b/tests/cli.test.ts
@@ -499,48 +499,60 @@ describe('cli args parsing', () => {
]);
});
- it('rejects a blocked-url-pattern with a regexp group', async () => {
+ it('rejects url-pattern with a regexp group or named group', async () => {
assert.throws(
() =>
parseConfig([
- String.raw`--blocked-url-pattern=*://(127\.\d+\.\d+\.\d+):*/*`,
+ String.raw`--blockedUrlPattern=*://(127\.\d+\.\d+\.\d+):*/*`,
]),
- /Invalid --blockedUrlPattern .*a regexp group is not enforced/,
+ /Invalid --blockedUrlPattern .*is not enforced/,
);
assert.throws(
() =>
parseConfig([
- '--blocked-url-pattern=https://a.com/*',
- String.raw`--blocked-url-pattern=*://example.com/(foo|bar)`,
+ String.raw`--allowedUrlPattern=*://(127\.\d+\.\d+\.\d+):*/*`,
]),
- /Invalid --blockedUrlPattern .*a regexp group is not enforced/,
+ /Invalid --allowedUrlPattern .*is not enforced/,
+ );
+
+ assert.throws(
+ () => parseConfig(['--blockedUrlPattern=*://127.0.0.1::port/secret']),
+ /Invalid --blockedUrlPattern .*is not enforced/,
+ );
+
+ assert.throws(
+ () => parseConfig(['--allowedUrlPattern=*://127.0.0.1::port/secret']),
+ /Invalid --allowedUrlPattern .*is not enforced/,
);
});
- it('rejects an allowed-url-pattern with a regexp group', async () => {
+ it('rejects when any pattern in multiple url patterns is unenforceable', async () => {
assert.throws(
() =>
parseConfig([
- String.raw`--allowed-url-pattern=*://(127\.\d+\.\d+\.\d+):*/*`,
+ '--blocked-url-pattern=https://a.com/*',
+ String.raw`--blocked-url-pattern=*://example.com/(foo|bar)`,
]),
- /Invalid --allowedUrlPattern .*a regexp group is not enforced/,
+ /Invalid --blockedUrlPattern .*is not enforced/,
);
-
assert.throws(
() =>
parseConfig([
'--allowed-url-pattern=https://a.com/*',
- String.raw`--allowed-url-pattern=(http|https)://example.com/*`,
+ String.raw`--allowed-url-pattern=*://example.com/(foo|bar)`,
]),
- /Invalid --allowedUrlPattern .*a regexp group is not enforced/,
+ /Invalid --allowedUrlPattern .*is not enforced/,
);
});
- it('rejects a blocked-url-pattern with invalid syntax', async () => {
+ it('rejects url-pattern with invalid syntax', async () => {
assert.throws(() =>
parseConfig(['--blocked-url-pattern=*://example.com/(unterminated']),
);
+ assert.throws(() =>
+ parseConfig(['--allowed-url-pattern=*://example.com/(unterminated']),
+ );
});
it('strips an empty blocked-url-pattern', async () => {
diff --git a/tests/utils/url.test.ts b/tests/utils/url.test.ts
index db4ba599..b5eeeb5b 100644
--- a/tests/utils/url.test.ts
+++ b/tests/utils/url.test.ts
@@ -566,11 +566,14 @@ describe('isAllowedUrl', () => {
});
describe('findUnenforceablePattern', () => {
- it('flags a hostname regexp group', () => {
- assert.strictEqual(
- findUnenforceablePattern([String.raw`*://(127\.\d+\.\d+\.\d+):*/*`]),
+ it('flags a regexp group', () => {
+ for (const pattern of [
String.raw`*://(127\.\d+\.\d+\.\d+):*/*`,
- );
+ '*://example.com/(foo|bar)',
+ '(http|https)://example.com/*',
+ ]) {
+ assert.strictEqual(findUnenforceablePattern([pattern]), pattern);
+ }
});
it('returns the first offending pattern among several', () => {
@@ -584,56 +587,55 @@ describe('findUnenforceablePattern', () => {
);
});
- it('allows an exact hostname', () => {
+ it('allows plain wildcard patterns', () => {
assert.strictEqual(
- findUnenforceablePattern(['*://127.0.0.1:*/*']),
+ findUnenforceablePattern([
+ '*://127.0.0.1:*/*',
+ '*://*.example.com/*',
+ 'https://127.0.0.1:8080/secret',
+ ]),
undefined,
);
});
- it('allows a wildcard hostname', () => {
- assert.strictEqual(
- findUnenforceablePattern(['*://*.example.com/*']),
- undefined,
- );
+ it('flags a named group in any component', () => {
+ for (const pattern of [
+ '*://127.0.0.1::port/secret',
+ '*://127.0.0.1:{:port}/secret',
+ ':proto://example.com/*',
+ '*://:[email protected]/*',
+ '*://user::[email protected]/*',
+ '*://:sub.example.com/*',
+ '*://{:sub}.example.com/*',
+ '*://127.0.0.:oct:*/blocked/:page',
+ '*://example.com/:path',
+ '*://example.com/{:path}',
+ '*://example.com/path?:query',
+ '*://example.com/path?token=:secret',
+ '*://example.com/*#:hash',
+ ]) {
+ assert.strictEqual(findUnenforceablePattern([pattern]), pattern);
+ }
});
- it('allows a named group hostname', () => {
+ it('allows escaped colons unless an unescaped named group is also present', () => {
assert.strictEqual(
- findUnenforceablePattern(['*://:sub.example.com/*']),
+ 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,
);
- });
-
- it('flags a regexp group in the pathname', () => {
assert.strictEqual(
- findUnenforceablePattern(['*://example.com/(foo|bar)']),
- '*://example.com/(foo|bar)',
- );
- });
-
- it('flags a regexp group in the protocol', () => {
- assert.strictEqual(
- findUnenforceablePattern(['(http|https)://example.com/*']),
- '(http|https)://example.com/*',
- );
- });
-
- it('flags a regexp group in the port', () => {
- assert.strictEqual(
- findUnenforceablePattern(['*://example.com:(80|443)/*']),
- '*://example.com:(80|443)/*',
- );
- });
-
- it('flags a regexp group in the search or hash', () => {
- assert.strictEqual(
- findUnenforceablePattern(['*://example.com/*?(foo|bar)']),
- '*://example.com/*?(foo|bar)',
+ findUnenforceablePattern([
+ String.raw`*://example.com/path?foo=a\:b&bar=:baz`,
+ ]),
+ String.raw`*://example.com/path?foo=a\:b&bar=:baz`,
);
assert.strictEqual(
- findUnenforceablePattern(['*://example.com/*#(foo|bar)']),
- '*://example.com/*#(foo|bar)',
+ findUnenforceablePattern([String.raw`*://example.com/path?foo\\:bar`]),
+ String.raw`*://example.com/path?foo\\:bar`,
);
});