Skip to content

Commit 8cd4ca4

Browse files
committed
fix(server): capture certificate baseline before shell updates
1 parent b5b9c42 commit 8cd4ca4

2 files changed

Lines changed: 75 additions & 12 deletions

File tree

‎apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
77
import * as Effect from "effect/Effect";
88
import * as FileSystem from "effect/FileSystem";
99
import * as Path from "effect/Path";
10+
import * as Stream from "effect/Stream";
11+
import { ChildProcess, ChildProcessSpawner } from "effect/process";
1012
import { describe, expect } from "vite-plus/test";
1113

1214
import {
@@ -15,6 +17,65 @@ import {
1517
} from "./DesktopTelemetryReceiver.ts";
1618

1719
describe("DesktopTelemetryReceiver", () => {
20+
it.live(
21+
"preserves system and bundled roots when the first snapshot withdraws overlapping extras",
22+
() =>
23+
Effect.gen(function* () {
24+
const fs = yield* FileSystem.FileSystem;
25+
const path = yield* Path.Path;
26+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
27+
const directory = yield* fs.makeTempDirectoryScoped();
28+
const certificatePath = path.join(directory, "startup-extra-ca.pem");
29+
yield* fs.writeFileString(
30+
certificatePath,
31+
[...NodeTls.getCACertificates("system"), ...NodeTls.rootCertificates].join("\n"),
32+
);
33+
const child = yield* spawner.spawn(
34+
ChildProcess.make(
35+
process.execPath,
36+
[
37+
"--use-bundled-ca",
38+
"--use-system-ca",
39+
"--input-type=module",
40+
"--eval",
41+
`
42+
import * as NodeAssert from "node:assert/strict";
43+
import * as NodeCrypto from "node:crypto";
44+
import * as NodeTls from "node:tls";
45+
import * as Effect from "effect/Effect";
46+
import * as NodeServices from "@effect/platform-node/NodeServices";
47+
import { makeApplyDesktopShellEnvironmentSnapshot } from ${JSON.stringify(new URL("./DesktopTelemetryReceiver.ts", import.meta.url).href)};
48+
const fingerprints = (certificates) => [...new Set(certificates.map((pem) => new NodeCrypto.X509Certificate(pem).fingerprint256))].sort();
49+
const expected = fingerprints([...NodeTls.getCACertificates("bundled"), ...NodeTls.getCACertificates("system")]);
50+
const applySnapshot = makeApplyDesktopShellEnvironmentSnapshot(process.env, process.platform);
51+
await Effect.runPromise(applySnapshot({
52+
version: 1,
53+
type: "shellEnvironment",
54+
platform: process.platform,
55+
discovered: true,
56+
environment: { NODE_EXTRA_CA_CERTS: null },
57+
}).pipe(Effect.provide(NodeServices.layer)));
58+
NodeAssert.equal(process.env.NODE_EXTRA_CA_CERTS, undefined);
59+
NodeAssert.deepEqual(fingerprints(NodeTls.getCACertificates("default")), expected);
60+
`,
61+
],
62+
{
63+
cwd: path.resolve(import.meta.dirname, "../.."),
64+
env: { NODE_OPTIONS: "", NODE_EXTRA_CA_CERTS: certificatePath },
65+
},
66+
),
67+
);
68+
const result = yield* Effect.all(
69+
{
70+
stderr: child.stderr.pipe(Stream.decodeText(), Stream.mkString),
71+
exitCode: child.exitCode.pipe(Effect.map(Number)),
72+
},
73+
{ concurrency: "unbounded" },
74+
);
75+
expect(result.exitCode, result.stderr).toBe(0);
76+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
77+
);
78+
1879
it.effect("updates live TLS trust when shell certificates arrive after startup", () =>
1980
Effect.gen(function* () {
2081
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/resourceTelemetry/DesktopTelemetryReceiver.ts‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,23 @@ export function makeApplyDesktopShellEnvironmentSnapshot(
225225
message: DesktopShellEnvironmentSnapshot,
226226
) {
227227
if (message.platform !== platform) return;
228+
// Node's first default-CA enumeration consults process.env, so capture it before the patch.
229+
if (message.environment.NODE_EXTRA_CA_CERTS !== undefined && baseCertificates === undefined) {
230+
// Remove only the startup extras, preserving duplicates from bundled or system trust.
231+
const extras = [...NodeTls.getCACertificates("extra")];
232+
baseCertificates = NodeTls.getCACertificates("default").filter((certificate) => {
233+
const index = extras.indexOf(certificate);
234+
if (index === -1) return true;
235+
extras.splice(index, 1);
236+
return false;
237+
});
238+
}
228239
applyShellEnvironment(environment, message.environment);
229240
if (message.environment.NODE_EXTRA_CA_CERTS !== undefined) {
230241
yield* Effect.gen(function* () {
231-
if (baseCertificates === undefined) {
232-
// Remove only the startup extras, preserving duplicates from bundled or system trust.
233-
const extras = [...NodeTls.getCACertificates("extra")];
234-
baseCertificates = NodeTls.getCACertificates("default").filter((certificate) => {
235-
const index = extras.indexOf(certificate);
236-
if (index === -1) return true;
237-
extras.splice(index, 1);
238-
return false;
239-
});
240-
}
242+
const certificates = baseCertificates;
241243
// --use-openssl-ca roots are not enumerable; replacing the list would discard them.
242-
if (baseCertificates.length === 0) {
244+
if (certificates === undefined || certificates.length === 0) {
243245
yield* Effect.logWarning(
244246
"Preserving non-enumerable TLS trust. Restart to apply updated desktop shell certificates.",
245247
);
@@ -255,7 +257,7 @@ export function makeApplyDesktopShellEnvironmentSnapshot(
255257
if (bundle?.trim() && extras.length === 0) {
256258
throw new Error("No PEM certificates found.");
257259
}
258-
NodeTls.setDefaultCACertificates([...baseCertificates!, ...extras]);
260+
NodeTls.setDefaultCACertificates([...certificates, ...extras]);
259261
},
260262
catch: (cause) => new DesktopShellCertificatesUpdateFailed({ cause }),
261263
});

0 commit comments

Comments
 (0)