Skip to content

Chrome DevTools for coding agents

fix: respecte allow/block list in loadResouce (#2254)

Nikolay Vitkov6a9466378c13
diff --git a/src/McpContext.ts b/src/McpContext.ts
index fbf9e5dc..4fda83ea 100644
--- a/src/McpContext.ts
+++ b/src/McpContext.ts
@@ -64,8 +64,10 @@ interface McpContextOptions {
experimentalIncludeAllPages?: boolean;
// Whether CrUX data should be fetched.
performanceCrux: boolean;
- // Whether allowlist/blocklist is configured.
- hasNetworkBlockOrAllowlist?: boolean;
+ // The allow list of URL patterns to allow loading resources.
+ allowList?: string[];
+ // The block list of URL patterns to block loading resources.
+ blocklist?: string[];
}
const DEFAULT_TIMEOUT = 5_000;
@@ -347,6 +349,10 @@ export class McpContext implements Context {
await this.emulate(currentSetting, page.pptrPage);
}
+ get #hasNetworkBlockOrAllowlist(): boolean {
+ return !!(this.#options.allowList || this.#options.blocklist);
+ }
+
async emulate(
options: {
networkConditions?: string;
@@ -364,7 +370,7 @@ export class McpContext implements Context {
const newSettings: EmulationSettings = {...mcpPage.emulationSettings};
// Skip network emulation if blocklist/allowlist is configured, as it conflicts with blocking rules in Puppeteer.
- if (this.#options.hasNetworkBlockOrAllowlist) {
+ if (this.#hasNetworkBlockOrAllowlist) {
if (options.networkConditions !== undefined) {
throw new Error(
'Network throttling is not supported when network blocking (allowlist/blocklist) is configured.',
@@ -952,13 +958,41 @@ export class McpContext implements Context {
return await this.#heapSnapshotManager.getDominatorsOf(filePath, nodeId);
}
+ #validateUrlNotBlocked(url: URL): void {
+ if (!this.#options.blocklist) {
+ return;
+ }
+ for (const block of this.#options.blocklist) {
+ const pattern = new URLPattern(block);
+ if (pattern.test(url)) {
+ throw new Error(`Blocked by blocklist: ${url}`);
+ }
+ }
+ }
+
+ #validateUrlAllowed(url: URL): void {
+ if (!this.#options.allowList) {
+ return;
+ }
+ for (const allow of this.#options.allowList) {
+ const pattern = new URLPattern(allow);
+ if (pattern.test(url)) {
+ return;
+ }
+ }
+ throw new Error(`Not allowed by allowlist: ${url}`);
+ }
+
async loadResource(path: string): Promise<string> {
const url = new URL(path);
+ this.#validateUrlNotBlocked(url);
+
switch (url.protocol) {
case 'https:':
case 'http:': {
- // TODO: Verify allow/block list
+ this.#validateUrlAllowed(url);
+
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to load resource: ${url}`);
diff --git a/src/index.ts b/src/index.ts
index fe725af6..8270a5c7 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -144,10 +144,8 @@ export async function createMcpServer(
experimentalDevToolsDebugging: devtools,
experimentalIncludeAllPages: serverArgs.experimentalIncludeAllPages,
performanceCrux: serverArgs.performanceCrux,
- hasNetworkBlockOrAllowlist: Boolean(
- (blocklist && blocklist.length > 0) ||
- (allowlist && allowlist.length > 0),
- ),
+ allowList: allowlist,
+ blocklist: blocklist,
});
await updateRoots();
}
diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.ts
index 5424600c..9ecceaa7 100644
--- a/tests/McpContext.test.ts
+++ b/tests/McpContext.test.ts
@@ -300,20 +300,110 @@ describe('McpContext', () => {
});
describe('loadResource', () => {
- it('file protocol calls validatePath', async () => {
- await withMcpContext(async (_response, context) => {
- const validatePathSpy = sinon.spy(context, 'validatePath');
- const testFilePath = path.join(os.tmpdir(), 'load-resource-test.txt');
+ describe('file protocol', () => {
+ it('calls validatePath', async () => {
+ await withMcpContext(async (_response, context) => {
+ const validatePathSpy = sinon.spy(context, 'validatePath');
+ const testFilePath = path.join(os.tmpdir(), 'load-resource-test.txt');
+ await fs.writeFile(testFilePath, 'test content');
+ try {
+ const url = pathToFileURL(testFilePath).href;
+ const content = await context.loadResource(url);
+ assert.strictEqual(content, 'test content');
+ sinon.assert.calledWith(validatePathSpy, testFilePath);
+ } finally {
+ await fs.rm(testFilePath, {force: true});
+ }
+ });
+ });
+
+ it('is not blocked by allowlist', async () => {
+ const testFilePath = path.join(
+ os.tmpdir(),
+ 'load-resource-test-allow.txt',
+ );
await fs.writeFile(testFilePath, 'test content');
try {
- const url = pathToFileURL(testFilePath).href;
- const content = await context.loadResource(url);
- assert.strictEqual(content, 'test content');
- sinon.assert.calledWith(validatePathSpy, testFilePath);
+ await withMcpContext(
+ async (_response, context) => {
+ const url = pathToFileURL(testFilePath).href;
+ const content = await context.loadResource(url);
+ assert.strictEqual(content, 'test content');
+ },
+ {
+ allowedUrlPattern: ['https://example.com/allowed*'],
+ },
+ );
} finally {
await fs.rm(testFilePath, {force: true});
}
});
});
+
+ describe('https protocol', () => {
+ it('respects blocklist by throwing if blocked', async () => {
+ await withMcpContext(
+ async (_response, context) => {
+ await assert.rejects(() =>
+ context.loadResource('https://example.com/blocked'),
+ );
+ },
+ {
+ blockedUrlPattern: ['https://example.com/blocked*'],
+ },
+ );
+ });
+
+ it('respects blocklist by not throwing if not blocked', async () => {
+ await withMcpContext(
+ async (_response, context) => {
+ sinon.stub(globalThis, 'fetch').resolves({
+ ok: true,
+ text: async () => 'mock data',
+ } as Response);
+
+ const content = await context.loadResource(
+ 'https://example.com/allowed',
+ );
+ assert.strictEqual(content, 'mock data');
+ },
+ {
+ blockedUrlPattern: ['https://example.com/blocked*'],
+ },
+ );
+ });
+
+ it('respects allowlist by throwing if not allowed', async () => {
+ await withMcpContext(
+ async (_response, context) => {
+ await assert.rejects(() =>
+ context.loadResource('https://example.com/blocked'),
+ );
+ },
+ {
+ allowedUrlPattern: ['https://example.com/allowed*'],
+ },
+ );
+ });
+
+ it('respects allowlist by not throwing if allowed', async () => {
+ await withMcpContext(
+ async (_response, context) => {
+ sinon.stub(globalThis, 'fetch').resolves({
+ ok: true,
+ text: async () => 'mock data',
+ } as Response);
+
+ const content = await context.loadResource(
+ 'https://example.com/allowed',
+ );
+ assert.strictEqual(content, 'mock data');
+ },
+ {
+ allowedUrlPattern: ['https://example.com/allowed*'],
+ },
+ );
+ });
+ });
});
});
diff --git a/tests/utils.ts b/tests/utils.ts
index 71597903..465404f3 100644
--- a/tests/utils.ts
+++ b/tests/utils.ts
@@ -136,10 +136,8 @@ export async function withMcpContext(
{
experimentalDevToolsDebugging: false,
performanceCrux: options.performanceCrux ?? true,
- hasNetworkBlockOrAllowlist: Boolean(
- (options.blockedUrlPattern && options.blockedUrlPattern.length > 0) ||
- (options.allowedUrlPattern && options.allowedUrlPattern.length > 0),
- ),
+ allowList: options.allowedUrlPattern,
+ blocklist: options.blockedUrlPattern,
},
Locator,
);