Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`**
Expand Down
8 changes: 4 additions & 4 deletions src/config/puppeteer-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ 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;
}
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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
35 changes: 25 additions & 10 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,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(
Expand All @@ -135,6 +137,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;
}
Expand Down
38 changes: 25 additions & 13 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,48 +480,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 () => {
Expand Down
84 changes: 43 additions & 41 deletions tests/utils/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,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', () => {
Expand All @@ -454,56 +457,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/*',
'*://:user@example.com/*',
'*://user::pass@example.com/*',
'*://: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`,
);
});

Expand Down
Loading