Skip to content

The agent that grows with you

fix(desktop-update): ask the system proxy about the checkout's origin and the user's mirrors; honour NO_PROXY=*

Tekniumadbfa8244ffe
diff --git a/apps/desktop/electron/updater/checkout-source.ts b/apps/desktop/electron/updater/checkout-source.ts
index e030e3875fa..0d737f74a98 100644
--- a/apps/desktop/electron/updater/checkout-source.ts
+++ b/apps/desktop/electron/updater/checkout-source.ts
@@ -67,7 +67,7 @@ export async function sourceUpdateEnvironment(
): Promise<NodeJS.ProcessEnv> {
const env: NodeJS.ProcessEnv = {
...process.env,
- ...(await updateProxyEnv(process.env, note)),
+ ...(await updateProxyEnv(process.env, note, updateRoot)),
...buildDesktopBackendEnv(),
HERMES_HOME: hermesHome,
HERMES_INSTALL_ROOT: updateRoot
diff --git a/apps/desktop/electron/updater/system-proxy-env.test.ts b/apps/desktop/electron/updater/system-proxy-env.test.ts
index c82a8bbf2a6..fa006490103 100644
--- a/apps/desktop/electron/updater/system-proxy-env.test.ts
+++ b/apps/desktop/electron/updater/system-proxy-env.test.ts
@@ -1,6 +1,19 @@
-import { describe, expect, it } from 'vitest'
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
-import { LOCAL_BYPASS, restoreUserProxyEnv, systemProxyEnv } from './system-proxy-env'
+import { describe, expect, it, vi } from 'vitest'
+
+import { LOCAL_BYPASS, restoreUserProxyEnv, systemProxyEnv, updateProxyEnv } from './system-proxy-env'
+
+// Chromium's answer for the system proxy: only github.com goes through it, every other host direct.
+vi.mock('electron', () => ({
+ session: {
+ defaultSession: {
+ resolveProxy: async (url: string) => (new URL(url).hostname === 'github.com' ? 'PROXY 10.0.0.7:7890' : 'DIRECT')
+ }
+ }
+}))
const pac = (answers: Record<string, string>) => async (url: string) => answers[new URL(url).hostname] ?? 'DIRECT'
const proxied = (value: string) => ({ HTTPS_PROXY: value, https_proxy: value, HTTP_PROXY: value, http_proxy: value })
@@ -101,4 +114,32 @@ describe('update child proxy env from the system proxy (#123387)', () => {
expect(new Set(Object.entries(restored).filter(([key]) => key.toLowerCase() === 'no_proxy').map(([, v]) => v)))
.toEqual(new Set(['corp.internal']))
})
+
+ it("keeps a fork origin and the user's package mirrors direct when the system sends them direct (S2)", async () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-update-hosts-'))
+
+ try {
+ fs.mkdirSync(path.join(root, '.git'))
+ fs.writeFileSync(
+ path.join(root, '.git', 'config'),
+ '[core]\n\tbare = false\n[remote "origin"]\n\turl = https://git.corp.example/me/hermes-agent.git\n'
+ )
+ fs.writeFileSync(path.join(root, 'npmrc'), 'registry=https://npm.corp.example/\n')
+ const user = { UV_INDEX_URL: 'https://pypi.corp.example/simple', npm_config_userconfig: path.join(root, 'npmrc') }
+
+ const env = await updateProxyEnv(user, undefined, root)
+
+ for (const mirror of ['git.corp.example', 'pypi.corp.example', 'npm.corp.example']) {
+ expect(env.NO_PROXY?.split(',')).toContain(mirror)
+ }
+ } finally {
+ fs.rmSync(root, { recursive: true, force: true })
+ }
+ })
+
+ it('injects nothing over a user NO_PROXY of *, which curl and urllib read as bypass-all only alone (S4)', async () => {
+ const github = pac({ 'github.com': 'PROXY 10.0.0.7:7890' })
+
+ expect(await systemProxyEnv({ no_proxy: '*' }, github, ['github.com'])).toEqual({})
+ })
})
diff --git a/apps/desktop/electron/updater/system-proxy-env.ts b/apps/desktop/electron/updater/system-proxy-env.ts
index d713eb0741f..81d2958e5df 100644
--- a/apps/desktop/electron/updater/system-proxy-env.ts
+++ b/apps/desktop/electron/updater/system-proxy-env.ts
@@ -1,5 +1,7 @@
import { session } from 'electron'
+import { configuredUpdateHosts } from './update-hosts'
+
/**
* The OS proxy for the update's child processes (#123387).
*
@@ -152,6 +154,12 @@ export async function systemProxyEnv(
return {}
}
+ // A user NO_PROXY of `*` bypasses every proxy, but `*,localhost,…` bypasses none in curl or
+ // urllib: inject nothing, as agent/proxy_bypass.py honours it (review S4).
+ if (valuesAnyCase(userEnv, 'no_proxy').some(value => value.split(/[\s,]+/).includes('*'))) {
+ return {}
+ }
+
// Each host is asked on its own: a PAC can send each one a different way.
const answers = await Promise.all(
hosts.map(async (host: string) => {
@@ -190,12 +198,18 @@ export async function systemProxyEnv(
}
}
-/** The system proxy env for an update child; empty outside Electron (unit tests, scripts). */
+/**
+ * The system proxy env for an update child; empty outside Electron (unit tests, scripts).
+ * Besides the public UPDATE_HOSTS, the checkout's origin and the user's configured package
+ * indexes are asked too, so one the system sends direct stays direct.
+ */
export async function updateProxyEnv(
env: NodeJS.ProcessEnv = process.env,
- note?: (message: string) => void
+ note?: (message: string) => void,
+ updateRoot?: string
): Promise<NodeJS.ProcessEnv> {
const defaultSession = session?.defaultSession
+ const hosts = [...new Set([...UPDATE_HOSTS, ...(updateRoot ? configuredUpdateHosts(updateRoot, env) : [])])]
- return defaultSession ? systemProxyEnv(env, url => defaultSession.resolveProxy(url), UPDATE_HOSTS, note) : {}
+ return defaultSession ? systemProxyEnv(env, url => defaultSession.resolveProxy(url), hosts, note) : {}
}
diff --git a/apps/desktop/electron/updater/update-hosts.ts b/apps/desktop/electron/updater/update-hosts.ts
new file mode 100644
index 00000000000..3e1c11b1071
--- /dev/null
+++ b/apps/desktop/electron/updater/update-hosts.ts
@@ -0,0 +1,107 @@
+import { readFileSync } from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+
+/**
+ * Hosts this install's update reaches beyond the public ones in UPDATE_HOSTS: the
+ * checkout's origin (a fork, an internal git mirror) and every package index or npm
+ * registry the user configured (#123387 review S2). The system proxy is asked about each
+ * one, so a mirror the system sends direct joins NO_PROXY instead of being proxied.
+ *
+ * Only http(s) URLs count: git over ssh and file:// remotes never read proxy env.
+ */
+
+// uv and pip index settings (any letter case; UV_INDEX_<NAME>_URL too) and npm's registry.
+const INDEX_KEYS = [
+ 'uv_index_url',
+ 'uv_extra_index_url',
+ 'uv_default_index',
+ 'uv_index',
+ 'pip_index_url',
+ 'pip_extra_index_url',
+ 'npm_config_registry'
+]
+
+function httpHost(value: string): string | null {
+ // UV_INDEX entries may be `name=url`.
+ const url = value.includes('=') && !/^https?:/i.test(value) ? value.slice(value.indexOf('=') + 1) : value
+
+ try {
+ const parsed = new URL(url.trim())
+
+ return /^https?:$/.test(parsed.protocol) && parsed.hostname ? parsed.hostname.toLowerCase() : null
+ } catch {
+ return null
+ }
+}
+
+function readText(file: string): string {
+ try {
+ return readFileSync(file, 'utf8')
+ } catch {
+ return ''
+ }
+}
+
+/** `key = value` lines of one INI-style file, `[section]` names lower-cased; '' before any section. */
+function iniValues(text: string, section: string, keys: string[]): string[] {
+ let current = ''
+ const values: string[] = []
+
+ for (const raw of text.split(/\r?\n/)) {
+ const line = raw.trim()
+ const header = /^\[(.+)\]$/.exec(line)
+
+ if (header) {
+ current = header[1].trim().toLowerCase()
+ } else if (current === section) {
+ const [key, ...rest] = line.split('=')
+
+ if (rest.length && keys.includes(key.trim().toLowerCase())) {
+ values.push(rest.join('=').trim().replace(/^["']|["']$/g, ''))
+ }
+ }
+ }
+
+ return values
+}
+
+function pipConfigFiles(env: NodeJS.ProcessEnv, home: string): string[] {
+ const explicit = env.PIP_CONFIG_FILE
+
+ if (process.platform === 'win32') {
+ return [path.join(home, 'pip', 'pip.ini'), path.join(env.APPDATA || path.join(home, 'AppData', 'Roaming'), 'pip', 'pip.ini'), ...(explicit ? [explicit] : [])]
+ }
+
+ return [
+ path.join(home, '.pip', 'pip.conf'),
+ path.join(home, 'Library', 'Application Support', 'pip', 'pip.conf'),
+ path.join(env.XDG_CONFIG_HOME || path.join(home, '.config'), 'pip', 'pip.conf'),
+ ...(explicit ? [explicit] : [])
+ ]
+}
+
+export function configuredUpdateHosts(
+ updateRoot: string,
+ env: NodeJS.ProcessEnv,
+ home: string = os.homedir()
+): string[] {
+ const values: string[] = iniValues(readText(path.join(updateRoot, '.git', 'config')), 'remote "origin"', ['url'])
+
+ for (const [key, value] of Object.entries(env)) {
+ const name = key.toLowerCase()
+
+ if (value && (INDEX_KEYS.includes(name) || /^uv_index_.+_url$/.test(name))) {
+ values.push(...value.split(/\s+/))
+ }
+ }
+
+ const npmrc = env.npm_config_userconfig || env.NPM_CONFIG_USERCONFIG || path.join(home, '.npmrc')
+ values.push(...iniValues(readText(npmrc), '', ['registry']))
+
+ for (const file of pipConfigFiles(env, home)) {
+ values.push(...iniValues(readText(file), 'global', ['index-url', 'extra-index-url']).flatMap(v => v.split(/\s+/)))
+ }
+
+ return [...new Set(values.map(httpHost).filter((host): host is string => Boolean(host)))]
+}