From 8a1aa4eddc61fe047af94e022e60ced8890a0fd3 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 1 Oct 2026 09:58:12 +0800 Subject: [PATCH 1/5] fix(ca): merge OS trust store into combined-ca.pem The SSL_CERT_FILE bundle is consumed with REPLACEMENT semantics by some launched clients (codex's rustls HTTP stack trusts exactly the file contents, no native roots merged). On Windows the bundle previously held only Node's static Mozilla snapshot plus the MITM root, so any direct, non-proxied connection of such a client could fail whenever the OS store diverged from that snapshot. Merge the OS trust store (Windows cert stores via PowerShell, macOS keychains via security(1); Linux already reads the filesystem bundle) with per-certificate fingerprint dedup, and bound the rebuild (PowerShell spawn cost) to once per 24h per machine via an mtime + CA-fingerprint staleness gate. --- CONFIGURATION.md | 2 +- CONFIGURATION.zh-CN.md | 2 +- src/ca.ts | 136 +++++++++++++++++++++++++++++++++++--- tests/ca-combined.test.ts | 73 +++++++++++++++++++- 4 files changed, 202 insertions(+), 11 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 3822c6ac9c..261a03b57b 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -991,7 +991,7 @@ How each client is pointed at the proxy (set automatically in the child env): | hermes | `HTTPS_PROXY` (plain-http rides absolute-form forward-proxy requests) | `SSL_CERT_FILE` → `combined-ca.pem` (+ legacy `HERMES_CA_BUNDLE` → `root-ca.pem`) | | dsh | `HTTPS_PROXY` (+ `HTTP_PROXY` for plain-http) + `DEEPSEEK_BASE_URL`; **loopback-only** isolated `DSH_HOME` | `SSL_CERT_FILE` → `combined-ca.pem` | -`NODE_EXTRA_CA_CERTS` *appends* to the built-in trust store, so it points at the MITM root alone (`root-ca.pem`). `SSL_CERT_FILE` *replaces* the default CA bundle, so for codex/dsh/hermes it points at `combined-ca.pem` — a bundle containing the MITM root **plus** the system/Node public roots — keeping pip/git/curl style TLS (blind-tunnelled, real certificates) working inside the child env (#152; hermes since #1375, because current hermes resolves ambient trust only through `SSL_CERT_FILE`). +`NODE_EXTRA_CA_CERTS` *appends* to the built-in trust store, so it points at the MITM root alone (`root-ca.pem`). `SSL_CERT_FILE` *replaces* the default CA bundle, so for codex/dsh/hermes it points at `combined-ca.pem` — a bundle containing the MITM root **plus** the OS trust store and the Node public roots. The OS-store merge matters because some launched clients treat this file as their *entire* trust pool (codex's rustls HTTP stack replaces rather than appends, #1802): any direct, non-proxied connection of theirs must validate against this file alone, so the file must be a superset of what the OS itself trusts (Windows cert stores via PowerShell, macOS keychains via `security(1)`; Linux already reads the filesystem bundle; rebuilt at most once per 24 h). Keeping pip/git/curl style TLS (blind-tunnelled, real certificates) working inside the child env is the original reason for the bundle (#152; hermes since #1375, because current hermes resolves ambient trust only through `SSL_CERT_FILE`; OS-store merge since #1802). Claude Code's undici fetch ignores `HTTPS_PROXY`, so cert MITM cannot intercept it. Every claude upstream — including a pre-configured `ANTHROPIC_BASE_URL` relay — is routed through the `/bili/` URL form via `ANTHROPIC_BASE_URL` instead; no CA trust is required. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 251e839b74..7fb46bcba2 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -966,7 +966,7 @@ MITM 只对一份**白名单**中的模型域名生效(`open.bigmodel.cn`、`a | hermes | `HTTPS_PROXY`(明文 http 走 absolute-form 正向代理请求) | `SSL_CERT_FILE` → `combined-ca.pem`(另设旧版 `HERMES_CA_BUNDLE` → `root-ca.pem`) | | dsh | `HTTPS_PROXY`(明文 http 另加 `HTTP_PROXY`)+ `DEEPSEEK_BASE_URL`;**仅回环**隔离 `DSH_HOME` | `SSL_CERT_FILE` → `combined-ca.pem` | -`NODE_EXTRA_CA_CERTS` 是**追加**到内置信任库,所以只指向 MITM 根证书(`root-ca.pem`)即可。`SSL_CERT_FILE` 会**替换**默认 CA bundle,所以 codex/dsh/hermes 指向 `combined-ca.pem` —— 包含 MITM 根证书**加上**系统/Node 公共根 —— 保证子进程环境里 pip/git/curl 类 TLS(盲转发、真证书)不受影响(#152;hermes 自 #1375 起,因为当前 hermes 只经 `SSL_CERT_FILE` 解析环境信任)。 +`NODE_EXTRA_CA_CERTS` 是**追加**到内置信任库,所以只指向 MITM 根证书(`root-ca.pem`)即可。`SSL_CERT_FILE` 会**替换**默认 CA bundle,所以 codex/dsh/hermes 指向 `combined-ca.pem` —— 包含 MITM 根证书**加上**操作系统信任库与 Node 公共根。合并 OS 信任库很关键:部分被启动的客户端把这个文件当作**整个**信任池(codex 的 rustls HTTP 栈是替换而非追加,#1802),它们的直连(不经代理)连接只能靠这个文件校验,因此文件必须是 OS 自身信任集合的超集(Windows 经 PowerShell 读证书库、macOS 经 `security(1)` 读钥匙串;Linux 本就直读文件系统 bundle;最多每 24 小时重建一次)。最初引入该 bundle 是为了让子进程环境里 pip/git/curl 类 TLS(盲转发、真证书)不受影响(#152;hermes 自 #1375 起,因为当前 hermes 只经 `SSL_CERT_FILE` 解析环境信任;OS 信任库合并自 #1802 起)。 Claude Code 的 undici fetch 忽略 `HTTPS_PROXY`,所以证书 MITM 拦不到它。claude 的所有上游 —— 包括预先配置的 `ANTHROPIC_BASE_URL` relay —— 一律改走 `/bili/` URL 形式的 `ANTHROPIC_BASE_URL`;无需任何 CA 信任。 diff --git a/src/ca.ts b/src/ca.ts index c6904f99e0..d55321d5f9 100644 --- a/src/ca.ts +++ b/src/ca.ts @@ -1,17 +1,24 @@ +import crypto from "node:crypto"; +import { execFileSync } from "node:child_process"; import forge from "node-forge"; import fs from "node:fs"; +import os from "node:os"; import path from "node:path"; import tls from "node:tls"; import { caDir } from "./paths.js"; +import { log } from "./logger.js"; const ROOT_CERT_FILE = "root-ca.pem"; const ROOT_KEY_FILE = "root-ca-key.pem"; const COMBINED_CA_FILE = "combined-ca.pem"; const ROOT_CN = "billion-context MITM Root CA"; +// #1802: the combined bundle merges the OS trust store, which costs a +// PowerShell spawn on Windows — bound the rebuild to once per day per machine. +const COMBINED_MAX_AGE_MS = 24 * 60 * 60 * 1000; /** First readable candidate wins per platform; Node's Mozilla root set is - * always merged in, which is ALSO the only root source on Windows (no - * filesystem bundle to probe — no XDG on win32). */ + * always merged in. On Windows there is no filesystem bundle to probe (no + * XDG on win32) — the OS store comes from collectOsStorePems() instead. */ const PLATFORM_CA_CANDIDATES: readonly string[] = process.platform === "darwin" ? ["/etc/ssl/cert.pem", "/private/etc/ssl/cert.pem"] @@ -73,13 +80,126 @@ export function collectSystemCaPems(env: NodeJS.ProcessEnv = process.env): strin return pems; } +/** #1802: sha256 over the whitespace-stripped base64 payload — dedupes the + * same certificate across differently-wrapped PEM encodings (Node's Mozilla + * snapshot vs OS-store exports wrap at different columns). */ +export function pemFingerprint(pem: string): string { + const b64 = pem.replace(/-----(BEGIN|END) CERTIFICATE-----/g, "").replace(/\s+/g, ""); + return crypto.createHash("sha256").update(b64).digest("hex"); +} + +/** #1802: wrap a base64 DER certificate as a 64-column PEM block. */ +export function wrapDerPem(b64: string): string { + const clean = b64.replace(/[^A-Za-z0-9+/=]/g, ""); + const lines: string[] = []; + for (let i = 0; i < clean.length; i += 64) lines.push(clean.slice(i, i + 64)); + return `-----BEGIN CERTIFICATE-----\n${lines.join("\n")}\n-----END CERTIFICATE-----\n`; +} + +/** #1802: decode captured PowerShell output — PS 5.1 pipes are frequently + * UTF-16LE even though our payload is pure ASCII, so a NUL stride at odd + * offsets is the tell; re-decode accordingly instead of dropping everything. */ +export function decodeOsStoreOutput(buf: Buffer): string { + if (buf.length > 2 && buf[1] === 0) return buf.toString("utf16le"); + return buf.toString("utf8"); +} + +/** #1802: parse the NDJSON lines emitted by osStorePowerShellScript() into PEM + * blocks. Malformed/junk lines are skipped, never fatal. */ +export function parseOsStoreNdjson(out: string): string[] { + const pems: string[] = []; + for (const line of out.split(/\r?\n/)) { + const t = line.trim(); + if (!t.startsWith("{")) continue; + try { + const d = (JSON.parse(t) as { d?: unknown }).d; + if (typeof d === "string" && d.length > 0) pems.push(wrapDerPem(d)); + } catch { } + } + return pems; +} + +/** #1802: PowerShell 5.1 script exporting every certificate of the machine + + * user Root stores as one NDJSON {"d":""} line per certificate + * (NDJSON keeps the pipe output parseable regardless of locale/console code + * page — no human-readable table text to mangle). */ +export function osStorePowerShellScript(): string { + return `$ErrorActionPreference='Stop' +$out = New-Object System.Collections.Generic.List[string] +foreach ($loc in 'LocalMachine','CurrentUser') { + $store = New-Object System.Security.Cryptography.X509Certificates.X509Store('Root',$loc) + try { $store.Open('ReadOnly'); foreach ($c in $store.Certificates) { $out.Add([Convert]::ToBase64String($c.Export('Cert'))) } } finally { $store.Close() } +} +$out | ForEach-Object { '{"d":"' + $_ + '"}' }`; +} + +function splitPemBlocks(text: string): string[] { + const out: string[] = []; + const re = /-----BEGIN CERTIFICATE-----[\s\S]*?-----END CERTIFICATE-----\s*/g; + let m: RegExpExecArray | null; + while ((m = re.exec(text))) out.push(m[0]); + return out; +} + +/** #1802: certificates from the OS-native trust store. Launched clients treat + * combined-ca.pem as their ENTIRE trust pool when SSL_CERT_FILE points at it + * (codex/rustls replace, not append — see codex-rs/http-client custom_ca.rs), + * so the bundle must be a superset of what the OS itself trusts: any direct + * (non-MITM) leg of the client validates against this file alone. On Linux + * the OS store IS the filesystem bundle collectSystemCaPems already reads. + * Best-effort: any failure degrades to the previous bundle shape rather than + * breaking startup. */ +export function collectOsStorePems(platform: NodeJS.Platform = process.platform): string[] { + try { + if (platform === "win32") { + const buf = execFileSync("powershell", ["-NoProfile", "-NonInteractive", "-Command", osStorePowerShellScript()], + { timeout: 30_000, maxBuffer: 16 * 1024 * 1024 }); + return parseOsStoreNdjson(decodeOsStoreOutput(buf)); + } + if (platform === "darwin") { + const pems: string[] = []; + const keychains = ["/Library/Keychains/System.keychain"]; + const login = path.join(os.homedir(), "Library/Keychains/login.keychain-db"); + if (fs.existsSync(login)) keychains.push(login); + for (const k of keychains) { + const out = execFileSync("/usr/bin/security", ["find-certificate", "-a", "-p", k], + { encoding: "utf8", timeout: 15_000, maxBuffer: 16 * 1024 * 1024 }); + pems.push(...splitPemBlocks(out)); + } + return pems; + } + return []; + } catch (err) { + log("warn", `os-store merge skipped (${platform}): ${String(err).slice(0, 200)}`); + return []; + } +} + +/** Rebuild combined-ca.pem unless a fresh copy (< 24h) already carries the + * current MITM root CA — the OS-store merge makes each rebuild expensive on + * Windows, so steady-state startups reuse the existing file. */ function writeCombinedBundle(): void { - const certs = new Set(); - for (const pem of collectSystemCaPems()) certs.add(pem.trim()); - for (const pem of tls.rootCertificates) certs.add(pem.trim()); - certs.add(rootCertPem!.trim()); - const body = [...certs].map((pem) => (pem.endsWith("\n") ? pem : pem + "\n")).join(""); - fs.writeFileSync(path.join(caDir(), COMBINED_CA_FILE), body, { mode: 0o644 }); + const file = path.join(caDir(), COMBINED_CA_FILE); + try { + const st = fs.statSync(file); + if (Date.now() - st.mtimeMs < COMBINED_MAX_AGE_MS && fs.readFileSync(file, "utf8").includes(rootCertPem!.trim())) return; + } catch { } + const byFingerprint = new Map(); + const add = (pem: string): void => { + const t = pem.trim(); + if (!t.includes("BEGIN CERTIFICATE")) return; + // Sources like collectSystemCaPems hand over a WHOLE FILE as one + // string — split into single-certificate blocks first or the + // fingerprint lands on the concatenated payload and cross-source + // duplicates survive. + for (const block of splitPemBlocks(t)) byFingerprint.set(pemFingerprint(block), block); + }; + for (const pem of collectSystemCaPems()) add(pem); + for (const pem of collectOsStorePems()) add(pem); + for (const pem of tls.rootCertificates) add(pem); + add(rootCertPem!); + const body = [...byFingerprint.values()].map((pem) => (pem.endsWith("\n") ? pem : pem + "\n")).join(""); + fs.writeFileSync(file, body, { mode: 0o644 }); } function generateRootCA(): { cert: string; key: string } { diff --git a/tests/ca-combined.test.ts b/tests/ca-combined.test.ts index 2a2264b340..55dbdba632 100644 --- a/tests/ca-combined.test.ts +++ b/tests/ca-combined.test.ts @@ -4,7 +4,7 @@ import os from "node:os"; import path from "node:path"; import tls from "node:tls"; import test from "node:test"; -import { combinedCaPath, collectSystemCaPems, ensureRootCA, rootCaPath } from "../src/ca.js"; +import { combinedCaPath, collectOsStorePems, collectSystemCaPems, decodeOsStoreOutput, ensureRootCA, osStorePowerShellScript, parseOsStoreNdjson, pemFingerprint, rootCaPath, wrapDerPem } from "../src/ca.js"; import { resolveCombinedCaPath } from "../src/launcher.js"; import { rmrf } from "./tmp-rm.ts"; @@ -78,3 +78,74 @@ test("#152: resolveCombinedCaPath mirrors the caDir layout", () => { assert.equal(resolveCombinedCaPath({ XDG_DATA_HOME: "/custom/data" } as NodeJS.ProcessEnv), path.join("/custom/data", "billion-context", "ca", "combined-ca.pem")); }); + +const b64Of = (pem: string): string => pem.replace(/-----(BEGIN|END) CERTIFICATE-----/g, "").replace(/\s+/g, ""); + +test("#1802: wrapDerPem round-trips a real root cert at 64 columns", () => { + const b64 = b64Of(tls.rootCertificates[0]); + const wrapped = wrapDerPem(b64); + const lines = wrapped.trim().split("\n"); + assert.equal(lines[0], "-----BEGIN CERTIFICATE-----"); + assert.equal(lines[lines.length - 1], "-----END CERTIFICATE-----"); + const body = lines.slice(1, -1); + for (const l of body.slice(0, -1)) assert.equal(l.length, 64); + assert.ok(body[body.length - 1].length > 0 && body[body.length - 1].length <= 64, "final base64 line is a short tail"); + assert.equal(b64Of(wrapped), b64); +}); + +test("#1802: pemFingerprint dedupes across wrappings, separates distinct certs", () => { + const b64a = b64Of(tls.rootCertificates[0]); + const altWrap = (b64: string) => `-----BEGIN CERTIFICATE-----\n${b64.match(/.{1,32}/g)!.join("\n")}\n-----END CERTIFICATE-----`; + assert.equal(pemFingerprint(tls.rootCertificates[0]), pemFingerprint(altWrap(b64a))); + assert.notEqual(pemFingerprint(tls.rootCertificates[0]), pemFingerprint(altWrap(b64Of(tls.rootCertificates[1])))); +}); + +test("#1802: parseOsStoreNdjson skips junk/malformed lines and tolerates CRLF", () => { + const b64 = b64Of(tls.rootCertificates[0]); + const out = `{"d":"${b64}"}\r\ngarbage line\r\nnot json at all\r\n{"d":""}\r\n{"nope":1}\r\n{"d":"${b64}"}\r\n`; + const pems = parseOsStoreNdjson(out); + assert.equal(pems.length, 2); + assert.equal(pems[0], wrapDerPem(b64)); +}); + +test("#1802: decodeOsStoreOutput handles UTF-16LE PowerShell pipes", () => { + const text = '{"d":"QUJDRA=="}'; + assert.equal(decodeOsStoreOutput(Buffer.from(text, "utf8")), text); + assert.equal(decodeOsStoreOutput(Buffer.from(text, "utf16le")), text); +}); + +test("#1802: osStorePowerShellScript covers both Root stores without interpolation leaks", () => { + const s = osStorePowerShellScript(); + assert.ok(s.includes("X509Store('Root'")); + assert.ok(s.includes("'LocalMachine','CurrentUser'")); + assert.ok(!s.includes("${")); +}); + +test("#1802: collectOsStorePems degrades to [] when platform tooling is absent", () => { + for (const platform of ["win32", "darwin", "linux"] as const) { + assert.deepEqual(collectOsStorePems(platform), []); + } +}); + +test("#1802: combined bundle reused while fresh, rebuilt when stale or CA-missing", () => { + ensureRootCA(); + const file = combinedCaPath(); + const mitmRoot = fs.readFileSync(rootCaPath(), "utf8").trim(); + const future = new Date(Date.now() + 3600e3); + fs.utimesSync(file, future, future); + const before = fs.statSync(file).mtimeMs; + ensureRootCA(); + assert.equal(fs.statSync(file).mtimeMs, before, "fresh bundle carrying the MITM CA is NOT rewritten"); + + const stale = new Date(Date.now() - 25 * 3600e3); + fs.utimesSync(file, stale, stale); + ensureRootCA(); + assert.notEqual(fs.statSync(file).mtimeMs, stale.getTime(), "stale (>24h) bundle is rebuilt"); + assert.ok(fs.readFileSync(file, "utf8").includes(mitmRoot), "rebuilt bundle carries the MITM CA"); + + fs.writeFileSync(file, "-----BEGIN CERTIFICATE-----\n" + Buffer.from("junk").toString("base64") + "\n-----END CERTIFICATE-----\n"); + const freshButWrong = new Date(Date.now() + 3600e3); + fs.utimesSync(file, freshButWrong, freshButWrong); + ensureRootCA(); + assert.ok(fs.readFileSync(file, "utf8").includes(mitmRoot), "fresh file missing the MITM CA is rebuilt"); +}); From b4fd70c3c08be1c4781d3fcde40b58320d0b3be5 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 1 Oct 2026 10:24:54 +0800 Subject: [PATCH 2/5] fix(ca): degrade OS-store merge per source instead of all-or-nothing Review fix for the #1807 feature: one failing source must not discard certificates already collected from the others. - Windows: a Root store that fails to open (LocalMachine or CurrentUser) is skipped via a per-store catch instead of aborting the whole script under $ErrorActionPreference='Stop'. - macOS: a failing keychain (e.g. a locked login keychain) no longer drops the System keychain certs collected before it; only total failure throws and logs, as before. --- src/ca.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ca.ts b/src/ca.ts index d55321d5f9..eac9c76888 100644 --- a/src/ca.ts +++ b/src/ca.ts @@ -128,7 +128,7 @@ export function osStorePowerShellScript(): string { $out = New-Object System.Collections.Generic.List[string] foreach ($loc in 'LocalMachine','CurrentUser') { $store = New-Object System.Security.Cryptography.X509Certificates.X509Store('Root',$loc) - try { $store.Open('ReadOnly'); foreach ($c in $store.Certificates) { $out.Add([Convert]::ToBase64String($c.Export('Cert'))) } } finally { $store.Close() } + try { $store.Open('ReadOnly'); foreach ($c in $store.Certificates) { $out.Add([Convert]::ToBase64String($c.Export('Cert'))) } } catch { } finally { $store.Close() } } $out | ForEach-Object { '{"d":"' + $_ + '"}' }`; } @@ -161,11 +161,17 @@ export function collectOsStorePems(platform: NodeJS.Platform = process.platform) const keychains = ["/Library/Keychains/System.keychain"]; const login = path.join(os.homedir(), "Library/Keychains/login.keychain-db"); if (fs.existsSync(login)) keychains.push(login); + let failures = 0; for (const k of keychains) { - const out = execFileSync("/usr/bin/security", ["find-certificate", "-a", "-p", k], - { encoding: "utf8", timeout: 15_000, maxBuffer: 16 * 1024 * 1024 }); - pems.push(...splitPemBlocks(out)); + // One keychain failing (e.g. a locked login keychain) must not + // drop the certificates already collected from the others. + try { + const out = execFileSync("/usr/bin/security", ["find-certificate", "-a", "-p", k], + { encoding: "utf8", timeout: 15_000, maxBuffer: 16 * 1024 * 1024 }); + pems.push(...splitPemBlocks(out)); + } catch { failures++; } } + if (failures === keychains.length) throw new Error(`all ${keychains.length} keychain exports failed`); return pems; } return []; From 117616f3147b313c604d32480ad007858be9933c Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 1 Oct 2026 10:25:18 +0800 Subject: [PATCH 3/5] test(ca): make OS-store degradation test deterministic on win/mac hosts The previous assertion expected collectOsStorePems([]) for win32 and darwin on EVERY host, which fails on real Windows/macOS machines (the CI matrix includes windows-latest) where the tools exist and export real certificates. Assert [] only where the tooling is absent, and exercise the production export path end-to-end when running on a native host. --- tests/ca-combined.test.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/ca-combined.test.ts b/tests/ca-combined.test.ts index 55dbdba632..ea31d9c388 100644 --- a/tests/ca-combined.test.ts +++ b/tests/ca-combined.test.ts @@ -121,9 +121,16 @@ test("#1802: osStorePowerShellScript covers both Root stores without interpolati assert.ok(!s.includes("${")); }); -test("#1802: collectOsStorePems degrades to [] when platform tooling is absent", () => { - for (const platform of ["win32", "darwin", "linux"] as const) { - assert.deepEqual(collectOsStorePems(platform), []); +test("#1802: collectOsStorePems degrades to [] where the platform tooling is absent", () => { + assert.deepEqual(collectOsStorePems("linux"), [], "linux has no OS-store source"); + if (process.platform !== "win32") assert.deepEqual(collectOsStorePems("win32"), [], "no powershell off-Windows"); + if (process.platform !== "darwin") assert.deepEqual(collectOsStorePems("darwin"), [], "no security(1) off-macOS"); + // On a native host the real store IS reachable — exercise the production + // export path end-to-end (PS script + decode + parse on win32, security(1) on darwin). + if (process.platform === "win32" || process.platform === "darwin") { + const pems = collectOsStorePems(); + assert.ok(pems.length > 0, `native ${process.platform} OS-store export returned no certs`); + for (const pem of pems) assert.ok(pem.startsWith("-----BEGIN CERTIFICATE-----")); } }); From 8f568e59afdccaaa774cc1c319f0b403bbed69c2 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 1 Oct 2026 10:26:25 +0800 Subject: [PATCH 4/5] docs(ca): cite #1807 for the OS-store merge; align logger import alias MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The OS-store merge was developed during the #1802 diagnosis before the dedicated issue existed. Cite the dedicated tracker (#1807) in code comments, test names, and the en/zh config docs so grep-ability matches the repo convention; the PR body carries the 来源 link back to #1802. Also rename the local logger binding to loggerLog to match every other server-side module (no behavior change). --- CONFIGURATION.md | 2 +- CONFIGURATION.zh-CN.md | 2 +- src/ca.ts | 18 +++++++++--------- tests/ca-combined.test.ts | 14 +++++++------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 261a03b57b..7c5491fd61 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -991,7 +991,7 @@ How each client is pointed at the proxy (set automatically in the child env): | hermes | `HTTPS_PROXY` (plain-http rides absolute-form forward-proxy requests) | `SSL_CERT_FILE` → `combined-ca.pem` (+ legacy `HERMES_CA_BUNDLE` → `root-ca.pem`) | | dsh | `HTTPS_PROXY` (+ `HTTP_PROXY` for plain-http) + `DEEPSEEK_BASE_URL`; **loopback-only** isolated `DSH_HOME` | `SSL_CERT_FILE` → `combined-ca.pem` | -`NODE_EXTRA_CA_CERTS` *appends* to the built-in trust store, so it points at the MITM root alone (`root-ca.pem`). `SSL_CERT_FILE` *replaces* the default CA bundle, so for codex/dsh/hermes it points at `combined-ca.pem` — a bundle containing the MITM root **plus** the OS trust store and the Node public roots. The OS-store merge matters because some launched clients treat this file as their *entire* trust pool (codex's rustls HTTP stack replaces rather than appends, #1802): any direct, non-proxied connection of theirs must validate against this file alone, so the file must be a superset of what the OS itself trusts (Windows cert stores via PowerShell, macOS keychains via `security(1)`; Linux already reads the filesystem bundle; rebuilt at most once per 24 h). Keeping pip/git/curl style TLS (blind-tunnelled, real certificates) working inside the child env is the original reason for the bundle (#152; hermes since #1375, because current hermes resolves ambient trust only through `SSL_CERT_FILE`; OS-store merge since #1802). +`NODE_EXTRA_CA_CERTS` *appends* to the built-in trust store, so it points at the MITM root alone (`root-ca.pem`). `SSL_CERT_FILE` *replaces* the default CA bundle, so for codex/dsh/hermes it points at `combined-ca.pem` — a bundle containing the MITM root **plus** the OS trust store and the Node public roots. The OS-store merge matters because some launched clients treat this file as their *entire* trust pool (codex's rustls HTTP stack replaces rather than appends, #1807): any direct, non-proxied connection of theirs must validate against this file alone, so the file must be a superset of what the OS itself trusts (Windows cert stores via PowerShell, macOS keychains via `security(1)`; Linux already reads the filesystem bundle; rebuilt at most once per 24 h). Keeping pip/git/curl style TLS (blind-tunnelled, real certificates) working inside the child env is the original reason for the bundle (#152; hermes since #1375, because current hermes resolves ambient trust only through `SSL_CERT_FILE`; OS-store merge since #1807). Claude Code's undici fetch ignores `HTTPS_PROXY`, so cert MITM cannot intercept it. Every claude upstream — including a pre-configured `ANTHROPIC_BASE_URL` relay — is routed through the `/bili/` URL form via `ANTHROPIC_BASE_URL` instead; no CA trust is required. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 7fb46bcba2..5a71704c46 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -966,7 +966,7 @@ MITM 只对一份**白名单**中的模型域名生效(`open.bigmodel.cn`、`a | hermes | `HTTPS_PROXY`(明文 http 走 absolute-form 正向代理请求) | `SSL_CERT_FILE` → `combined-ca.pem`(另设旧版 `HERMES_CA_BUNDLE` → `root-ca.pem`) | | dsh | `HTTPS_PROXY`(明文 http 另加 `HTTP_PROXY`)+ `DEEPSEEK_BASE_URL`;**仅回环**隔离 `DSH_HOME` | `SSL_CERT_FILE` → `combined-ca.pem` | -`NODE_EXTRA_CA_CERTS` 是**追加**到内置信任库,所以只指向 MITM 根证书(`root-ca.pem`)即可。`SSL_CERT_FILE` 会**替换**默认 CA bundle,所以 codex/dsh/hermes 指向 `combined-ca.pem` —— 包含 MITM 根证书**加上**操作系统信任库与 Node 公共根。合并 OS 信任库很关键:部分被启动的客户端把这个文件当作**整个**信任池(codex 的 rustls HTTP 栈是替换而非追加,#1802),它们的直连(不经代理)连接只能靠这个文件校验,因此文件必须是 OS 自身信任集合的超集(Windows 经 PowerShell 读证书库、macOS 经 `security(1)` 读钥匙串;Linux 本就直读文件系统 bundle;最多每 24 小时重建一次)。最初引入该 bundle 是为了让子进程环境里 pip/git/curl 类 TLS(盲转发、真证书)不受影响(#152;hermes 自 #1375 起,因为当前 hermes 只经 `SSL_CERT_FILE` 解析环境信任;OS 信任库合并自 #1802 起)。 +`NODE_EXTRA_CA_CERTS` 是**追加**到内置信任库,所以只指向 MITM 根证书(`root-ca.pem`)即可。`SSL_CERT_FILE` 会**替换**默认 CA bundle,所以 codex/dsh/hermes 指向 `combined-ca.pem` —— 包含 MITM 根证书**加上**操作系统信任库与 Node 公共根。合并 OS 信任库很关键:部分被启动的客户端把这个文件当作**整个**信任池(codex 的 rustls HTTP 栈是替换而非追加,#1807),它们的直连(不经代理)连接只能靠这个文件校验,因此文件必须是 OS 自身信任集合的超集(Windows 经 PowerShell 读证书库、macOS 经 `security(1)` 读钥匙串;Linux 本就直读文件系统 bundle;最多每 24 小时重建一次)。最初引入该 bundle 是为了让子进程环境里 pip/git/curl 类 TLS(盲转发、真证书)不受影响(#152;hermes 自 #1375 起,因为当前 hermes 只经 `SSL_CERT_FILE` 解析环境信任;OS 信任库合并自 #1807 起)。 Claude Code 的 undici fetch 忽略 `HTTPS_PROXY`,所以证书 MITM 拦不到它。claude 的所有上游 —— 包括预先配置的 `ANTHROPIC_BASE_URL` relay —— 一律改走 `/bili/` URL 形式的 `ANTHROPIC_BASE_URL`;无需任何 CA 信任。 diff --git a/src/ca.ts b/src/ca.ts index eac9c76888..e3f787b952 100644 --- a/src/ca.ts +++ b/src/ca.ts @@ -6,13 +6,13 @@ import os from "node:os"; import path from "node:path"; import tls from "node:tls"; import { caDir } from "./paths.js"; -import { log } from "./logger.js"; +import { log as loggerLog } from "./logger.js"; const ROOT_CERT_FILE = "root-ca.pem"; const ROOT_KEY_FILE = "root-ca-key.pem"; const COMBINED_CA_FILE = "combined-ca.pem"; const ROOT_CN = "billion-context MITM Root CA"; -// #1802: the combined bundle merges the OS trust store, which costs a +// #1807: the combined bundle merges the OS trust store, which costs a // PowerShell spawn on Windows — bound the rebuild to once per day per machine. const COMBINED_MAX_AGE_MS = 24 * 60 * 60 * 1000; @@ -80,7 +80,7 @@ export function collectSystemCaPems(env: NodeJS.ProcessEnv = process.env): strin return pems; } -/** #1802: sha256 over the whitespace-stripped base64 payload — dedupes the +/** #1807: sha256 over the whitespace-stripped base64 payload — dedupes the * same certificate across differently-wrapped PEM encodings (Node's Mozilla * snapshot vs OS-store exports wrap at different columns). */ export function pemFingerprint(pem: string): string { @@ -88,7 +88,7 @@ export function pemFingerprint(pem: string): string { return crypto.createHash("sha256").update(b64).digest("hex"); } -/** #1802: wrap a base64 DER certificate as a 64-column PEM block. */ +/** #1807: wrap a base64 DER certificate as a 64-column PEM block. */ export function wrapDerPem(b64: string): string { const clean = b64.replace(/[^A-Za-z0-9+/=]/g, ""); const lines: string[] = []; @@ -96,7 +96,7 @@ export function wrapDerPem(b64: string): string { return `-----BEGIN CERTIFICATE-----\n${lines.join("\n")}\n-----END CERTIFICATE-----\n`; } -/** #1802: decode captured PowerShell output — PS 5.1 pipes are frequently +/** #1807: decode captured PowerShell output — PS 5.1 pipes are frequently * UTF-16LE even though our payload is pure ASCII, so a NUL stride at odd * offsets is the tell; re-decode accordingly instead of dropping everything. */ export function decodeOsStoreOutput(buf: Buffer): string { @@ -104,7 +104,7 @@ export function decodeOsStoreOutput(buf: Buffer): string { return buf.toString("utf8"); } -/** #1802: parse the NDJSON lines emitted by osStorePowerShellScript() into PEM +/** #1807: parse the NDJSON lines emitted by osStorePowerShellScript() into PEM * blocks. Malformed/junk lines are skipped, never fatal. */ export function parseOsStoreNdjson(out: string): string[] { const pems: string[] = []; @@ -119,7 +119,7 @@ export function parseOsStoreNdjson(out: string): string[] { return pems; } -/** #1802: PowerShell 5.1 script exporting every certificate of the machine + +/** #1807: PowerShell 5.1 script exporting every certificate of the machine + * user Root stores as one NDJSON {"d":""} line per certificate * (NDJSON keeps the pipe output parseable regardless of locale/console code * page — no human-readable table text to mangle). */ @@ -141,7 +141,7 @@ function splitPemBlocks(text: string): string[] { return out; } -/** #1802: certificates from the OS-native trust store. Launched clients treat +/** #1807: certificates from the OS-native trust store. Launched clients treat * combined-ca.pem as their ENTIRE trust pool when SSL_CERT_FILE points at it * (codex/rustls replace, not append — see codex-rs/http-client custom_ca.rs), * so the bundle must be a superset of what the OS itself trusts: any direct @@ -176,7 +176,7 @@ export function collectOsStorePems(platform: NodeJS.Platform = process.platform) } return []; } catch (err) { - log("warn", `os-store merge skipped (${platform}): ${String(err).slice(0, 200)}`); + loggerLog("warn", `os-store merge skipped (${platform}): ${String(err).slice(0, 200)}`); return []; } } diff --git a/tests/ca-combined.test.ts b/tests/ca-combined.test.ts index ea31d9c388..69e6c8cc5a 100644 --- a/tests/ca-combined.test.ts +++ b/tests/ca-combined.test.ts @@ -81,7 +81,7 @@ test("#152: resolveCombinedCaPath mirrors the caDir layout", () => { const b64Of = (pem: string): string => pem.replace(/-----(BEGIN|END) CERTIFICATE-----/g, "").replace(/\s+/g, ""); -test("#1802: wrapDerPem round-trips a real root cert at 64 columns", () => { +test("#1807: wrapDerPem round-trips a real root cert at 64 columns", () => { const b64 = b64Of(tls.rootCertificates[0]); const wrapped = wrapDerPem(b64); const lines = wrapped.trim().split("\n"); @@ -93,14 +93,14 @@ test("#1802: wrapDerPem round-trips a real root cert at 64 columns", () => { assert.equal(b64Of(wrapped), b64); }); -test("#1802: pemFingerprint dedupes across wrappings, separates distinct certs", () => { +test("#1807: pemFingerprint dedupes across wrappings, separates distinct certs", () => { const b64a = b64Of(tls.rootCertificates[0]); const altWrap = (b64: string) => `-----BEGIN CERTIFICATE-----\n${b64.match(/.{1,32}/g)!.join("\n")}\n-----END CERTIFICATE-----`; assert.equal(pemFingerprint(tls.rootCertificates[0]), pemFingerprint(altWrap(b64a))); assert.notEqual(pemFingerprint(tls.rootCertificates[0]), pemFingerprint(altWrap(b64Of(tls.rootCertificates[1])))); }); -test("#1802: parseOsStoreNdjson skips junk/malformed lines and tolerates CRLF", () => { +test("#1807: parseOsStoreNdjson skips junk/malformed lines and tolerates CRLF", () => { const b64 = b64Of(tls.rootCertificates[0]); const out = `{"d":"${b64}"}\r\ngarbage line\r\nnot json at all\r\n{"d":""}\r\n{"nope":1}\r\n{"d":"${b64}"}\r\n`; const pems = parseOsStoreNdjson(out); @@ -108,20 +108,20 @@ test("#1802: parseOsStoreNdjson skips junk/malformed lines and tolerates CRLF", assert.equal(pems[0], wrapDerPem(b64)); }); -test("#1802: decodeOsStoreOutput handles UTF-16LE PowerShell pipes", () => { +test("#1807: decodeOsStoreOutput handles UTF-16LE PowerShell pipes", () => { const text = '{"d":"QUJDRA=="}'; assert.equal(decodeOsStoreOutput(Buffer.from(text, "utf8")), text); assert.equal(decodeOsStoreOutput(Buffer.from(text, "utf16le")), text); }); -test("#1802: osStorePowerShellScript covers both Root stores without interpolation leaks", () => { +test("#1807: osStorePowerShellScript covers both Root stores without interpolation leaks", () => { const s = osStorePowerShellScript(); assert.ok(s.includes("X509Store('Root'")); assert.ok(s.includes("'LocalMachine','CurrentUser'")); assert.ok(!s.includes("${")); }); -test("#1802: collectOsStorePems degrades to [] where the platform tooling is absent", () => { +test("#1807: collectOsStorePems degrades to [] where the platform tooling is absent", () => { assert.deepEqual(collectOsStorePems("linux"), [], "linux has no OS-store source"); if (process.platform !== "win32") assert.deepEqual(collectOsStorePems("win32"), [], "no powershell off-Windows"); if (process.platform !== "darwin") assert.deepEqual(collectOsStorePems("darwin"), [], "no security(1) off-macOS"); @@ -134,7 +134,7 @@ test("#1802: collectOsStorePems degrades to [] where the platform tooling is abs } }); -test("#1802: combined bundle reused while fresh, rebuilt when stale or CA-missing", () => { +test("#1807: combined bundle reused while fresh, rebuilt when stale or CA-missing", () => { ensureRootCA(); const file = combinedCaPath(); const mitmRoot = fs.readFileSync(rootCaPath(), "utf8").trim(); From 793a0acdc5d48d7f01dfe8a74f92ca7684761db6 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 1 Oct 2026 10:52:36 +0800 Subject: [PATCH 5/5] test(claude-native): pre-warm CA on Windows so first-run OS-store merge stays out of the 20s bring-up budget (#1807) --- tests/claude-native.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/claude-native.test.ts b/tests/claude-native.test.ts index 82ec9692a2..e8ad217ee9 100644 --- a/tests/claude-native.test.ts +++ b/tests/claude-native.test.ts @@ -26,6 +26,7 @@ import { resolveClaudeCli, stripClaudeManagedBlock, } from "../src/plugin-install.ts"; +import { ensureRootCA } from "../src/ca.ts"; import { ZONE_PORT_BASE, resolveClaudeNativePort, resolveNativeAttachExternal } from "../src/config.ts"; import { chooseWatchdogParentPid, isClaudeHostArgv, isTransientShArgv, planClaudeNativeBootstrap, readPsProcInfo, readWinProcInfo, resolveClaudeHostPid, splitWindowsCommandLine } from "../src/claude-native-bootstrap.ts"; import { rmrf } from "./tmp-rm.ts"; @@ -1055,6 +1056,26 @@ test("hook e2e: dist script spawns a proxy on the stable port, second run attach cache: path.join(home, "cache"), data: path.join(home, "data"), }; + // #1807: first-run CA init now merges the OS trust store — on Windows + // that is a synchronous PowerShell export (~8s on loaded runners) inside + // the hook's 20s bring-up budget. Pre-warm the CA here (outside the + // budget) so the spawned proxy hits the 24h freshness gate and skips the + // export; the export itself stays covered end-to-end by + // tests/ca-combined.test.ts on native hosts. + if (process.platform === "win32") { + const prevHome = process.env.HOME; + const prevData = process.env.XDG_DATA_HOME; + process.env.HOME = xdg.home; + process.env.XDG_DATA_HOME = xdg.data; + try { + ensureRootCA(); + } finally { + if (prevHome === undefined) delete process.env.HOME; + else process.env.HOME = prevHome; + if (prevData === undefined) delete process.env.XDG_DATA_HOME; + else process.env.XDG_DATA_HOME = prevData; + } + } const port = await freePort(); const instanceFile = path.join(xdg.state, "billion-context", "proxy-origin"); let proxyPid = 0;