Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(mcp): use protocol blocklist instead of allowlist (#39600)

Pavel Feldman9d698703a32c
diff --git a/packages/playwright-core/src/client/browserContext.ts b/packages/playwright-core/src/client/browserContext.ts
index 5aa0321036..99c006eb81 100644
--- a/packages/playwright-core/src/client/browserContext.ts
+++ b/packages/playwright-core/src/client/browserContext.ts
@@ -79,7 +79,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
private _closeReason: string | undefined;
private _harRouters: HarRouter[] = [];
private _onRecorderEventSink: RecorderEventSink | undefined;
- private _allowedProtocols: string[] | undefined;
+ private _disallowedProtocols: string[] | undefined;
private _allowedDirectories: string[] | undefined;
static from(context: channels.BrowserContextChannel): BrowserContext {
@@ -555,12 +555,12 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
await this._channel.exposeConsoleApi();
}
- _setAllowedProtocols(protocols: string[]) {
- this._allowedProtocols = protocols;
+ _setDisallowedProtocols(protocols: string[]) {
+ this._disallowedProtocols = protocols;
}
_checkUrlAllowed(url: string) {
- if (!this._allowedProtocols)
+ if (!this._disallowedProtocols)
return;
let parsedURL;
try {
@@ -568,8 +568,8 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
} catch (e) {
throw new Error(`Access to ${url} is blocked. Invalid URL: ${e.message}`);
}
- if (!this._allowedProtocols.includes(parsedURL.protocol))
- throw new Error(`Access to "${parsedURL.protocol}" URL is blocked. Allowed protocols: ${this._allowedProtocols.join(', ')}. Attempted URL: ${url}`);
+ if (this._disallowedProtocols.includes(parsedURL.protocol))
+ throw new Error(`Access to "${parsedURL.protocol}" protocol is blocked. Attempted URL: "${url}"`);
}
_setAllowedDirectories(rootDirectories: string[]) {
diff --git a/packages/playwright-core/src/tools/context.ts b/packages/playwright-core/src/tools/context.ts
index 992d7f7ece..730d252b65 100644
--- a/packages/playwright-core/src/tools/context.ts
+++ b/packages/playwright-core/src/tools/context.ts
@@ -287,7 +287,7 @@ export class Context {
selectors.setTestIdAttribute(this.config.testIdAttribute);
const browserContext = this._rawBrowserContext;
if (!this.config.allowUnrestrictedFileAccess) {
- (browserContext as any)._setAllowedProtocols(['http:', 'https:', 'about:', 'data:']);
+ (browserContext as any)._setDisallowedProtocols(['file:']);
(browserContext as any)._setAllowedDirectories([this.options.cwd]);
}
await this._setupRequestInterception(browserContext);
diff --git a/tests/mcp/core.spec.ts b/tests/mcp/core.spec.ts
index 87c7ed9772..731ac32ed2 100644
--- a/tests/mcp/core.spec.ts
+++ b/tests/mcp/core.spec.ts
@@ -37,7 +37,7 @@ test('browser_navigate blocks file:// URLs by default', async ({ client }) => {
name: 'browser_navigate',
arguments: { url: 'file:///etc/passwd' },
})).toHaveResponse({
- error: expect.stringContaining('Error: Access to "file:" URL is blocked. Allowed protocols: http:, https:, about:, data:. Attempted URL: file:///etc/passwd'),
+ error: expect.stringContaining('Error: Access to "file:" protocol is blocked. Attempted URL: "file:///etc/passwd"'),
isError: true,
});
});
diff --git a/tests/mcp/run-code.spec.ts b/tests/mcp/run-code.spec.ts
index bc673f2d88..b9ab561e9b 100644
--- a/tests/mcp/run-code.spec.ts
+++ b/tests/mcp/run-code.spec.ts
@@ -90,7 +90,7 @@ test('browser_run_code blocks fetch of file:// URLs by default', async ({ client
code: `async (page) => { await page.request.get('file:///etc/passwd'); }`,
},
})).toHaveResponse({
- error: expect.stringContaining('Error: apiRequestContext.get: Access to "file:" URL is blocked. Allowed protocols: http:, https:, about:, data:. Attempted URL: file:///etc/passwd'),
+ error: expect.stringContaining('Error: apiRequestContext.get: Access to "file:" protocol is blocked. Attempted URL: "file:///etc/passwd"'),
isError: true,
});
});