From 92be663257537589fbcbd1c39c8a997117e2f1e7 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Tue, 16 Apr 2024 02:23:21 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20use=20system=20certificates=20in?= =?UTF-8?q?=20binary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binary/package-lock.json | 132 +++++++++++++++++++++++++++++++++- binary/package.json | 5 +- binary/src/ca.ts | 23 ++++++ binary/src/index.ts | 3 + core/util/fetchWithOptions.ts | 11 ++- 5 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 binary/src/ca.ts diff --git a/binary/package-lock.json b/binary/package-lock.json index bbf793193..4cf2af206 100644 --- a/binary/package-lock.json +++ b/binary/package-lock.json @@ -13,10 +13,13 @@ "commander": "^12.0.0", "core": "file:../core", "follow-redirects": "^1.15.5", + "mac-ca": "^2.0.3", "ncp": "^2.0.0", "posthog-node": "^3.6.3", + "system-ca": "^1.0.2", "uuid": "^9.0.1", - "vectordb": "^0.4.10" + "vectordb": "^0.4.10", + "win-ca": "^3.5.1" }, "bin": { "binary": "out/index.js" @@ -1015,6 +1018,15 @@ "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", "integrity": "sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==" }, + "node_modules/bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "optional": true, + "dependencies": { + "file-uri-to-path": "1.0.0" + } + }, "node_modules/bl": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", @@ -1387,6 +1399,12 @@ "reusify": "^1.0.4" } }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "optional": true + }, "node_modules/fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -1653,6 +1671,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-electron": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/is-electron/-/is-electron-2.2.2.tgz", + "integrity": "sha512-FO/Rhvz5tuw4MCWkpMzHFKWD2LsfHzIb7i6MdPYZ/KW7AlxawyLkqdy+jPZP1WubqEADE3O4FUENlJHDfQASRg==" + }, "node_modules/is-extglob": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", @@ -1755,6 +1778,39 @@ "node": ">=10" } }, + "node_modules/mac-ca": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/mac-ca/-/mac-ca-2.0.3.tgz", + "integrity": "sha512-FkcNXYdUzfNEElPoYYLDHOXPCQePj4iK7Ir4OYy6Tr7yAUS2bFud4Uh1QdIsATDwRQN/miNUyZEltX2nA/tDAQ==", + "dependencies": { + "node-forge": "^1.3.1" + } + }, + "node_modules/macos-export-certificate-and-key": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/macos-export-certificate-and-key/-/macos-export-certificate-and-key-1.1.1.tgz", + "integrity": "sha512-J2g0dJRLG3DghmdCkbJnif/zPmSylj6ql//xBYff5allzNlHPnWxRoyho9XznBYLbPJw4jZlKjMO69jtV8VC7Q==", + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "dependencies": { + "bindings": "^1.5.0", + "node-addon-api": "^4.3.0" + } + }, + "node_modules/make-dir": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.3.0.tgz", + "integrity": "sha512-2w31R7SJtieJJnQtGc7RVL2StM2vGYVfqUOvUDxH6bC6aJTxPxTF0GnIgCyu7tjockiUWAYQRbxa7vKn34s5sQ==", + "dependencies": { + "pify": "^3.0.0" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/merge2": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", @@ -1893,6 +1949,12 @@ "node": ">=10" } }, + "node_modules/node-addon-api": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-4.3.0.tgz", + "integrity": "sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ==", + "optional": true + }, "node_modules/node-fetch": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", @@ -1913,6 +1975,14 @@ } } }, + "node_modules/node-forge": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/node-forge/-/node-forge-1.3.1.tgz", + "integrity": "sha512-dPEtOeMvF9VMcYV/1Wb8CPoVAXtp6MKMlcbAt4ddqmGqUJ6fQZFXkNZNkNlfevtNkGtaSoXf/vNNNSvgrdXwtA==", + "engines": { + "node": ">= 6.13.0" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -1969,6 +2039,14 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/pify": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", + "integrity": "sha512-C3FsVNH1udSEX48gGX1xfvwTWfsYWj5U+8/uK15BGzIGrKoUpghX8hWZwa/OFnakBiiVNmBvemTJR5mcy7iPcg==", + "engines": { + "node": ">=4" + } + }, "node_modules/pkg": { "version": "5.8.1", "resolved": "https://registry.npmjs.org/pkg/-/pkg-5.8.1.tgz", @@ -2299,6 +2377,17 @@ "node": ">=8" } }, + "node_modules/split": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "dependencies": { + "through": "2" + }, + "engines": { + "node": "*" + } + }, "node_modules/stream-meter": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/stream-meter/-/stream-meter-1.0.4.tgz", @@ -2384,6 +2473,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/system-ca": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/system-ca/-/system-ca-1.0.2.tgz", + "integrity": "sha512-/6CCJOKB5Fpi0x7/DCbV7uiFPgwGCeJsAaSondXS2DjLBv7ER2worVGvQWJqPM0kgOKO6auaCcSWpJKnrDmXjw==", + "optionalDependencies": { + "macos-export-certificate-and-key": "^1.1.1", + "win-export-certificate-and-key": "^1.1.1" + } + }, "node_modules/table-layout": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/table-layout/-/table-layout-3.0.2.tgz", @@ -2465,6 +2563,11 @@ "node": ">= 6" } }, + "node_modules/through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==" + }, "node_modules/to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", @@ -2609,6 +2712,33 @@ "webidl-conversions": "^3.0.0" } }, + "node_modules/win-ca": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/win-ca/-/win-ca-3.5.1.tgz", + "integrity": "sha512-RNy9gpBS6cxWHjfbqwBA7odaHyT+YQNhtdpJZwYCFoxB/Dq22oeOZ9YCXMwjhLytKpo7JJMnKdJ/ve7N12zzfQ==", + "hasInstallScript": true, + "dependencies": { + "is-electron": "^2.2.0", + "make-dir": "^1.3.0", + "node-forge": "^1.2.1", + "split": "^1.0.1" + } + }, + "node_modules/win-export-certificate-and-key": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/win-export-certificate-and-key/-/win-export-certificate-and-key-1.1.1.tgz", + "integrity": "sha512-wvF1DKlbt/PLOSdnKzIqv0Ipj+87n2VYOJFbkqBoN7l3l244reT7Lf6+Dnu86bYVWoVpq3ZZG417OLNHFnkP6A==", + "hasInstallScript": true, + "optional": true, + "os": [ + "win32" + ], + "dependencies": { + "bindings": "^1.5.0", + "node-addon-api": "^4.3.0", + "node-forge": "^1.2.1" + } + }, "node_modules/wordwrapjs": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/wordwrapjs/-/wordwrapjs-5.1.0.tgz", diff --git a/binary/package.json b/binary/package.json index d821cd3fe..eed56f17c 100644 --- a/binary/package.json +++ b/binary/package.json @@ -41,9 +41,12 @@ "commander": "^12.0.0", "core": "file:../core", "follow-redirects": "^1.15.5", + "mac-ca": "^2.0.3", "ncp": "^2.0.0", "posthog-node": "^3.6.3", + "system-ca": "^1.0.2", "uuid": "^9.0.1", - "vectordb": "^0.4.10" + "vectordb": "^0.4.10", + "win-ca": "^3.5.1" } } diff --git a/binary/src/ca.ts b/binary/src/ca.ts new file mode 100644 index 000000000..9767c556b --- /dev/null +++ b/binary/src/ca.ts @@ -0,0 +1,23 @@ +import { globalAgent } from "https"; +import { systemCertsAsync } from "system-ca"; + +export async function setupCa() { + try { + switch (process.platform) { + case "darwin": + // https://www.npmjs.com/package/mac-ca#usage + require("mac-ca").addToGlobalAgent(); + break; + case "win32": + // https://www.npmjs.com/package/win-ca#caveats + require("win-ca").inject("+"); + break; + default: + // https://www.npmjs.com/package/system-ca + globalAgent.options.ca = await systemCertsAsync(); + break; + } + } catch (e) { + console.warn("Failed to setup CA: ", e); + } +} diff --git a/binary/src/index.ts b/binary/src/index.ts index 4f6c8448e..de600d15f 100644 --- a/binary/src/index.ts +++ b/binary/src/index.ts @@ -3,6 +3,7 @@ import { Command } from "commander"; import { getCoreLogsPath } from "core/util/paths"; import fs from "fs"; import { IpcIde } from "./IpcIde"; +import { setupCa } from "./ca"; import { Core } from "./core"; import { IpcMessenger } from "./messenger"; @@ -18,6 +19,8 @@ program.action(() => { // const ide = new FileSystemIde(); const core = new Core(messenger, ide); + setupCa(); + // setTimeout(() => { // messenger.mock({ // messageId: "2fe7823c-10bd-4771-abb5-781f520039ec", diff --git a/core/util/fetchWithOptions.ts b/core/util/fetchWithOptions.ts index 83f146d86..7a1e7734b 100644 --- a/core/util/fetchWithOptions.ts +++ b/core/util/fetchWithOptions.ts @@ -1,6 +1,7 @@ import { http, https } from "follow-redirects"; import * as fs from "fs"; import { HttpProxyAgent } from "http-proxy-agent"; +import { globalAgent } from "https"; import { HttpsProxyAgent } from "https-proxy-agent"; import fetch, { RequestInit, Response } from "node-fetch"; import tls from "tls"; @@ -13,7 +14,15 @@ export function fetchwithRequestOptions( ): Promise { const TIMEOUT = 7200; // 7200 seconds = 2 hours - const ca = [...tls.rootCertificates]; + let globalCerts: string[] = []; + if (process.env.IS_BINARY) { + if (Array.isArray(globalAgent.options.ca)) { + globalCerts = [...globalAgent.options.ca.map((cert) => cert.toString())]; + } else if (typeof globalAgent.options.ca !== "undefined") { + globalCerts.push(globalAgent.options.ca.toString()); + } + } + const ca = Array.from(new Set(...tls.rootCertificates, ...globalCerts)); const customCerts = typeof requestOptions?.caBundlePath === "string" ? [requestOptions?.caBundlePath]