From 14ff5829f6b6959b9098ce609274f3404b70f496 Mon Sep 17 00:00:00 2001 From: ScriptedAlchemy Date: Sun, 15 Nov 2026 07:36:03 -0800 Subject: [PATCH] fix(server): CLIProxyAPI updates work behind TLS-inspecting proxies Node trusts only its bundled roots, so on networks whose gateway re-signs GitHub's asset host the update download failed with "unable to get local issuer certificate", surfaced as "Could not install the verified CLIProxyAPI update." The server now adds OS-store roots at startup. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/binCli.ts | 7 ++++++- apps/server/src/outboundHttp.test.ts | 29 ++++++++++++++++++++++++++++ apps/server/src/outboundHttp.ts | 19 ++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/apps/server/src/binCli.ts b/apps/server/src/binCli.ts index 995b207e7c781..305877c172459 100644 --- a/apps/server/src/binCli.ts +++ b/apps/server/src/binCli.ts @@ -17,7 +17,11 @@ import { appCommand } from "./cli/app.ts"; import { browserCommand } from "./cli/browser.ts"; import { pairCommand } from "./cli/pair.ts"; import { hasCloudPublicConfig } from "./cloud/publicConfig.ts"; -import { extendConnectionAttemptTimeout, keepOutboundFetchOnHttp1 } from "./outboundHttp.ts"; +import { + extendConnectionAttemptTimeout, + keepOutboundFetchOnHttp1, + trustSystemCertificates, +} from "./outboundHttp.ts"; import { sharedServerCommandFlags } from "./cli/config.ts"; import { projectCommand } from "./cli/project.ts"; import { threadCommand } from "./cli/thread.ts"; @@ -122,6 +126,7 @@ export const cli = makeCli(); export function runCli() { extendConnectionAttemptTimeout(); keepOutboundFetchOnHttp1(); + trustSystemCertificates(); offerCliInstall({ args: process.argv.slice(2), // A child started over IPC (a service launcher) has no one to answer. diff --git a/apps/server/src/outboundHttp.test.ts b/apps/server/src/outboundHttp.test.ts index ed3b09abaa5e6..410c3dadba271 100644 --- a/apps/server/src/outboundHttp.test.ts +++ b/apps/server/src/outboundHttp.test.ts @@ -16,6 +16,7 @@ import { builtinFetchUsesHttp2, ignoreMacosTypeOfServiceEinval, installHttp1FetchDispatcher, + trustSystemCertificates, } from "./outboundHttp.ts"; // Non-secret, self-signed 127.0.0.1 certificate, valid until 2126; ALPN h2 requires TLS. @@ -164,3 +165,31 @@ if (calls === 0) throw new Error("fetch did not call setTypeOfService"); }).pipe(Effect.provide(NodeServices.layer)), ); }); + +describe("trustSystemCertificates", () => { + const fakeTls = (stores: { default: Array; system: Array | Error }) => { + const installed: Array> = []; + const tls = { + getCACertificates: (type: "default" | "system") => { + const store = stores[type]; + if (store instanceof Error) throw store; + return store; + }, + setDefaultCACertificates: (certificates: Array) => void installed.push(certificates), + } as unknown as typeof NodeTls; + trustSystemCertificates(tls); + return installed; + }; + + it("adds only OS roots Node does not already trust", () => { + expect(fakeTls({ default: ["bundled", "shared"], system: ["shared", "corp"] })).toEqual([ + ["bundled", "shared", "corp"], + ]); + expect(fakeTls({ default: ["bundled"], system: ["bundled"] })).toEqual([]); + }); + + it("keeps existing trust when it is not enumerable or the OS store is unreadable", () => { + expect(fakeTls({ default: [], system: ["corp"] })).toEqual([]); + expect(fakeTls({ default: ["bundled"], system: new Error("keychain locked") })).toEqual([]); + }); +}); diff --git a/apps/server/src/outboundHttp.ts b/apps/server/src/outboundHttp.ts index ead14cb5d2035..a5992172989b3 100644 --- a/apps/server/src/outboundHttp.ts +++ b/apps/server/src/outboundHttp.ts @@ -1,5 +1,6 @@ import * as NodeModule from "node:module"; import * as NodeNet from "node:net"; +import * as NodeTls from "node:tls"; import type * as Undici from "undici"; @@ -31,6 +32,24 @@ export function keepOutboundFetchOnHttp1(): void { if (builtinFetchUsesHttp2(process.versions.undici)) installHttp1FetchDispatcher(); } +/** + * TLS-inspecting corporate gateways re-sign traffic with roots that live only in the OS + * store, which curl and browsers trust but Node does not. Without them GitHub release + * downloads fail with "unable to get local issuer certificate". + */ +export function trustSystemCertificates(tls: typeof NodeTls = NodeTls): void { + try { + const current = tls.getCACertificates("default"); + // --use-openssl-ca roots are not enumerable; replacing the list would discard them. + if (current.length === 0) return; + const known = new Set(current); + const added = tls.getCACertificates("system").filter((certificate) => !known.has(certificate)); + if (added.length > 0) tls.setDefaultCACertificates([...current, ...added]); + } catch { + // An unreadable OS store leaves Node's bundled trust in place. + } +} + // @types/node does not declare setTypeOfService yet. type TypeOfServiceSocket = { setTypeOfService?: (this: NodeNet.Socket, typeOfService: number) => NodeNet.Socket;