Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions scripts/live-files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 10 additions & 7 deletions src/upstream-ca.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
126 changes: 126 additions & 0 deletions tests/upstream-ca-default-trust.test.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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<string>((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<void>((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/);
});
Loading