Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): skip expired OS trust anchors when merging the CA set

luoshangf5b35021b82d
diff --git a/packages/agent-runtime/src/system-ca.test.ts b/packages/agent-runtime/src/system-ca.test.ts
index f2119849f..0749ff3e4 100644
--- a/packages/agent-runtime/src/system-ca.test.ts
+++ b/packages/agent-runtime/src/system-ca.test.ts
@@ -28,3 +28,30 @@ it("applyAdditiveDefaultCaCertificates is idempotent", () => {
const twice = fingerprints(tls.getCACertificates("default")).size;
expect(twice).toBe(once);
});
+
+it("applyAdditiveDefaultCaCertificates never adds an already expired system anchor", () => {
+ const now = Date.now();
+ const bundled = fingerprints(tls.getCACertificates("bundled"));
+ const expiredSystem = tls
+ .getCACertificates("system")
+ .filter((pem) => {
+ try {
+ return new Date(new X509Certificate(pem).validTo).getTime() <= now;
+ } catch {
+ return false;
+ }
+ })
+ .map((pem) => new X509Certificate(pem).fingerprint256);
+
+ applyAdditiveDefaultCaCertificates();
+
+ const after = fingerprints(tls.getCACertificates("default"));
+ for (const fingerprint of expiredSystem) {
+ // A bundled root that happens to be expired stays: the bundled set is the
+ // runtime's own baseline. Only the system merge is filtered, because a
+ // stale cache entry there can act as the anchor for a live chain.
+ if (bundled.has(fingerprint)) continue;
+ expect(after.has(fingerprint)).toBe(false);
+ }
+});
+
diff --git a/packages/agent-runtime/src/system-ca.ts b/packages/agent-runtime/src/system-ca.ts
index 6dcebe3fe..12e0a070d 100644
--- a/packages/agent-runtime/src/system-ca.ts
+++ b/packages/agent-runtime/src/system-ca.ts
@@ -1,3 +1,4 @@
+import { X509Certificate } from "node:crypto";
import * as tls from "node:tls";
/**
@@ -18,7 +19,25 @@ import * as tls from "node:tls";
* and an inherited `NODE_EXTRA_CA_CERTS` still lands in the `extra` set.
* Certificate chain, expiration and hostname validation are untouched — this
* widens the trust set, it never disables verification.
+ *
+ * The OS stores are a cache, so they keep entries that already expired: Windows
+ * still lists Let's Encrypt's `ISRG Root X2` and `R3` with a 2025-09-15 expiry
+ * long after the fact. Merging one of those makes OpenSSL accept an expired
+ * certificate as the anchor for any chain that names it as issuer, and the
+ * handshake then fails with `CERT_HAS_EXPIRED` even though the server's own
+ * chain is valid and reaches a current root. An anchor that has already
+ * expired can verify nothing, so it is never merged.
*/
+function isExpiredCertificate(pem: string, now: number): boolean {
+ try {
+ return new Date(new X509Certificate(pem).validTo).getTime() <= now;
+ } catch {
+ // Unparseable input is left to the runtime: dropping it would silently
+ // narrow the trust set this function exists to widen.
+ return false;
+ }
+}
+
export function applyAdditiveDefaultCaCertificates(): void {
if (typeof tls.setDefaultCACertificates !== "function") return;
const bundled = tls.getCACertificates("bundled");
@@ -27,11 +46,12 @@ export function applyAdditiveDefaultCaCertificates(): void {
if (system.length === 0) return;
const seen = new Set(bundled.map((cert) => cert));
const merged = [...bundled, ...extra.filter((cert) => !seen.has(cert))];
+ const now = Date.now();
for (const cert of system) {
- if (!seen.has(cert)) {
- seen.add(cert);
- merged.push(cert);
- }
+ if (seen.has(cert)) continue;
+ if (isExpiredCertificate(cert, now)) continue;
+ seen.add(cert);
+ merged.push(cert);
}
try {
tls.setDefaultCACertificates(merged);
diff --git a/scripts/e2e-provider-certificates.mjs b/scripts/e2e-provider-certificates.mjs
index 9990ffa2d..2ee8ef939 100644
--- a/scripts/e2e-provider-certificates.mjs
+++ b/scripts/e2e-provider-certificates.mjs
@@ -54,8 +54,12 @@ try {
define: { PI_BUNDLED_NODE: "true" },
banner: { js: `import { createRequire as __piCreateRequire } from 'node:module'; const require = __piCreateRequire(import.meta.url);
import __certificateProbeTls from 'node:tls';
-{ const tls = __certificateProbeTls; const defaults = new Set(tls.getCACertificates('default'));
-process.stdout.write(JSON.stringify({jsonrpc:'2.0',method:'test.trust',params:{systemCount:tls.getCACertificates('system').length,systemIncluded:tls.getCACertificates('system').every(c=>defaults.has(c)),extraIncluded:tls.getCACertificates('extra').every(c=>defaults.has(c))}})+'\\n'); }` },
+{ const tls = __certificateProbeTls; const { X509Certificate: __ProbeCert } = require('node:crypto');
+const defaults = new Set(tls.getCACertificates('default')); const now = Date.now();
+// An OS store is a cache and keeps expired entries; a merged expired anchor can
+// never verify anything, so only the usable roots are expected in the defaults.
+const usable = tls.getCACertificates('system').filter(c=>{ try { return new Date(new __ProbeCert(c).validTo).getTime() > now } catch { return true } });
+process.stdout.write(JSON.stringify({jsonrpc:'2.0',method:'test.trust',params:{systemCount:tls.getCACertificates('system').length,usableSystemCount:usable.length,systemIncluded:usable.every(c=>defaults.has(c)),extraIncluded:tls.getCACertificates('extra').every(c=>defaults.has(c))}})+'\\n'); }` },
});
await build({ entryPoints: [join(root, "scripts/e2e/provider-certificate-sidecar.ts")],
outfile: join(temp, "parent.mjs"), bundle: true, platform: "node", format: "esm",
diff --git a/scripts/e2e/provider-certificate-sidecar.ts b/scripts/e2e/provider-certificate-sidecar.ts
index a736dcdd3..80326bb48 100644
--- a/scripts/e2e/provider-certificate-sidecar.ts
+++ b/scripts/e2e/provider-certificate-sidecar.ts
@@ -8,7 +8,14 @@ sidecar.setHost({
async call<T>() { return { session: { messages: [] } } as T; },
onNotification: () => () => {}, onExit: () => () => {},
});
-let trust: { systemCount: number; systemIncluded: boolean; extraIncluded: boolean } | undefined;
+let trust:
+ | {
+ systemCount: number;
+ usableSystemCount: number;
+ systemIncluded: boolean;
+ extraIncluded: boolean;
+ }
+ | undefined;
const events: AgentEventEnvelope[] = [];
const ended = Promise.withResolvers<void>();
sidecar.onNotification((method, params) => {
@@ -32,7 +39,10 @@ try {
},
});
await ended.promise;
- assert.ok(trust?.systemIncluded, "sidecar must include OS-trusted roots");
+ assert.ok(
+ trust?.systemIncluded,
+ "sidecar must include every non-expired OS-trusted root",
+ );
assert.ok(trust?.extraIncluded, "inherited extra CA must remain trusted");
const messages = events.flatMap(({ event }) => event.type === "message_end" ? [event.message] : []);
const expected = process.argv[4];