Skip to content

Commit e67c79e

Browse files
author
Natallia Harshunova
committed
prevent netwrok emulation if there is allowlist or blocklist
1 parent 998d4be commit e67c79e

3 files changed

Lines changed: 31 additions & 18 deletions

File tree

src/McpContext.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ interface McpContextOptions {
5555
experimentalIncludeAllPages?: boolean;
5656
// Whether CrUX data should be fetched.
5757
performanceCrux: boolean;
58+
// Whether allowlist/blocklist is configured.
59+
hasNetworkBlockOrAllowlist?: boolean;
5860
}
5961

6062
const DEFAULT_TIMEOUT = 5_000;
@@ -308,24 +310,27 @@ export class McpContext implements Context {
308310
const mcpPage = this.#getMcpPage(page);
309311
const newSettings: EmulationSettings = {...mcpPage.emulationSettings};
310312

311-
if (!options.networkConditions) {
312-
await page.emulateNetworkConditions(null);
313-
delete newSettings.networkConditions;
314-
} else if (options.networkConditions === 'Offline') {
315-
await page.emulateNetworkConditions({
316-
offline: true,
317-
download: 0,
318-
upload: 0,
319-
latency: 0,
320-
});
321-
newSettings.networkConditions = 'Offline';
322-
} else if (options.networkConditions in PredefinedNetworkConditions) {
323-
const networkCondition =
324-
PredefinedNetworkConditions[
325-
options.networkConditions as keyof typeof PredefinedNetworkConditions
326-
];
327-
await page.emulateNetworkConditions(networkCondition);
328-
newSettings.networkConditions = options.networkConditions;
313+
// Skip network emulation if blocklist/allowlist is configured, as it is rejected by Puppeteer.
314+
if (!this.#options.hasNetworkBlockOrAllowlist) {
315+
if (!options.networkConditions) {
316+
await page.emulateNetworkConditions(null);
317+
delete newSettings.networkConditions;
318+
} else if (options.networkConditions === 'Offline') {
319+
await page.emulateNetworkConditions({
320+
offline: true,
321+
download: 0,
322+
upload: 0,
323+
latency: 0,
324+
});
325+
newSettings.networkConditions = 'Offline';
326+
} else if (options.networkConditions in PredefinedNetworkConditions) {
327+
const networkCondition =
328+
PredefinedNetworkConditions[
329+
options.networkConditions as keyof typeof PredefinedNetworkConditions
330+
];
331+
await page.emulateNetworkConditions(networkCondition);
332+
newSettings.networkConditions = options.networkConditions;
333+
}
329334
}
330335

331336
if (!options.cpuThrottlingRate) {

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ export async function createMcpServer(
144144
experimentalDevToolsDebugging: devtools,
145145
experimentalIncludeAllPages: serverArgs.experimentalIncludeAllPages,
146146
performanceCrux: serverArgs.performanceCrux,
147+
hasNetworkBlockOrAllowlist: Boolean(
148+
(blocklist && blocklist.length > 0) ||
149+
(allowlist && allowlist.length > 0),
150+
),
147151
});
148152
await updateRoots();
149153
}

tests/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ export async function withMcpContext(
136136
{
137137
experimentalDevToolsDebugging: false,
138138
performanceCrux: options.performanceCrux ?? true,
139+
hasNetworkBlockOrAllowlist: Boolean(
140+
(options.blockedUrlPattern && options.blockedUrlPattern.length > 0) ||
141+
(options.allowedUrlPattern && options.allowedUrlPattern.length > 0),
142+
),
139143
},
140144
Locator,
141145
);

0 commit comments

Comments
 (0)