Skip to content

Chrome DevTools for coding agents

fix(network): enforce URL policy across redirects (#2767)

Robert McConnell274cd4cdba92
diff --git a/src/McpContext.ts b/src/McpContext.ts
index c1ee385a..818e7c0a 100644
--- a/src/McpContext.ts
+++ b/src/McpContext.ts
@@ -923,6 +923,39 @@ export class McpContext implements Context {
throw new Error(`Not allowed by allowlist: ${url}`);
}
+ async #loadNetworkResource(url: URL): Promise<string> {
+ const redirectStatuses = new Set([301, 302, 303, 307, 308]);
+ const maxRedirects = 20;
+ let currentUrl = url;
+
+ for (let redirectCount = 0; ; redirectCount++) {
+ this.#validateUrlNotBlocked(currentUrl);
+ this.#validateUrlAllowed(currentUrl);
+
+ const response = await fetch(currentUrl, {redirect: 'manual'});
+ if (!redirectStatuses.has(response.status)) {
+ if (!response.ok) {
+ throw new Error(`Failed to load resource: ${currentUrl}`);
+ }
+ return await response.text();
+ }
+
+ const location = response.headers.get('location');
+ if (!location) {
+ throw new Error(`Failed to load resource: ${currentUrl}`);
+ }
+ if (redirectCount >= maxRedirects) {
+ throw new Error(`Too many redirects loading resource: ${url}`);
+ }
+
+ await response.body?.cancel();
+ currentUrl = new URL(location, currentUrl);
+ if (currentUrl.protocol !== 'https:' && currentUrl.protocol !== 'http:') {
+ throw new Error(`Unsupported protocol for: ${currentUrl}`);
+ }
+ }
+ }
+
async loadResource(path: string): Promise<string> {
const url = new URL(path);
@@ -931,13 +964,7 @@ export class McpContext implements Context {
switch (url.protocol) {
case 'https:':
case 'http:': {
- this.#validateUrlAllowed(url);
-
- const response = await fetch(url);
- if (!response.ok) {
- throw new Error(`Failed to load resource: ${url}`);
- }
- return await response.text();
+ return await this.#loadNetworkResource(url);
}
case 'file:': {
diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.ts
index b2a24d48..a1193dc0 100644
--- a/tests/McpContext.test.ts
+++ b/tests/McpContext.test.ts
@@ -22,11 +22,12 @@ import {type HTTPResponse} from '../src/third_party/index.js';
import type {TraceResult} from '../src/processors/PerformanceTrace.js';
import {resolveCanonicalPath} from '../src/utils/files.js';
-import {serverHooks} from './server.js';
+import {serverHooks, TestServer} from './server.js';
import {
assertNoServiceWorkerReported,
createTempDir,
createTempFile,
+ getMockBrowser,
getMockRequest,
html,
withBrowser,
@@ -759,6 +760,40 @@ describe('McpContext', () => {
});
describe('https protocol', () => {
+ async function createNetworkPolicyContext(options: {
+ allowlist?: string[];
+ blocklist?: string[];
+ }) {
+ const browser = getMockBrowser();
+ browser.pages = async () => [];
+ browser.targets = () => [];
+ return await McpContext.from(
+ browser,
+ undefined,
+ {
+ experimentalDevToolsDebugging: false,
+ performanceCrux: false,
+ ...options,
+ },
+ Locator,
+ );
+ }
+
+ async function withServers<T>(
+ servers: TestServer[],
+ callback: () => Promise<T>,
+ ): Promise<T> {
+ for (const server of servers) {
+ await server.start();
+ }
+ try {
+ return await callback();
+ } finally {
+ for (const server of servers.reverse()) {
+ await server.stop();
+ }
+ }
+ }
it('respects blocklist by throwing if blocked', async () => {
await withMcpContext(
async (_response, context) => {
@@ -822,6 +857,123 @@ describe('McpContext', () => {
},
);
});
+
+ it('rejects redirects outside the allowlist', async () => {
+ const allowedServer = new TestServer(TestServer.randomPort());
+ const forbiddenServer = new TestServer(TestServer.randomPort());
+ forbiddenServer.addRoute('/secret', (_req, res) => {
+ res.end('FORBIDDEN_BODY');
+ });
+ allowedServer.addRoute('/redirect', (_req, res) => {
+ res.writeHead(302, {
+ Location: forbiddenServer.getRoute('/secret'),
+ });
+ res.end();
+ });
+ await withServers([allowedServer, forbiddenServer], async () => {
+ const context = await createNetworkPolicyContext({
+ allowlist: [allowedServer.getRoute('/redirect')],
+ });
+ try {
+ await assert.rejects(
+ () => context.loadResource(allowedServer.getRoute('/redirect')),
+ /Not allowed by allowlist/,
+ );
+ } finally {
+ context.dispose();
+ }
+ });
+ });
+
+ it('rejects redirects into the blocklist', async () => {
+ const allowedServer = new TestServer(TestServer.randomPort());
+ const forbiddenServer = new TestServer(TestServer.randomPort());
+ forbiddenServer.addRoute('/secret', (_req, res) => {
+ res.end('FORBIDDEN_BODY');
+ });
+ allowedServer.addRoute('/redirect', (_req, res) => {
+ res.writeHead(302, {
+ Location: forbiddenServer.getRoute('/secret'),
+ });
+ res.end();
+ });
+ await withServers([allowedServer, forbiddenServer], async () => {
+ const context = await createNetworkPolicyContext({
+ blocklist: [forbiddenServer.getRoute('/secret')],
+ });
+ try {
+ await assert.rejects(
+ () => context.loadResource(allowedServer.getRoute('/redirect')),
+ /Blocked by blocklist/,
+ );
+ } finally {
+ context.dispose();
+ }
+ });
+ });
+
+ it('rejects a blocked intermediate redirect hop', async () => {
+ const startServer = new TestServer(TestServer.randomPort());
+ const blockedServer = new TestServer(TestServer.randomPort());
+ const finalServer = new TestServer(TestServer.randomPort());
+ finalServer.addRoute('/final', (_req, res) => {
+ res.end('FINAL_BODY');
+ });
+ blockedServer.addRoute('/bounce', (_req, res) => {
+ res.writeHead(302, {
+ Location: finalServer.getRoute('/final'),
+ });
+ res.end();
+ });
+ startServer.addRoute('/redirect', (_req, res) => {
+ res.writeHead(302, {
+ Location: blockedServer.getRoute('/bounce'),
+ });
+ res.end();
+ });
+ await withServers(
+ [startServer, blockedServer, finalServer],
+ async () => {
+ const context = await createNetworkPolicyContext({
+ blocklist: [blockedServer.getRoute('/bounce')],
+ });
+ try {
+ await assert.rejects(
+ () => context.loadResource(startServer.getRoute('/redirect')),
+ /Blocked by blocklist/,
+ );
+ } finally {
+ context.dispose();
+ }
+ },
+ );
+ });
+
+ it('follows redirects when every hop is allowed', async () => {
+ const server = new TestServer(TestServer.randomPort());
+ server.addRoute('/final', (_req, res) => {
+ res.end('ALLOWED_BODY');
+ });
+ server.addRoute('/redirect', (_req, res) => {
+ res.writeHead(302, {
+ Location: server.getRoute('/final'),
+ });
+ res.end();
+ });
+ await withServers([server], async () => {
+ const context = await createNetworkPolicyContext({
+ allowlist: [server.baseUrl + '/*'],
+ });
+ try {
+ assert.strictEqual(
+ await context.loadResource(server.getRoute('/redirect')),
+ 'ALLOWED_BODY',
+ );
+ } finally {
+ context.dispose();
+ }
+ });
+ });
});
describe('getDevToolsData', () => {