fix(web-probe): enforce browser no-proxy launch (#641)

Co-authored-by: Codex <codex@noreply.local>
This commit is contained in:
Lyon
2026-06-22 09:06:10 +08:00
committed by GitHub
parent 3ee79d7701
commit 8434d62d07
2 changed files with 44 additions and 2 deletions
@@ -22,6 +22,7 @@ const screenshotIntervalMs = positiveInteger(process.env.UNIDESK_WEB_OBSERVE_SCR
const maxSamples = positiveInteger(process.env.UNIDESK_WEB_OBSERVE_MAX_SAMPLES, 0);
const viewport = parseViewport(process.env.UNIDESK_WEB_OBSERVE_VIEWPORT || "1440x900");
const playwrightProxy = proxyConfigFromEnv(baseUrl);
const chromiumLaunchOptions = chromiumLaunchOptionsForProxy(playwrightProxy);
const pageId = "page-" + randomBytes(4).toString("hex");
const dirs = {
commandsPending: path.join(stateDir, "commands", "pending"),
@@ -63,7 +64,7 @@ try {
await writeHeartbeat({ status: "starting" });
const launcher = await import(pathToFileURL(path.resolve("scripts/src/browser-launcher.mjs")).href);
const { chromium } = await launcher.importPlaywright();
browser = await launcher.launchChromium(chromium);
browser = await launcher.launchChromium(chromium, chromiumLaunchOptions);
context = await browser.newContext({ viewport, ...(playwrightProxy === null ? {} : { proxy: playwrightProxy }) });
auth = await runControlCommand({ id: "startup-login", type: "login", createdAt: startedAt, source: "startup" }, async () => authenticate(context));
page = await context.newPage();
@@ -301,6 +302,21 @@ function proxyConfigFromEnv(targetBaseUrl) {
return { server: raw };
}
function chromiumLaunchOptionsForProxy(proxy) {
const base = { env: browserProcessEnvWithoutProxy() };
if (proxy === null) return { ...base, args: ["--no-proxy-server"] };
return { ...base, proxy };
}
function browserProcessEnvWithoutProxy() {
const blocked = new Set(["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY", "http_proxy", "https_proxy", "all_proxy", "no_proxy"]);
const env = {};
for (const [key, value] of Object.entries(process.env)) {
if (!blocked.has(key) && value !== undefined) env[key] = value;
}
return env;
}
function noProxyMatches(hostname, rawList) {
const host = String(hostname || "").toLowerCase();
if (!host) return false;
@@ -324,6 +340,11 @@ function publicNetwork(proxy) {
server: publicProxyServer(proxy.server),
valuesRedacted: true,
},
browser: {
proxyMode: proxy === null ? "direct-no-proxy-server" : "explicit-playwright-proxy",
proxyEnvCleared: true,
valuesRedacted: true,
},
valuesRedacted: true,
};
}
@@ -18,6 +18,7 @@ const userScript = path.resolve(process.env.UNIDESK_WEB_PROBE_USER_SCRIPT || pat
const timeoutMs = positiveInteger(process.env.UNIDESK_WEB_PROBE_TIMEOUT_MS, 30000);
const viewport = parseViewport(process.env.UNIDESK_WEB_PROBE_VIEWPORT || "1440x900");
const playwrightProxy = proxyConfigFromEnv(baseUrl);
const chromiumLaunchOptions = chromiumLaunchOptionsForProxy(playwrightProxy);
const artifactRecords = [];
const readinessRecords = [];
const stepRecords = [];
@@ -34,7 +35,7 @@ try {
userScriptSha256 = await sha256File(userScript).catch(() => null);
const launcher = await import(pathToFileURL(path.resolve("scripts/src/browser-launcher.mjs")).href);
const { chromium } = await launcher.importPlaywright();
browser = await launcher.launchChromium(chromium);
browser = await launcher.launchChromium(chromium, chromiumLaunchOptions);
context = await browser.newContext({ viewport, ...(playwrightProxy === null ? {} : { proxy: playwrightProxy }) });
auth = await authenticate(context);
if (!auth.ok) throw new Error("auth-login-failed");
@@ -384,6 +385,21 @@ function proxyConfigFromEnv(targetBaseUrl) {
return { server: raw };
}
function chromiumLaunchOptionsForProxy(proxy) {
const base = { env: browserProcessEnvWithoutProxy() };
if (proxy === null) return { ...base, args: ["--no-proxy-server"] };
return { ...base, proxy };
}
function browserProcessEnvWithoutProxy() {
const blocked = new Set(["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY", "http_proxy", "https_proxy", "all_proxy", "no_proxy"]);
const env = {};
for (const [key, value] of Object.entries(process.env)) {
if (!blocked.has(key) && value !== undefined) env[key] = value;
}
return env;
}
function noProxyMatches(hostname, rawList) {
const host = String(hostname || "").toLowerCase();
if (!host) return false;
@@ -407,6 +423,11 @@ function publicNetwork(proxy) {
server: publicProxyServer(proxy.server),
valuesRedacted: true,
},
browser: {
proxyMode: proxy === null ? "direct-no-proxy-server" : "explicit-playwright-proxy",
proxyEnvCleared: true,
valuesRedacted: true,
},
valuesRedacted: true,
};
}