From ccfd44a05b70ed1466779ed58be25b2da8692f64 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 11:01:36 +0800 Subject: [PATCH 1/2] fix: preserve Node default trust when adding an upstream CA --- src/upstream-ca.ts | 17 ++-- tests/upstream-ca-default-trust.test.ts | 126 ++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 tests/upstream-ca-default-trust.test.ts diff --git a/src/upstream-ca.ts b/src/upstream-ca.ts index b021f135dc..9831cbd9eb 100644 --- a/src/upstream-ca.ts +++ b/src/upstream-ca.ts @@ -1,10 +1,9 @@ // #3776 sub-defect 2: CA trust material for bili's OWN outbound upstream fetch. -// Node's default TLS trust is the built-in Mozilla bundle + NODE_EXTRA_CA_CERTS; -// it does NOT read the OS certificate store, so a security-software MITM root -// (Kaspersky / corporate DLP) installed in the Windows/Mac store is untrusted and +// Node's default TLS trust may omit the OS certificate store, so a security-software MITM root +// (Kaspersky / corporate DLP) installed in the Windows/Mac store can be untrusted and // every request dies with SELF_SIGNED_CERT_IN_CHAIN (#3773). network.caFile / -// BILI_CA_FILE points at that vendor's PEM; we MERGE it with the built-ins (never -// replace) so all other upstreams keep working. When unset we return undefined and +// BILI_CA_FILE points at that vendor's PEM; we MERGE it with Node's effective trust +// so all other upstreams keep working. When unset we return undefined and // the dispatcher uses Node's default trust byte-for-byte (no connect override). import { readFileSync, statSync } from "node:fs"; @@ -47,7 +46,7 @@ export function upstreamCaIdentity(now = Date.now()): string { return lastIdentity; } -/** Merged CA list (built-ins + NODE_EXTRA_CA_CERTS + network.caFile) for bili's +/** Merged CA list (Node's effective trust + NODE_EXTRA_CA_CERTS + network.caFile) for bili's * own fetch, or undefined when no caFile is configured — in which case the * dispatcher must NOT set connect.ca and Node's default trust applies unchanged. * The merge (not replace) matters: an explicit undici connect.ca REPLACES the @@ -56,7 +55,11 @@ export function upstreamExtraCa(): string[] | undefined { const identity = upstreamCaIdentity(); if (identity === "none") return undefined; if (cacheKey === identity) return cacheValue; - const merged: string[] = [...tls.rootCertificates]; + // Node 22.15+ exposes the effective store, including opted-in system CAs + // and programmatic defaults. rootCertificates contains only bundled roots. + const merged: string[] = typeof tls.getCACertificates === "function" + ? [...tls.getCACertificates("default")] + : [...tls.rootCertificates]; const nodeExtra = process.env["NODE_EXTRA_CA_CERTS"]; // Node treats NODE_EXTRA_CA_CERTS as ONE file path — no list separator on any // platform (verified against Node 22: the whole value is loaded as a single diff --git a/tests/upstream-ca-default-trust.test.ts b/tests/upstream-ca-default-trust.test.ts new file mode 100644 index 0000000000..8f853e38ff --- /dev/null +++ b/tests/upstream-ca-default-trust.test.ts @@ -0,0 +1,126 @@ +import assert from "node:assert/strict"; +import { execFileSync } from "node:child_process"; +import { once } from "node:events"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import https from "node:https"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import tls from "node:tls"; +import { fileURLToPath } from "node:url"; +import { after, afterEach, before, test } from "node:test"; +import { ensureRootCA, mintHostCert, pemFingerprint, rootCaPath, _resetForTest as resetCa } from "../src/ca.ts"; +import { upstreamExtraCa, _resetUpstreamCaForTest } from "../src/upstream-ca.ts"; + +const supported = typeof tls.getCACertificates === "function" && typeof tls.setDefaultCACertificates === "function"; +const originalDefaults = supported ? tls.getCACertificates("default") : []; +const previousCa = process.env.BILI_CA_FILE; +const previousExtra = process.env.NODE_EXTRA_CA_CERTS; +const directory = mkdtempSync(join(tmpdir(), "bili-default-ca-")); +const additionalFile = join(directory, "additional.pem"); +let trustedRoot: string; +let trustedRootFile: string; +let leaf: { keyPem: string; certPem: string }; + +before(() => { + if (!supported) return; + const previousData = process.env.XDG_DATA_HOME; + process.env.XDG_DATA_HOME = directory; + try { + resetCa(); + ensureRootCA(); + trustedRootFile = rootCaPath(); + trustedRoot = readFileSync(trustedRootFile, "utf8"); + leaf = mintHostCert("localhost"); + writeFileSync(additionalFile, tls.rootCertificates[0]!); + } finally { + if (previousData === undefined) delete process.env.XDG_DATA_HOME; + else process.env.XDG_DATA_HOME = previousData; + resetCa(); + } +}); + +afterEach(() => { + if (supported) tls.setDefaultCACertificates(originalDefaults); + if (previousCa === undefined) delete process.env.BILI_CA_FILE; + else process.env.BILI_CA_FILE = previousCa; + if (previousExtra === undefined) delete process.env.NODE_EXTRA_CA_CERTS; + else process.env.NODE_EXTRA_CA_CERTS = previousExtra; + _resetUpstreamCaForTest(); +}); + +after(() => rmSync(directory, { recursive: true, force: true })); + +function setDefaultTrust(): void { + tls.setDefaultCACertificates([trustedRoot]); + delete process.env.NODE_EXTRA_CA_CERTS; + process.env.BILI_CA_FILE = additionalFile; + _resetUpstreamCaForTest(); +} + +async function requestWithConfiguredTrust(): Promise { + const server = https.createServer({ key: leaf.keyPem, cert: leaf.certPem }, (_request, response) => response.end("trusted default CA")); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + try { + return await new Promise((resolve, reject) => { + const request = https.get({ hostname: "localhost", port: address.port, ca: upstreamExtraCa(), agent: false, signal: AbortSignal.timeout(3000) }, (response) => { + let body = ""; + response.setEncoding("utf8"); + response.on("data", (chunk: string) => { body += chunk; }); + response.on("end", () => resolve(body)); + response.on("error", reject); + }); + request.on("error", reject); + }); + } finally { + server.closeAllConnections(); + await new Promise((resolve) => server.close(() => resolve())); + } +} + +test("adding network.caFile preserves CA certificates already in Node's effective default store", { skip: !supported }, () => { + setDefaultTrust(); + const fingerprints = new Set(upstreamExtraCa()!.map(pemFingerprint)); + assert.ok(fingerprints.has(pemFingerprint(trustedRoot))); + assert.ok(fingerprints.has(pemFingerprint(tls.rootCertificates[0]!))); +}); + +test("the merged trust accepts a real HTTPS server signed by a default-only CA", { skip: !supported }, async () => { + setDefaultTrust(); + assert.equal(await requestWithConfiguredTrust(), "trusted default CA"); +}); + +test("merging an extra CA does not reintroduce bundled roots excluded from default trust", { skip: !supported }, () => { + setDefaultTrust(); + const fingerprints = new Set(upstreamExtraCa()!.map(pemFingerprint)); + assert.ok(!fingerprints.has(pemFingerprint(tls.rootCertificates[1]!))); +}); + +test("without network.caFile, native default trust remains untouched", { skip: !supported }, async () => { + setDefaultTrust(); + delete process.env.BILI_CA_FILE; + assert.equal(upstreamExtraCa(), undefined); + assert.equal(await requestWithConfiguredTrust(), "trusted default CA"); +}); + +test("a Linux process started with --use-system-ca retains its SSL_CERT_FILE trust", { skip: !supported || process.platform !== "linux" }, () => { + const certificateDirectory = join(directory, "empty-certs"); + mkdirSync(certificateDirectory); + const result = execFileSync(process.execPath, ["--use-system-ca", "--import", "tsx", "--input-type=module", "-e", ` + import assert from "node:assert/strict"; + import { readFileSync } from "node:fs"; + import tls from "node:tls"; + import { pemFingerprint } from "./src/ca.ts"; + import { upstreamExtraCa } from "./src/upstream-ca.ts"; + const expected = pemFingerprint(readFileSync(process.env.SSL_CERT_FILE, "utf8")); + assert.ok(tls.getCACertificates("default").some(pem => pemFingerprint(pem) === expected)); + assert.ok(upstreamExtraCa().some(pem => pemFingerprint(pem) === expected)); + console.log("system CA retained"); + `], { + cwd: fileURLToPath(new URL("..", import.meta.url)), encoding: "utf8", timeout: 10_000, + env: { ...process.env, SSL_CERT_FILE: trustedRootFile, SSL_CERT_DIR: certificateDirectory, BILI_CA_FILE: additionalFile, NODE_EXTRA_CA_CERTS: "" }, + }); + assert.match(result, /system CA retained/); +}); From b20d51ae076d4d82eec8bbc3722a92bdca67fdb1 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 13:57:56 +0800 Subject: [PATCH 2/2] test: register the default CA trust child in the CI live-file table --- scripts/live-files.mjs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/live-files.mjs b/scripts/live-files.mjs index 7bd93fe7d3..30c4a60135 100644 --- a/scripts/live-files.mjs +++ b/scripts/live-files.mjs @@ -147,6 +147,9 @@ export const LIVE_FILES = [ // Native-zstd absence is simulated in an isolated Node child; it binds // no sockets and changes only that child's built-in module exports. "tests/content-encoding-zstd.test.ts", + // One Linux-only Node child reads fixture system CAs and exits within 10s; + // no child ports or network I/O. In-process HTTPS fixtures use listen(0). + "tests/upstream-ca-default-trust.test.ts", ]; /** Band width per live file: the zone ladder drifts +1 per EADDRINUSE retry;