mirror of
https://github.com/microsoft/playwright-mcp.git
synced 2025-07-27 00:52:27 +08:00
feat: add --(allowed|blocked)-origins (#319)
Useful to limit the agent when using the playwright-mcp server with an agent in auto-invocation mode. Not intended to be a security feature.
This commit is contained in:
parent
4694d60fc5
commit
42faa3ccf8
11
README.md
11
README.md
@ -76,6 +76,8 @@ The Playwright MCP server supports the following command-line options:
|
|||||||
- `--user-data-dir <path>`: Path to the user data directory
|
- `--user-data-dir <path>`: Path to the user data directory
|
||||||
- `--port <port>`: Port to listen on for SSE transport
|
- `--port <port>`: Port to listen on for SSE transport
|
||||||
- `--host <host>`: Host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.
|
- `--host <host>`: Host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.
|
||||||
|
- `--allowed-origins <origins>`: Semicolon-separated list of origins to allow the browser to request. Default is to allow all. Origins matching both `--allowed-origins` and `--blocked-origins` will be blocked.
|
||||||
|
- `--blocked-origins <origins>`: Semicolon-separated list of origins to block the browser to request. Origins matching both `--allowed-origins` and `--blocked-origins` will be blocked.
|
||||||
- `--vision`: Run server that uses screenshots (Aria snapshots are used by default)
|
- `--vision`: Run server that uses screenshots (Aria snapshots are used by default)
|
||||||
- `--output-dir`: Directory for output files
|
- `--output-dir`: Directory for output files
|
||||||
- `--config <path>`: Path to the configuration file
|
- `--config <path>`: Path to the configuration file
|
||||||
@ -153,6 +155,15 @@ The Playwright MCP server can be configured using a JSON configuration file. Her
|
|||||||
// Directory for output files
|
// Directory for output files
|
||||||
outputDir?: string;
|
outputDir?: string;
|
||||||
|
|
||||||
|
// Network configuration
|
||||||
|
network?: {
|
||||||
|
// List of origins to allow the browser to request. Default is to allow all. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
|
||||||
|
allowedOrigins?: string[];
|
||||||
|
|
||||||
|
// List of origins to block the browser to request. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
|
||||||
|
blockedOrigins?: string[];
|
||||||
|
};
|
||||||
|
|
||||||
// Tool-specific configurations
|
// Tool-specific configurations
|
||||||
tools?: {
|
tools?: {
|
||||||
browser_take_screenshot?: {
|
browser_take_screenshot?: {
|
||||||
|
12
config.d.ts
vendored
12
config.d.ts
vendored
@ -94,6 +94,18 @@ export type Config = {
|
|||||||
*/
|
*/
|
||||||
outputDir?: string;
|
outputDir?: string;
|
||||||
|
|
||||||
|
network?: {
|
||||||
|
/**
|
||||||
|
* List of origins to allow the browser to request. Default is to allow all. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
|
||||||
|
*/
|
||||||
|
allowedOrigins?: string[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of origins to block the browser to request. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked.
|
||||||
|
*/
|
||||||
|
blockedOrigins?: string[];
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration for specific tools.
|
* Configuration for specific tools.
|
||||||
*/
|
*/
|
||||||
|
@ -36,6 +36,8 @@ export type CLIOptions = {
|
|||||||
host?: string;
|
host?: string;
|
||||||
vision?: boolean;
|
vision?: boolean;
|
||||||
config?: string;
|
config?: string;
|
||||||
|
allowedOrigins?: string[];
|
||||||
|
blockedOrigins?: string[];
|
||||||
outputDir?: string;
|
outputDir?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -50,6 +52,10 @@ const defaultConfig: Config = {
|
|||||||
viewport: null,
|
viewport: null,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
network: {
|
||||||
|
allowedOrigins: undefined,
|
||||||
|
blockedOrigins: undefined,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function resolveConfig(cliOptions: CLIOptions): Promise<Config> {
|
export async function resolveConfig(cliOptions: CLIOptions): Promise<Config> {
|
||||||
@ -110,6 +116,10 @@ export async function configFromCLIOptions(cliOptions: CLIOptions): Promise<Conf
|
|||||||
},
|
},
|
||||||
capabilities: cliOptions.caps?.split(',').map((c: string) => c.trim() as ToolCapability),
|
capabilities: cliOptions.caps?.split(',').map((c: string) => c.trim() as ToolCapability),
|
||||||
vision: !!cliOptions.vision,
|
vision: !!cliOptions.vision,
|
||||||
|
network: {
|
||||||
|
allowedOrigins: cliOptions.allowedOrigins,
|
||||||
|
blockedOrigins: cliOptions.blockedOrigins,
|
||||||
|
},
|
||||||
outputDir: cliOptions.outputDir,
|
outputDir: cliOptions.outputDir,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -171,5 +181,9 @@ function mergeConfig(base: Config, overrides: Config): Config {
|
|||||||
...pickDefined(base),
|
...pickDefined(base),
|
||||||
...pickDefined(overrides),
|
...pickDefined(overrides),
|
||||||
browser,
|
browser,
|
||||||
|
network: {
|
||||||
|
...pickDefined(base.network),
|
||||||
|
...pickDefined(overrides.network),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -290,11 +290,26 @@ ${code.join('\n')}
|
|||||||
}).catch(() => {});
|
}).catch(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async _setupRequestInterception(context: playwright.BrowserContext) {
|
||||||
|
if (this.config.network?.allowedOrigins?.length) {
|
||||||
|
await context.route('**', route => route.abort('blockedbyclient'));
|
||||||
|
|
||||||
|
for (const origin of this.config.network.allowedOrigins)
|
||||||
|
await context.route(`*://${origin}/**`, route => route.continue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.config.network?.blockedOrigins?.length) {
|
||||||
|
for (const origin of this.config.network.blockedOrigins)
|
||||||
|
await context.route(`*://${origin}/**`, route => route.abort('blockedbyclient'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async _ensureBrowserContext() {
|
private async _ensureBrowserContext() {
|
||||||
if (!this._browserContext) {
|
if (!this._browserContext) {
|
||||||
const context = await this._createBrowserContext();
|
const context = await this._createBrowserContext();
|
||||||
this._browser = context.browser;
|
this._browser = context.browser;
|
||||||
this._browserContext = context.browserContext;
|
this._browserContext = context.browserContext;
|
||||||
|
await this._setupRequestInterception(this._browserContext);
|
||||||
for (const page of this._browserContext.pages())
|
for (const page of this._browserContext.pages())
|
||||||
this._onPageCreated(page);
|
this._onPageCreated(page);
|
||||||
this._browserContext.on('page', page => this._onPageCreated(page));
|
this._browserContext.on('page', page => this._onPageCreated(page));
|
||||||
|
@ -37,6 +37,8 @@ program
|
|||||||
.option('--user-data-dir <path>', 'Path to the user data directory')
|
.option('--user-data-dir <path>', 'Path to the user data directory')
|
||||||
.option('--port <port>', 'Port to listen on for SSE transport.')
|
.option('--port <port>', 'Port to listen on for SSE transport.')
|
||||||
.option('--host <host>', 'Host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.')
|
.option('--host <host>', 'Host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.')
|
||||||
|
.option('--allowed-origins <origins>', 'Semicolon-separated list of origins to allow the browser to request. Default is to allow all.', semicolonSeparatedList)
|
||||||
|
.option('--blocked-origins <origins>', 'Semicolon-separated list of origins to block the browser from requesting. Blocklist is evaluated before allowlist. If used without the allowlist, requests not matching the blocklist are still allowed.', semicolonSeparatedList)
|
||||||
.option('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
|
.option('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
|
||||||
.option('--output-dir <path>', 'Path to the directory for output files.')
|
.option('--output-dir <path>', 'Path to the directory for output files.')
|
||||||
.option('--config <path>', 'Path to the configuration file.')
|
.option('--config <path>', 'Path to the configuration file.')
|
||||||
@ -63,4 +65,8 @@ function setupExitWatchdog(serverList: ServerList) {
|
|||||||
process.on('SIGTERM', handleExit);
|
process.on('SIGTERM', handleExit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function semicolonSeparatedList(value: string): string[] {
|
||||||
|
return value.split(';').map(v => v.trim());
|
||||||
|
}
|
||||||
|
|
||||||
program.parse(process.argv);
|
program.parse(process.argv);
|
||||||
|
91
tests/request-blocking.spec.ts
Normal file
91
tests/request-blocking.spec.ts
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) Microsoft Corporation.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
||||||
|
import { test, expect } from './fixtures.ts';
|
||||||
|
|
||||||
|
const BLOCK_MESSAGE = /Blocked by Web Inspector|NS_ERROR_FAILURE|net::ERR_BLOCKED_BY_CLIENT/g;
|
||||||
|
|
||||||
|
const fetchPage = async (client: Client, url: string) => {
|
||||||
|
const result = await client.callTool({
|
||||||
|
name: 'browser_navigate',
|
||||||
|
arguments: {
|
||||||
|
url,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return JSON.stringify(result, null, 2);
|
||||||
|
};
|
||||||
|
|
||||||
|
test('default to allow all', async ({ server, client }) => {
|
||||||
|
server.route('/ppp', (_req, res) => {
|
||||||
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||||
|
res.end('content:PPP');
|
||||||
|
});
|
||||||
|
const result = await fetchPage(client, server.PREFIX + '/ppp');
|
||||||
|
expect(result).toContain('content:PPP');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('blocked works', async ({ startClient }) => {
|
||||||
|
const client = await startClient({
|
||||||
|
args: ['--blocked-origins', 'microsoft.com;example.com;playwright.dev']
|
||||||
|
});
|
||||||
|
const result = await fetchPage(client, 'https://example.com/');
|
||||||
|
expect(result).toMatch(BLOCK_MESSAGE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('allowed works', async ({ server, startClient }) => {
|
||||||
|
server.route('/ppp', (_req, res) => {
|
||||||
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||||
|
res.end('content:PPP');
|
||||||
|
});
|
||||||
|
const client = await startClient({
|
||||||
|
args: ['--allowed-origins', `microsoft.com;${new URL(server.PREFIX).host};playwright.dev`]
|
||||||
|
});
|
||||||
|
const result = await fetchPage(client, server.PREFIX + '/ppp');
|
||||||
|
expect(result).toContain('content:PPP');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('blocked takes precedence', async ({ startClient }) => {
|
||||||
|
const client = await startClient({
|
||||||
|
args: [
|
||||||
|
'--blocked-origins', 'example.com',
|
||||||
|
'--allowed-origins', 'example.com',
|
||||||
|
],
|
||||||
|
});
|
||||||
|
const result = await fetchPage(client, 'https://example.com/');
|
||||||
|
expect(result).toMatch(BLOCK_MESSAGE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('allowed without blocked blocks all non-explicitly specified origins', async ({ startClient }) => {
|
||||||
|
const client = await startClient({
|
||||||
|
args: ['--allowed-origins', 'playwright.dev'],
|
||||||
|
});
|
||||||
|
const result = await fetchPage(client, 'https://example.com/');
|
||||||
|
expect(result).toMatch(BLOCK_MESSAGE);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('blocked without allowed allows non-explicitly specified origins', async ({ server, startClient }) => {
|
||||||
|
server.route('/ppp', (_req, res) => {
|
||||||
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||||
|
res.end('content:PPP');
|
||||||
|
});
|
||||||
|
const client = await startClient({
|
||||||
|
args: ['--blocked-origins', 'example.com'],
|
||||||
|
});
|
||||||
|
const result = await fetchPage(client, server.PREFIX + '/ppp');
|
||||||
|
expect(result).toContain('content:PPP');
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user