Skip to content

Commit 067276f

Browse files
committed
fix(browser): preserve bridge diagnostic edge cases
1 parent baf7c49 commit 067276f

4 files changed

Lines changed: 77 additions & 15 deletions

File tree

‎extensions/browser/src/browser/cdp-proxy-bypass.test.ts‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ describe("withNoProxyForLocalhost preserves user-configured NO_PROXY", () => {
321321

322322
await withNoProxyForLocalhost(async () => {
323323
expect(process.env.NO_PROXY).toBe(`${coveredNoProxy},localhost,127.0.0.1,[::1]`);
324-
expect(process.env.no_proxy).toBe(`${coveredNoProxy},localhost,127.0.0.1,[::1]`);
324+
expect(process.env.no_proxy).toBe(`${staleLowerNoProxy},localhost,127.0.0.1,[::1]`);
325325
});
326326

327327
expect(process.env.NO_PROXY).toBe(coveredNoProxy);
@@ -333,6 +333,53 @@ describe("withNoProxyForLocalhost preserves user-configured NO_PROXY", () => {
333333
}
334334
});
335335

336+
it("mirrors lowercase-only bypass entries into uppercase during the lease", async () => {
337+
const lowerNoProxy = "corp.internal";
338+
delete process.env.NO_PROXY;
339+
process.env.no_proxy = lowerNoProxy;
340+
process.env.HTTP_PROXY = "http://proxy:8080";
341+
342+
try {
343+
const { withNoProxyForLocalhost } = await import("./cdp-proxy-bypass.js");
344+
345+
await withNoProxyForLocalhost(async () => {
346+
expect(process.env.NO_PROXY).toBe(`${lowerNoProxy},localhost,127.0.0.1,[::1]`);
347+
expect(process.env.no_proxy).toBe(`${lowerNoProxy},localhost,127.0.0.1,[::1]`);
348+
});
349+
350+
expect(process.env.NO_PROXY).toBeUndefined();
351+
expect(process.env.no_proxy).toBe(lowerNoProxy);
352+
} finally {
353+
delete process.env.HTTP_PROXY;
354+
delete process.env.NO_PROXY;
355+
delete process.env.no_proxy;
356+
}
357+
});
358+
359+
it("restores untouched NO_PROXY casing when the lowercase value changes", async () => {
360+
const userNoProxy = "internal.corp";
361+
process.env.NO_PROXY = userNoProxy;
362+
process.env.no_proxy = userNoProxy;
363+
process.env.HTTP_PROXY = "http://proxy:8080";
364+
365+
try {
366+
const { withNoProxyForLocalhost } = await import("./cdp-proxy-bypass.js");
367+
368+
await withNoProxyForLocalhost(async () => {
369+
expect(process.env.NO_PROXY).toBe(`${userNoProxy},localhost,127.0.0.1,[::1]`);
370+
expect(process.env.no_proxy).toBe(`${userNoProxy},localhost,127.0.0.1,[::1]`);
371+
delete process.env.no_proxy;
372+
});
373+
374+
expect(process.env.NO_PROXY).toBe(userNoProxy);
375+
expect(process.env.no_proxy).toBeUndefined();
376+
} finally {
377+
delete process.env.HTTP_PROXY;
378+
delete process.env.NO_PROXY;
379+
delete process.env.no_proxy;
380+
}
381+
});
382+
336383
it("does not treat substring matches as complete loopback coverage", async () => {
337384
const userNoProxy = "notlocalhost,127.0.0.10,[::1].example";
338385
process.env.NO_PROXY = userNoProxy;
@@ -392,7 +439,7 @@ describe("withNoProxyForCdpUrl", () => {
392439
}
393440
});
394441

395-
it("does not restore stale no_proxy when it was deleted during execution", async () => {
442+
it("restores untouched NO_PROXY when no_proxy was deleted during execution", async () => {
396443
process.env.HTTP_PROXY = "http://proxy:8080";
397444
process.env.NO_PROXY = "corp.internal";
398445
process.env.no_proxy = "corp.internal";
@@ -402,7 +449,7 @@ describe("withNoProxyForCdpUrl", () => {
402449
expect(process.env.no_proxy).toBe("corp.internal,localhost,127.0.0.1,[::1]");
403450
delete process.env.no_proxy;
404451
});
405-
expect(process.env.NO_PROXY).toBe("corp.internal,localhost,127.0.0.1,[::1]");
452+
expect(process.env.NO_PROXY).toBe("corp.internal");
406453
expect(process.env.no_proxy).toBeUndefined();
407454
} finally {
408455
delete process.env.HTTP_PROXY;

‎extensions/browser/src/browser/cdp-proxy-bypass.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ function noProxyAlreadyCoversLocalhost(): boolean {
6262
);
6363
}
6464

65+
function appendLoopbackEntries(value: string | undefined): string {
66+
return value ? `${value},${LOOPBACK_ENTRIES}` : LOOPBACK_ENTRIES;
67+
}
68+
6569
export async function withNoProxyForLocalhost<T>(fn: () => Promise<T>): Promise<T> {
6670
return await withNoProxyForCdpUrl("http://127.0.0.1", fn);
6771
}
@@ -77,7 +81,8 @@ function isLoopbackCdpUrl(url: string): boolean {
7781
type NoProxySnapshot = {
7882
noProxy: string | undefined;
7983
noProxyLower: string | undefined;
80-
applied: string;
84+
appliedNoProxy: string;
85+
appliedNoProxyLower: string;
8186
};
8287

8388
class NoProxyLeaseManager {
@@ -92,11 +97,11 @@ class NoProxyLeaseManager {
9297
if (this.leaseCount === 0 && !noProxyAlreadyCoversLocalhost()) {
9398
const noProxy = process.env.NO_PROXY;
9499
const noProxyLower = process.env.no_proxy;
95-
const current = noProxy || noProxyLower || "";
96-
const applied = current ? `${current},${LOOPBACK_ENTRIES}` : LOOPBACK_ENTRIES;
97-
process.env.NO_PROXY = applied;
98-
process.env.no_proxy = applied;
99-
this.snapshot = { noProxy, noProxyLower, applied };
100+
const appliedNoProxy = appendLoopbackEntries(noProxy || noProxyLower);
101+
const appliedNoProxyLower = appendLoopbackEntries(noProxyLower || noProxy);
102+
process.env.NO_PROXY = appliedNoProxy;
103+
process.env.no_proxy = appliedNoProxyLower;
104+
this.snapshot = { noProxy, noProxyLower, appliedNoProxy, appliedNoProxyLower };
100105
}
101106

102107
this.leaseCount += 1;
@@ -119,16 +124,17 @@ class NoProxyLeaseManager {
119124
return;
120125
}
121126

122-
const { noProxy, noProxyLower, applied } = this.snapshot;
127+
const { noProxy, noProxyLower, appliedNoProxy, appliedNoProxyLower } = this.snapshot;
123128
const currentNoProxy = process.env.NO_PROXY;
124129
const currentNoProxyLower = process.env.no_proxy;
125-
const untouched = currentNoProxy === applied && currentNoProxyLower === applied;
126-
if (untouched) {
130+
if (currentNoProxy === appliedNoProxy) {
127131
if (noProxy !== undefined) {
128132
process.env.NO_PROXY = noProxy;
129133
} else {
130134
delete process.env.NO_PROXY;
131135
}
136+
}
137+
if (currentNoProxyLower === appliedNoProxyLower) {
132138
if (noProxyLower !== undefined) {
133139
process.env.no_proxy = noProxyLower;
134140
} else {

‎extensions/browser/src/browser/chrome-mcp.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ describe("chrome MCP page parsing", () => {
299299
"Chrome",
300300
"Profile 1",
301301
);
302+
const attachFailureDetail = `attach failed for ${userDataDir}`;
302303
const fakeMcpCommand = path.join(tempDir, "fake-mcp.mjs");
303304
await fs.writeFile(
304305
fakeMcpCommand,
@@ -311,7 +312,7 @@ describe("chrome MCP page parsing", () => {
311312
const body = JSON.stringify({
312313
jsonrpc: "2.0",
313314
id: Number(match[1]),
314-
error: { code: -32000, message: "attach failed" },
315+
error: { code: -32000, message: ${JSON.stringify(attachFailureDetail)} },
315316
});
316317
process.stdout.write(body + "\\n");
317318
});
@@ -337,6 +338,7 @@ describe("chrome MCP page parsing", () => {
337338

338339
expect(message).toContain("Chrome MCP existing-session attach failed");
339340
expect(message).toContain("~/Library/Application Support/Google/Chrome/Profile 1");
341+
expect(message).toContain("attach failed for ~/Library/Application Support/Google/Chrome/Profile 1");
340342
expect(message).not.toContain(homeDir);
341343
expect(message).not.toContain(userDataDir);
342344
});

‎extensions/browser/src/browser/chrome-mcp.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,13 @@ function redactChromeMcpDiagnosticText(text: string): string {
389389
);
390390
}
391391

392+
function redactChromeMcpDiagnosticTextWithLocalPaths(text: string): string {
393+
const homeDir = normalizeOptionalString(os.homedir());
394+
const homePath = homeDir ? path.resolve(homeDir) : undefined;
395+
const withHomeRedacted = homePath ? text.split(homePath).join("~") : text;
396+
return redactChromeMcpDiagnosticText(withHomeRedacted);
397+
}
398+
392399
function redactChromeMcpLocalPathForDiagnostic(filePath: string): string {
393400
const homeDir = normalizeOptionalString(os.homedir());
394401
if (!homeDir || !path.isAbsolute(filePath)) {
@@ -466,15 +473,15 @@ async function createRealSession(
466473
const stderr = getStderr();
467474
if (stderr) {
468475
log.warn(
469-
`Chrome MCP attach failed for profile "${redactChromeMcpProfileLabelForDiagnostic(profileName)}". Subprocess stderr:\n${redactChromeMcpDiagnosticText(stderr)}`,
476+
`Chrome MCP attach failed for profile "${redactChromeMcpProfileLabelForDiagnostic(profileName)}". Subprocess stderr:\n${redactChromeMcpDiagnosticTextWithLocalPaths(stderr)}`,
470477
);
471478
}
472479
const targetLabel = options.browserUrl
473480
? `the configured Chrome endpoint (${redactToolPayloadText(redactCdpUrl(options.browserUrl) ?? options.browserUrl)})`
474481
: options.userDataDir
475482
? `the configured Chromium user data dir (${redactChromeMcpLocalPathForDiagnostic(options.userDataDir)})`
476483
: "Google Chrome's default profile";
477-
const detail = redactChromeMcpDiagnosticText(
484+
const detail = redactChromeMcpDiagnosticTextWithLocalPaths(
478485
err instanceof Error ? err.message : String(err),
479486
);
480487
throw new BrowserProfileUnavailableError(

0 commit comments

Comments
 (0)