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
2 changes: 1 addition & 1 deletion packages/playwright/src/mcp/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type { MCPProvider } from './sdk/proxyBackend';

export function decorateCommand(command: Command, version: string) {
command
.option('--allowed-hosts <hosts...>', 'comma-separated list of hosts this server is allowed to serve from. Defaults to the host the server is bound to.', commaSeparatedList)
.option('--allowed-hosts <hosts...>', 'comma-separated list of hosts this server is allowed to serve from. Defaults to the host the server is bound to. Pass \'*\' to disable the host check.', commaSeparatedList)
.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('--block-service-workers', 'block service workers')
Expand Down
23 changes: 13 additions & 10 deletions packages/playwright/src/mcp/sdk/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,24 @@ export async function installHttpTransport(httpServer: http.Server, serverBacken
const url = httpAddressToString(httpServer.address());
const host = new URL(url).host;
allowedHosts = (allowedHosts || [host]).map(h => h.toLowerCase());
const allowAnyHost = allowedHosts.includes('*');

const sseSessions = new Map();
const streamableSessions = new Map();
httpServer.on('request', async (req, res) => {
const host = req.headers.host?.toLowerCase();
if (!host) {
res.statusCode = 400;
return res.end('Missing host');
}
if (!allowAnyHost) {
const host = req.headers.host?.toLowerCase();
if (!host) {
res.statusCode = 400;
return res.end('Missing host');
}

// Prevent DNS evil.com -> localhost rebind.
if (!allowedHosts.includes(host)) {
// Access from the browser is forbidden.
res.statusCode = 403;
return res.end('Access is only allowed at ' + allowedHosts.join(', '));
// Prevent DNS evil.com -> localhost rebind.
if (!allowedHosts.includes(host)) {
// Access from the browser is forbidden.
res.statusCode = 403;
return res.end('Access is only allowed at ' + allowedHosts.join(', '));
}
}

const url = new URL(`http://localhost${req.url}`);
Expand Down
8 changes: 8 additions & 0 deletions tests/mcp/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@ test('should respect allowed hosts (positive)', async ({ serverEndpoint }) => {
expect(response.status).toBe(400);
});

test('should be able to allow any host', async ({ serverEndpoint }) => {
const { url } = await serverEndpoint({ args: ['--allowed-hosts=*'] });
const response = await fetch(url.href);
// 400 is expected for the mcp fetch.
expect(response.status).toBe(400);
expect(await response.text()).toBe('Invalid request');
});

async function findFreePort(): Promise<number> {
return new Promise((resolve, reject) => {
const server = net.createServer();
Expand Down
Loading