Files
OmniRoute/tests/unit/browser-executable-boundary.test.ts
Ankit 69afe4acf7 fix(providers): find Chrome in its Windows install locations for ChatGPT Web Codex (#18850) (#19170)
## Summary



I reproduced this on my own Windows 11 machine, with Chrome installed in the default place and no `CHROME_PATH` set:

- before: `detectChromeExecutable()` returns `undefined`
- after: `C:\Program Files\Google\Chrome\Application\chrome.exe`

This covers only the first of the three bugs in #18850. The other two are left out on purpose:

- "Check cookie" minting a fresh validation id each time touches the session-identity check in `persistent-cdp-context.ts` and needs a live CDP session to prove. Not something I can test from here.

## Related Issues

- Refs #18850 (bug 1 of 3; the issue stays open for the other two)

## Target Branch

Thanks @anki1kr.
2026-11-08 01:08:20 -03:00

117 lines
4.8 KiB
TypeScript

// The ChatGPT Web browser providers launch a local Chrome/Chromium. Two inputs reached that
// launch from a remote management request:
// - validation of `chatgpt-web-codex` ignored `allowLocalSpawn`, which remote-reachable
// routes (POST /api/providers/bulk, the connection test) set to false for non-loopback
// callers so a remote request never makes the host start a process;
// - `providerSpecificData.chromeExecutablePath` — caller input, stored with the connection
// and read again by the credential-health scheduler and by inference — was used as the
// executable whenever the path existed on the server.
// The executable is now chosen only by the operator: the server's own environment
// (CHROME_PATH, CHATGPT_WEB_*_CHROME_PATH) or the known Chrome/Chromium install locations.
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-browser-exec-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const ENV_KEYS = [
"CHROME_PATH",
"CHATGPT_WEB_CODEX_CHROME_PATH",
"CHATGPT_WEB_CHROME_PATH",
"CHATGPT_WEB_CODEX_CDP_URL",
"PROGRAMFILES",
"PROGRAMFILES(X86)",
"LOCALAPPDATA",
];
const savedEnv = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
for (const k of ENV_KEYS) delete process.env[k];
const { detectChromeExecutable } = await import("../../open-sse/executors/chatgpt-web-codex.ts");
const { resolveChatGptWebChromeExecutable } =
await import("../../open-sse/utils/chatgptWebExecutorAdapter.ts");
const { validateChatGptWebCodexProvider } =
await import("../../src/lib/providers/validation/chatgptWebCodex.ts");
// Any program that exists on the server, standing in for a caller-chosen executable.
const NOT_A_BROWSER = path.join(TEST_DATA_DIR, "not-a-browser");
fs.writeFileSync(NOT_A_BROWSER, "#!/bin/sh\nexit 0\n", { mode: 0o755 });
test.after(() => {
for (const k of ENV_KEYS) {
if (savedEnv[k] === undefined) delete process.env[k];
else process.env[k] = savedEnv[k];
}
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("codex: a stored chromeExecutablePath outside the operator's browsers is never used", () => {
assert.notEqual(detectChromeExecutable(NOT_A_BROWSER), NOT_A_BROWSER);
});
test("codex: the operator's own CHATGPT_WEB_CODEX_CHROME_PATH / CHROME_PATH still selects the browser", () => {
process.env.CHATGPT_WEB_CODEX_CHROME_PATH = NOT_A_BROWSER;
try {
assert.equal(detectChromeExecutable(), NOT_A_BROWSER);
// Naming the operator's configured browser explicitly is fine too.
assert.equal(detectChromeExecutable(NOT_A_BROWSER), NOT_A_BROWSER);
} finally {
delete process.env.CHATGPT_WEB_CODEX_CHROME_PATH;
}
process.env.CHROME_PATH = NOT_A_BROWSER;
try {
assert.equal(detectChromeExecutable(), NOT_A_BROWSER);
} finally {
delete process.env.CHROME_PATH;
}
});
// #18850: on Windows the codex detector knew no install location, so Chrome in its default
// place was never found and the connection could not be added without CHROME_PATH.
test("codex: Chrome in the Windows install locations is an operator browser", () => {
for (const envKey of ["PROGRAMFILES", "PROGRAMFILES(X86)", "LOCALAPPDATA"]) {
const root = fs.mkdtempSync(path.join(TEST_DATA_DIR, "win-"));
const chromeExe = path.join(root, "Google", "Chrome", "Application", "chrome.exe");
fs.mkdirSync(path.dirname(chromeExe), { recursive: true });
fs.writeFileSync(chromeExe, "");
process.env[envKey] = root;
try {
assert.equal(detectChromeExecutable(chromeExe), chromeExe, envKey);
} finally {
delete process.env[envKey];
}
}
});
test("chatgpt-web: a stored chromeExecutablePath outside the operator's browsers is never used", () => {
const exists = (p: string) => p === NOT_A_BROWSER;
assert.equal(resolveChatGptWebChromeExecutable(NOT_A_BROWSER, { env: {}, exists }), undefined);
// The operator's environment still decides.
assert.equal(
resolveChatGptWebChromeExecutable(undefined, {
env: { CHATGPT_WEB_CHROME_PATH: NOT_A_BROWSER },
exists,
}),
NOT_A_BROWSER
);
assert.equal(
resolveChatGptWebChromeExecutable(NOT_A_BROWSER, {
env: { CHROME_PATH: NOT_A_BROWSER },
exists,
}),
NOT_A_BROWSER
);
});
test("codex validation honours allowLocalSpawn=false: a remote caller never reaches the browser launch", async () => {
const result = await validateChatGptWebCodexProvider({
apiKey: "__Secure-next-auth.session-token=test-cookie",
providerSpecificData: { chromeExecutablePath: NOT_A_BROWSER },
allowLocalSpawn: false,
});
assert.equal(result.valid, false);
assert.match(String(result.error), /local/i, "must say the browser check needs a local request");
});