Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(network): say why fetch failed, honour scheme-less system proxies, trust the OS cert store on desktop (#1381)

Tashfeendbf3a84c885b
diff --git a/desktop/src/__tests__/system-certificates.test.ts b/desktop/src/__tests__/system-certificates.test.ts
new file mode 100644
index 00000000..c1290108
--- /dev/null
+++ b/desktop/src/__tests__/system-certificates.test.ts
@@ -0,0 +1,33 @@
+import { describe, it, expect, vi } from 'vitest';
+
+vi.mock('../../../server/src/env.js', () => ({}));
+
+const { trustSystemCertificates } = await import('../server-host.js');
+
+// #1373: antivirus HTTPS scanning and corporate TLS inspection re-sign traffic
+// with a root installed in the OS store. The desktop app must trust that store
+// on top of Node's bundled roots, or every provider call is a bare "fetch failed".
+describe('trustSystemCertificates', () => {
+ it('adds the OS store to the default roots without duplicates', () => {
+ const setDefaultCACertificates = vi.fn();
+ const getCACertificates = vi.fn((type?: string) => (type === 'system' ? ['AV-ROOT', 'SHARED'] : ['BUNDLED', 'SHARED']));
+ expect(trustSystemCertificates({ getCACertificates, setDefaultCACertificates } as any)).toBe(2);
+ expect(setDefaultCACertificates).toHaveBeenCalledWith(['BUNDLED', 'SHARED', 'AV-ROOT']);
+ });
+
+ it('leaves the defaults alone when the OS store is empty', () => {
+ const setDefaultCACertificates = vi.fn();
+ const getCACertificates = vi.fn((type?: string) => (type === 'system' ? [] : ['BUNDLED']));
+ expect(trustSystemCertificates({ getCACertificates, setDefaultCACertificates } as any)).toBe(0);
+ expect(setDefaultCACertificates).not.toHaveBeenCalled();
+ });
+
+ it('is a no-op on a runtime without the APIs', () => {
+ expect(trustSystemCertificates({} as any)).toBe(0);
+ });
+
+ it('never throws when the OS store cannot be read', () => {
+ const getCACertificates = vi.fn(() => { throw new Error('keychain locked'); });
+ expect(trustSystemCertificates({ getCACertificates, setDefaultCACertificates: vi.fn() } as any)).toBe(0);
+ });
+});
diff --git a/desktop/src/server-host.ts b/desktop/src/server-host.ts
index abd99460..0bfccce7 100644
--- a/desktop/src/server-host.ts
+++ b/desktop/src/server-host.ts
@@ -8,6 +8,7 @@
// relative imports in sync with the repo-root default in main.ts.
import '../../server/src/env.js';
import crypto from 'node:crypto';
+import tls from 'node:tls';
import type { Server } from 'node:http';
import { createApp } from '../../server/src/app.js';
import { initDb, getDb, getUnifiedApiKey } from '../../server/src/db/index.js';
@@ -40,6 +41,31 @@ export interface ServerHandle {
port: number;
}
+/**
+ * Trust the OS certificate store for outbound TLS, on top of Node's bundled
+ * roots (#1373). Antivirus that scans HTTPS (Kaspersky, ESET, Avast, …) and
+ * corporate TLS inspection re-sign traffic with a root they install into the
+ * Windows/macOS store. Browsers trust it; Node's fetch only knows its bundled
+ * list, so every provider call failed with a bare "fetch failed"
+ * (SELF_SIGNED_CERT_IN_CHAIN). Desktop only: the server image keeps Node's
+ * default and can opt in with NODE_USE_SYSTEM_CA=1. Best effort — a runtime
+ * without these APIs (Node < 22.19) keeps the default behaviour.
+ */
+export function trustSystemCertificates(tlsModule: Pick<typeof tls, 'getCACertificates' | 'setDefaultCACertificates'> = tls): number {
+ try {
+ if (typeof tlsModule.getCACertificates !== 'function' || typeof tlsModule.setDefaultCACertificates !== 'function') return 0;
+ const system = tlsModule.getCACertificates('system');
+ if (system.length === 0) return 0;
+ const merged = [...new Set([...tlsModule.getCACertificates('default'), ...system])];
+ tlsModule.setDefaultCACertificates(merged);
+ console.log(`[tls] trusting ${system.length} certificate(s) from the OS store`);
+ return system.length;
+ } catch (err) {
+ console.warn(`[tls] could not load the OS certificate store: ${(err as Error)?.message ?? err}`);
+ return 0;
+ }
+}
+
// This function mirrors the boot sequence of server/src/index.ts — every
// startup step index.ts performs has to be repeated here by hand (#949), and
// __tests__/server-host-boot.test.ts cross-checks this list against the
@@ -98,6 +124,7 @@ export async function startServer(opts: StartOptions): Promise<ServerHandle> {
// the URL saved in the settings table is ignored on every restart and the
// outbound proxy fields appear empty until re-saved.
restoreProxySettings();
+ trustSystemCertificates();
// Rehydrate the persisted response cache, same as server/src/index.ts. The
// desktop app is restarted far more often than a server (every quit, every
// update), so without this the cache is effectively never warm here.
diff --git a/server/src/__tests__/lib/proxy.test.ts b/server/src/__tests__/lib/proxy.test.ts
index d9630751..4e9a3156 100644
--- a/server/src/__tests__/lib/proxy.test.ts
+++ b/server/src/__tests__/lib/proxy.test.ts
@@ -24,6 +24,7 @@ import {
DEFAULT_PROXY_PROBE_TARGET,
parseScutilProxy,
parseRegProxy,
+ normalizeProxyUrl,
} from '../../lib/proxy.js';
// #838 reads the OS-wide proxy (scutil/reg/gsettings) as a last-resort
@@ -1099,3 +1100,79 @@ describe('system proxy detection parsers (#353)', () => {
expect(parseRegProxy(enable, server)).toEqual({ url: '', source: 'none' });
});
});
+
+describe('scheme-less proxy values (#1373)', () => {
+ const REG_ENABLED = 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\n ProxyEnable REG_DWORD 0x1\n';
+ const regServer = (value: string) =>
+ `HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\n ProxyServer REG_SZ ${value}\n`;
+
+ it('gives a bare host:port an http:// scheme', () => {
+ expect(normalizeProxyUrl('127.0.0.1:7890')).toBe('http://127.0.0.1:7890');
+ expect(normalizeProxyUrl(' proxy.corp:8080 ')).toBe('http://proxy.corp:8080');
+ });
+
+ it('leaves a value that already has a scheme alone', () => {
+ expect(normalizeProxyUrl('http://proxy:8080')).toBe('http://proxy:8080');
+ expect(normalizeProxyUrl('socks5h://127.0.0.1:1080')).toBe('socks5h://127.0.0.1:1080');
+ expect(normalizeProxyUrl('')).toBe('');
+ });
+
+ it('uses a scheme-less HTTPS_PROXY instead of dropping it as invalid', () => {
+ process.env.HTTPS_PROXY = '127.0.0.1:7890';
+ applyProxyUrl('');
+ expect(getProxyUrl()).toBe('http://127.0.0.1:7890');
+ expect(isProxyActive()).toBe(true);
+ });
+
+ it('routes through the dispatcher for a scheme-less dashboard value', async () => {
+ applyProxyUrl('proxy:8080');
+ const spy = vi.spyOn(global, 'fetch').mockResolvedValue(okResponse());
+ await proxyFetch('https://api.example.com/v1', { method: 'POST' }, 'groq');
+ const [, init] = spy.mock.calls[0];
+ expect((init as any)?.dispatcher).toBeDefined();
+ });
+
+ it('falls back to the https= entry of a per-scheme Windows ProxyServer', () => {
+ expect(parseRegProxy(REG_ENABLED, regServer('https=proxy.corp:8443;ftp=proxy.corp:21')))
+ .toEqual({ url: 'proxy.corp:8443', source: 'system(Windows)' });
+ });
+
+ it('reads a socks-only Windows ProxyServer as SOCKS, not HTTP', () => {
+ expect(parseRegProxy(REG_ENABLED, regServer('socks=127.0.0.1:1080')))
+ .toEqual({ url: 'socks5://127.0.0.1:1080', source: 'system(Windows)' });
+ });
+});
+
+describe('fetch failed carries its cause (#1373)', () => {
+ const fetchFailed = (cause: unknown) => Object.assign(new TypeError('fetch failed'), { cause });
+
+ it('names a DNS failure and the host', async () => {
+ vi.spyOn(global, 'fetch').mockRejectedValue(fetchFailed(Object.assign(new Error('getaddrinfo ENOTFOUND router.huggingface.co'), { code: 'ENOTFOUND' })));
+ await expect(proxyFetch('https://router.huggingface.co/v1/chat/completions', undefined, 'huggingface'))
+ .rejects.toThrow('fetch failed (ENOTFOUND router.huggingface.co)');
+ });
+
+ it('names a certificate error', async () => {
+ vi.spyOn(global, 'fetch').mockRejectedValue(fetchFailed(Object.assign(new Error('self signed certificate in certificate chain'), { code: 'SELF_SIGNED_CERT_IN_CHAIN' })));
+ await expect(proxyFetch('https://api.groq.com/openai/v1/chat/completions', undefined, 'groq'))
+ .rejects.toThrow('fetch failed (SELF_SIGNED_CERT_IN_CHAIN api.groq.com)');
+ });
+
+ it('says when the request went through the proxy', async () => {
+ applyProxyUrl('http://127.0.0.1:7890');
+ vi.spyOn(global, 'fetch').mockRejectedValue(fetchFailed(Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:7890'), { code: 'ECONNREFUSED' })));
+ await expect(proxyFetch('https://api.groq.com/v1', undefined, 'groq'))
+ .rejects.toThrow('fetch failed (ECONNREFUSED api.groq.com via proxy)');
+ });
+
+ it('finds a code nested deeper in the cause chain', async () => {
+ const inner = Object.assign(new Error('connect ETIMEDOUT'), { code: 'ETIMEDOUT' });
+ vi.spyOn(global, 'fetch').mockRejectedValue(fetchFailed(new Error('wrapper', { cause: inner })));
+ await expect(proxyFetch('https://api.example.com/v1')).rejects.toThrow('fetch failed (ETIMEDOUT api.example.com)');
+ });
+
+ it('keeps a bare fetch failed when there is no cause code', async () => {
+ vi.spyOn(global, 'fetch').mockRejectedValue(fetchFailed(new Error('something odd')));
+ await expect(proxyFetch('https://api.example.com/v1')).rejects.toThrow(/^fetch failed$/);
+ });
+});
diff --git a/server/src/lib/proxy.ts b/server/src/lib/proxy.ts
index 7405c39e..9d49203f 100644
--- a/server/src/lib/proxy.ts
+++ b/server/src/lib/proxy.ts
@@ -122,6 +122,24 @@ function readEnv(name: string): string {
* user deliberately typed into the UI must not be silently overridden by them.
*/
function resolveProxySource(dbValue: string): { url: string; source: string } {
+ const { url, source } = pickProxySource(dbValue);
+ return { url: normalizeProxyUrl(url), source };
+}
+
+/**
+ * Give a scheme-less `host:port` proxy an `http://` scheme. Windows Internet
+ * Options and macOS `scutil --proxy` both store the proxy that way, and so do
+ * plenty of hand-written HTTPS_PROXY values; `new URL('127.0.0.1:7890')`
+ * throws, so without this the proxy was logged as invalid and every request
+ * silently went direct (#1373).
+ */
+export function normalizeProxyUrl(url: string): string {
+ const trimmed = url.trim();
+ if (!trimmed || /^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed)) return trimmed;
+ return `http://${trimmed}`;
+}
+
+function pickProxySource(dbValue: string): { url: string; source: string } {
const explicit = readEnv('PROXY_URL');
if (explicit) return { url: explicit, source: 'PROXY_URL' };
@@ -201,9 +219,20 @@ export function parseRegProxy(enableOut: string, serverOut: string): { url: stri
const m = serverOut.match(/ProxyServer\s+REG_SZ\s+(.+)$/m);
if (!m) return { url: '', source: 'none' };
const raw = m[1].trim();
- const http = raw.split(';').find(p => p.startsWith('http='))?.slice('http='.length) ?? raw;
- if (!http) return { url: '', source: 'none' };
- return { url: http, source: 'system(Windows)' };
+ if (!raw.includes('=')) return { url: raw, source: 'system(Windows)' };
+ // Per-scheme form: prefer the http entry, then https, then socks (which
+ // Internet Options means as SOCKS, so it must not become an HTTP proxy).
+ const entries = new Map(
+ raw.split(';').map(p => p.trim()).filter(Boolean).map(p => {
+ const eq = p.indexOf('=');
+ return [p.slice(0, eq).trim().toLowerCase(), p.slice(eq + 1).trim()] as const;
+ }),
+ );
+ const http = entries.get('http') || entries.get('https');
+ if (http) return { url: http, source: 'system(Windows)' };
+ const socks = entries.get('socks');
+ if (socks) return { url: `socks5://${socks}`, source: 'system(Windows)' };
+ return { url: '', source: 'none' };
}
/** GNOME desktop proxy in manual mode (gsettings). */
@@ -576,6 +605,33 @@ export function describeAbort(
return `${p}, ${t}, ${seconds}s`;
}
+/**
+ * undici reports every connection that never opened as a bare "fetch failed"
+ * and keeps the reason (ENOTFOUND, ECONNREFUSED, a certificate error) in
+ * `err.cause`, which never reached the attempt trail, the logs or the client.
+ * A DNS failure, a dead proxy and TLS-intercepting antivirus all looked the
+ * same (#1373). Append the cause code and the upstream host so the message
+ * says which it was. "fetch failed" stays as the prefix, which the
+ * retry classifier matches on.
+ */
+export function describeFetchFailure(err: unknown, url: string, platform?: string): void {
+ if (!(err instanceof TypeError) || err.message !== 'fetch failed') return;
+ let code = '';
+ const seen = new Set<unknown>();
+ let cur: any = err.cause;
+ for (let depth = 0; cur && typeof cur === 'object' && !seen.has(cur) && depth < 5; depth++) {
+ seen.add(cur);
+ if (typeof cur.code === 'string' && cur.code) { code = cur.code; break; }
+ cur = cur.cause;
+ }
+ if (!code) return;
+ let host = '';
+ try { host = new URL(url).hostname; } catch { /* leave it out */ }
+ const proxied = _proxyEnabled && !!(perKeyProxyStore.getStore() || _proxyUrl) && !shouldBypassProxy(url, platform);
+ const parts = [code, host, proxied ? 'via proxy' : ''].filter(Boolean);
+ err.message = `fetch failed (${parts.join(' ')})`;
+}
+
/**
* Rewrite an AbortError rejection so its `.message` carries the compact
* triage tag `<platform>, <type>, <timeout>s`. Preserves `name: 'AbortError'`
@@ -803,6 +859,7 @@ export async function proxyFetch(
}
return response;
} catch (err) {
+ describeFetchFailure(err, url, platform);
// Rewrite bare "The operation was aborted" rejections so they carry the
// compact triage tag. Preserves the AbortError name so
// `isRetryableError()` still classifies the failure as retryable.