Skip to content

The agent that grows with you

fix(desktop): update children use the OS system proxy when no proxy env is set

Tekniumbdb7ae2f7bc4
diff --git a/apps/desktop/electron/updater/checkout-source.ts b/apps/desktop/electron/updater/checkout-source.ts
index c93a53e4192..cc04236bfcf 100644
--- a/apps/desktop/electron/updater/checkout-source.ts
+++ b/apps/desktop/electron/updater/checkout-source.ts
@@ -7,6 +7,8 @@ import { buildDesktopBackendEnv } from '../backend-env'
import { resolveInstallationLauncher } from '../updater-process'
import { hiddenWindowsChildOptions } from '../windows-child-options'
+import { updateProxyEnv } from './system-proxy-env'
+
import type { UpdaterStatusWire } from './index'
export interface SourceUpdate extends UpdaterStatusWire {}
@@ -52,8 +54,10 @@ function probeFlags(probe: SourceUpdateProbe): string[] {
]
}
-export function sourceUpdateEnvironment(updateRoot: string, hermesHome: string): NodeJS.ProcessEnv {
+/** Every update child's env: the system proxy when none is set (#123387), then this process's env. */
+export async function sourceUpdateEnvironment(updateRoot: string, hermesHome: string): Promise<NodeJS.ProcessEnv> {
const env: NodeJS.ProcessEnv = {
+ ...(await updateProxyEnv()),
...process.env,
...buildDesktopBackendEnv(),
HERMES_HOME: hermesHome,
@@ -117,7 +121,7 @@ export async function readSourceUpdate(probe: SourceUpdateProbe): Promise<Source
: args,
hiddenWindowsChildOptions({
cwd: probe.updateRoot,
- env: sourceUpdateEnvironment(probe.updateRoot, probe.hermesHome),
+ env: await sourceUpdateEnvironment(probe.updateRoot, probe.hermesHome),
encoding: 'utf8',
timeout: 360000,
maxBuffer: 1024 * 1024,
diff --git a/apps/desktop/electron/updater/checkout.ts b/apps/desktop/electron/updater/checkout.ts
index 6fdba358167..b2d149c003e 100644
--- a/apps/desktop/electron/updater/checkout.ts
+++ b/apps/desktop/electron/updater/checkout.ts
@@ -367,7 +367,7 @@ export function createCheckoutStrategy(deps: CheckoutStrategyDeps): CheckoutStra
const child = spawnUpdaterProcess(updater, updaterArgs, {
cwd: deps.hermesHome,
env: {
- ...sourceUpdateEnvironment(updateRoot, deps.hermesHome)
+ ...(await sourceUpdateEnvironment(updateRoot, deps.hermesHome))
},
detached: true,
stdio: 'ignore'
@@ -753,7 +753,7 @@ export function createCheckoutStrategy(deps: CheckoutStrategyDeps): CheckoutStra
const child = spawnUpdaterProcess(handoff.command, args, {
cwd: deps.hermesHome,
env: {
- ...sourceUpdateEnvironment(updateRoot, deps.hermesHome),
+ ...(await sourceUpdateEnvironment(updateRoot, deps.hermesHome)),
HERMES_UPDATE_STARTED_AT: String(updateStartedAt)
},
detached: true,
diff --git a/apps/desktop/electron/updater/system-proxy-env.test.ts b/apps/desktop/electron/updater/system-proxy-env.test.ts
new file mode 100644
index 00000000000..8cb695c2384
--- /dev/null
+++ b/apps/desktop/electron/updater/system-proxy-env.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, it } from 'vitest'
+
+import { systemProxyEnv } from './system-proxy-env'
+
+const pac = (answers: Record<string, string>) => async (url: string) => answers[new URL(url).hostname] ?? 'DIRECT'
+
+describe('update child proxy env from the system proxy (#123387)', () => {
+ it('hands the system proxy to children that read only proxy env, bypassing hosts the system sends direct', async () => {
+ const env = await systemProxyEnv(
+ { PATH: '/usr/bin' },
+ pac({ 'github.com': 'PROXY 10.0.0.7:7890; DIRECT', 'pypi.org': 'PROXY 10.0.0.7:7890' }),
+ ['github.com', 'registry.npmjs.org', 'pypi.org']
+ )
+
+ expect(env).toEqual({
+ HTTPS_PROXY: 'http://10.0.0.7:7890',
+ https_proxy: 'http://10.0.0.7:7890',
+ HTTP_PROXY: 'http://10.0.0.7:7890',
+ http_proxy: 'http://10.0.0.7:7890',
+ NO_PROXY: 'localhost,127.0.0.1,::1,registry.npmjs.org',
+ no_proxy: 'localhost,127.0.0.1,::1,registry.npmjs.org'
+ })
+ })
+
+ it('leaves an explicit proxy env alone and adds nothing when the system goes direct', async () => {
+ const asked: string[] = []
+
+ const resolve = async (url: string) => {
+ asked.push(url)
+
+ return 'SOCKS5 10.0.0.9:1080'
+ }
+
+ expect(await systemProxyEnv({ https_proxy: 'http://corp:3128' }, resolve)).toEqual({})
+ expect(asked).toEqual([])
+ expect(await systemProxyEnv({}, async () => 'DIRECT')).toEqual({})
+ expect((await systemProxyEnv({}, resolve, ['github.com'])).HTTPS_PROXY).toBe('socks5h://10.0.0.9:1080')
+ })
+})
diff --git a/apps/desktop/electron/updater/system-proxy-env.ts b/apps/desktop/electron/updater/system-proxy-env.ts
new file mode 100644
index 00000000000..d49251dfe26
--- /dev/null
+++ b/apps/desktop/electron/updater/system-proxy-env.ts
@@ -0,0 +1,90 @@
+import { session } from 'electron'
+
+/**
+ * The OS proxy for the update's child processes (#123387).
+ *
+ * git, Python's urllib, npm, uv and electron-builder's downloads all honour
+ * HTTPS_PROXY/HTTP_PROXY/NO_PROXY, but none of them reads the macOS or Windows
+ * system proxy. A Desktop user whose only proxy is the system one got updates
+ * that went direct and died where direct access is blocked. Chromium already
+ * resolves the system proxy (PAC included), so ask it for the hosts the update
+ * reaches and hand the answer to the children as proxy env. Explicit proxy env
+ * always wins: it is returned untouched.
+ */
+
+const PROXY_ENV_KEYS = ['HTTPS_PROXY', 'https_proxy', 'HTTP_PROXY', 'http_proxy', 'ALL_PROXY', 'all_proxy']
+
+// git + the GitHub API and assets, the release pointer, npm, PyPI + uv, Electron downloads.
+export const UPDATE_HOSTS = [
+ 'github.com',
+ 'api.github.com',
+ 'codeload.github.com',
+ 'objects.githubusercontent.com',
+ 'release-assets.githubusercontent.com',
+ 'hermes-assets.nousresearch.com',
+ 'registry.npmjs.org',
+ 'pypi.org',
+ 'files.pythonhosted.org',
+ 'releases.astral.sh'
+]
+
+const LOCAL_BYPASS = ['localhost', '127.0.0.1', '::1']
+
+const PAC_SCHEMES: Record<string, string> = {
+ PROXY: 'http',
+ HTTPS: 'https',
+ SOCKS: 'socks5h',
+ SOCKS5: 'socks5h',
+ SOCKS4: 'socks4'
+}
+
+/** Chromium's PAC-style answer ("PROXY host:port; DIRECT") as a proxy URL; null for DIRECT. */
+export function proxyUrlFromPac(answer: string): string | null {
+ const [kind = '', hostPort = ''] = (answer.split(';')[0] ?? '').trim().split(/\s+/)
+ const scheme = PAC_SCHEMES[kind.toUpperCase()]
+
+ return scheme && hostPort ? `${scheme}://${hostPort}` : null
+}
+
+export async function systemProxyEnv(
+ env: NodeJS.ProcessEnv,
+ resolveProxy: (url: string) => Promise<string>,
+ hosts: string[] = UPDATE_HOSTS
+): Promise<NodeJS.ProcessEnv> {
+ if (PROXY_ENV_KEYS.some((key: string): boolean => Boolean(env[key]?.trim()))) {
+ return {}
+ }
+
+ const answers = await Promise.all(
+ hosts.map(async (host: string) => ({
+ host,
+ // A PAC script that fails to load leaves Chromium itself going direct.
+ proxy: proxyUrlFromPac(await resolveProxy(`https://${host}/`).catch((): string => 'DIRECT'))
+ }))
+ )
+
+ const proxy = answers.find(answer => answer.proxy)?.proxy
+
+ if (!proxy) {
+ return {}
+ }
+
+ const direct = answers.filter(answer => !answer.proxy).map(answer => answer.host)
+ const noProxy = env.NO_PROXY ?? env.no_proxy ?? [...LOCAL_BYPASS, ...direct].join(',')
+
+ return {
+ HTTPS_PROXY: proxy,
+ https_proxy: proxy,
+ HTTP_PROXY: proxy,
+ http_proxy: proxy,
+ NO_PROXY: noProxy,
+ no_proxy: noProxy
+ }
+}
+
+/** The system proxy env for an update child; empty outside Electron (unit tests, scripts). */
+export async function updateProxyEnv(env: NodeJS.ProcessEnv = process.env): Promise<NodeJS.ProcessEnv> {
+ const defaultSession = session?.defaultSession
+
+ return defaultSession ? systemProxyEnv(env, url => defaultSession.resolveProxy(url)) : {}
+}