Skip to content
Merged
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
7 changes: 6 additions & 1 deletion apps/server/src/binCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import { appCommand } from "./cli/app.ts";
import { browserCommand } from "./cli/browser.ts";
import { pairCommand } from "./cli/pair.ts";
import { hasCloudPublicConfig } from "./cloud/publicConfig.ts";
import { extendConnectionAttemptTimeout, keepOutboundFetchOnHttp1 } from "./outboundHttp.ts";
import {
extendConnectionAttemptTimeout,
keepOutboundFetchOnHttp1,
trustSystemCertificates,
} from "./outboundHttp.ts";
import { sharedServerCommandFlags } from "./cli/config.ts";
import { projectCommand } from "./cli/project.ts";
import { threadCommand } from "./cli/thread.ts";
Expand Down Expand Up @@ -122,6 +126,7 @@ export const cli = makeCli();
export function runCli() {
extendConnectionAttemptTimeout();
keepOutboundFetchOnHttp1();
trustSystemCertificates();
offerCliInstall({
args: process.argv.slice(2),
// A child started over IPC (a service launcher) has no one to answer.
Expand Down
29 changes: 29 additions & 0 deletions apps/server/src/outboundHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
builtinFetchUsesHttp2,
ignoreMacosTypeOfServiceEinval,
installHttp1FetchDispatcher,
trustSystemCertificates,
} from "./outboundHttp.ts";

// Non-secret, self-signed 127.0.0.1 certificate, valid until 2126; ALPN h2 requires TLS.
Expand Down Expand Up @@ -164,3 +165,31 @@ if (calls === 0) throw new Error("fetch did not call setTypeOfService");
}).pipe(Effect.provide(NodeServices.layer)),
);
});

describe("trustSystemCertificates", () => {
const fakeTls = (stores: { default: Array<string>; system: Array<string> | Error }) => {
const installed: Array<Array<string>> = [];
const tls = {
getCACertificates: (type: "default" | "system") => {
const store = stores[type];
if (store instanceof Error) throw store;
return store;
},
setDefaultCACertificates: (certificates: Array<string>) => void installed.push(certificates),
} as unknown as typeof NodeTls;
trustSystemCertificates(tls);
return installed;
};

it("adds only OS roots Node does not already trust", () => {
expect(fakeTls({ default: ["bundled", "shared"], system: ["shared", "corp"] })).toEqual([
["bundled", "shared", "corp"],
]);
expect(fakeTls({ default: ["bundled"], system: ["bundled"] })).toEqual([]);
});

it("keeps existing trust when it is not enumerable or the OS store is unreadable", () => {
expect(fakeTls({ default: [], system: ["corp"] })).toEqual([]);
expect(fakeTls({ default: ["bundled"], system: new Error("keychain locked") })).toEqual([]);
});
});
19 changes: 19 additions & 0 deletions apps/server/src/outboundHttp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as NodeModule from "node:module";
import * as NodeNet from "node:net";
import * as NodeTls from "node:tls";

import type * as Undici from "undici";

Expand Down Expand Up @@ -31,6 +32,24 @@ export function keepOutboundFetchOnHttp1(): void {
if (builtinFetchUsesHttp2(process.versions.undici)) installHttp1FetchDispatcher();
}

/**
* TLS-inspecting corporate gateways re-sign traffic with roots that live only in the OS
* store, which curl and browsers trust but Node does not. Without them GitHub release
* downloads fail with "unable to get local issuer certificate".
*/
export function trustSystemCertificates(tls: typeof NodeTls = NodeTls): void {
try {
const current = tls.getCACertificates("default");
// --use-openssl-ca roots are not enumerable; replacing the list would discard them.
if (current.length === 0) return;
const known = new Set(current);
const added = tls.getCACertificates("system").filter((certificate) => !known.has(certificate));
if (added.length > 0) tls.setDefaultCACertificates([...current, ...added]);
} catch {
// An unreadable OS store leaves Node's bundled trust in place.
}
}

// @types/node does not declare setTypeOfService yet.
type TypeOfServiceSocket = {
setTypeOfService?: (this: NodeNet.Socket, typeOfService: number) => NodeNet.Socket;
Expand Down
Loading