Skip to content

Commit ac182cc

Browse files
committed
fix: restrict URL schemes in page navigation to http/https
The new_page and navigate_page tools pass user-provided URLs directly to page.goto() without validating the URL scheme. This allows navigating to potentially dangerous schemes like file://, chrome://, or data: URLs. Add URL scheme validation that restricts navigation to http:, https:, and about: schemes.
1 parent 8d765c0 commit ac182cc

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/tools/pages.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ import {zod} from '../third_party/index.js';
1111
import {ToolCategory} from './categories.js';
1212
import {CLOSE_PAGE_ERROR, defineTool, timeoutSchema} from './ToolDefinition.js';
1313

14+
const ALLOWED_URL_SCHEMES = ['http:', 'https:', 'about:'];
15+
16+
function validateUrlScheme(url: string): void {
17+
let parsed: URL;
18+
try {
19+
parsed = new URL(url);
20+
} catch {
21+
// If it can't be parsed as a URL, let the browser handle it
22+
// (e.g. relative paths or malformed input)
23+
return;
24+
}
25+
if (!ALLOWED_URL_SCHEMES.includes(parsed.protocol)) {
26+
throw new Error(
27+
`URL scheme "${parsed.protocol}" is not allowed. Allowed schemes: ${ALLOWED_URL_SCHEMES.join(', ')}`,
28+
);
29+
}
30+
}
31+
1432
export const listPages = defineTool({
1533
name: 'list_pages',
1634
description: `Get a list of pages open in the browser.`,
@@ -96,6 +114,7 @@ export const newPage = defineTool({
96114
...timeoutSchema,
97115
},
98116
handler: async (request, response, context) => {
117+
validateUrlScheme(request.params.url);
99118
const page = await context.newPage(request.params.background);
100119

101120
await context.waitForEventsAfterAction(
@@ -193,6 +212,7 @@ export const navigatePage = defineTool({
193212
'A URL is required for navigation of type=url.',
194213
);
195214
}
215+
validateUrlScheme(request.params.url);
196216
try {
197217
await page.goto(request.params.url, options);
198218
response.appendResponseLine(

0 commit comments

Comments
 (0)