mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Replace buggy mac-ca dep with own injectMacCA()
Signed-off-by: Hung-Han (Henry) Chen <1474479+chenhunghan@users.noreply.github.com>
This commit is contained in:
parent
e510ec3880
commit
08fa0c6d63
31
src/common/system-ca.test.ts
Normal file
31
src/common/system-ca.test.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import https from "https";
|
||||||
|
import { injectMacCA } from "./system-ca";
|
||||||
|
import { dependencies, devDependencies } from "../../package.json";
|
||||||
|
|
||||||
|
describe("injectMacCA()", () => {
|
||||||
|
|
||||||
|
// for reset https.globalAgent.options.ca after testing
|
||||||
|
let _ca: string | Buffer | (string | Buffer)[];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
_ca = https.globalAgent.options.ca;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
https.globalAgent.options.ca = _ca;
|
||||||
|
});
|
||||||
|
|
||||||
|
const deps = { ...dependencies, ...devDependencies };
|
||||||
|
|
||||||
|
// skip the test if mac-ca is not installed
|
||||||
|
(deps["mac-ca"] ? it: it.skip)("should inject the same ca as mac-ca", async () => {
|
||||||
|
injectMacCA();
|
||||||
|
const injected = https.globalAgent.options.ca;
|
||||||
|
|
||||||
|
await import("mac-ca");
|
||||||
|
const injectedByMacCA = https.globalAgent.options.ca;
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
expect(new Set(injected)).toEqual(new Set(injectedByMacCA));
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,16 +1,28 @@
|
|||||||
import { isMac, isWindows } from "./vars";
|
import { isMac, isWindows } from "./vars";
|
||||||
import winca from "win-ca";
|
import winca from "win-ca";
|
||||||
import macca from "mac-ca";
|
import { spawnSync } from "child_process";
|
||||||
import logger from "../main/logger";
|
import https from "https";
|
||||||
|
|
||||||
if (isMac) {
|
const injectMacCA = () => {
|
||||||
for (const crt of macca.all()) {
|
// inspired mac-ca (abandoned by author)
|
||||||
const attributes = crt.issuer?.attributes?.map((a: any) => `${a.name}=${a.value}`);
|
// https://github.com/jfromaniello/mac-ca
|
||||||
|
const args = ["find-certificate", "-a", "-p"];
|
||||||
|
const splitPattern = /(?=-----BEGIN\sCERTIFICATE-----)/g;
|
||||||
|
const systemRootCertsPath = "/System/Library/Keychains/SystemRootCertificates.keychain";
|
||||||
|
const trusted = spawnSync("/usr/bin/security", args).stdout.toString().split(splitPattern);
|
||||||
|
const rootCerts = spawnSync("/usr/bin/security", args.concat(systemRootCertsPath)).stdout.toString().split(splitPattern);
|
||||||
|
const certs = [...new Set([...trusted, ...rootCerts])];
|
||||||
|
|
||||||
logger.debug(`Using host CA: ${attributes.join(",")}`);
|
for (const cert of certs) {
|
||||||
|
if (Array.isArray(https.globalAgent.options.ca)) {
|
||||||
|
!https.globalAgent.options.ca.includes(cert) && https.globalAgent.options.ca.push(cert);
|
||||||
|
} else {
|
||||||
|
https.globalAgent.options.ca = [cert];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
if (isWindows) {
|
isMac && injectMacCA();
|
||||||
winca.inject("+"); // see: https://github.com/ukoloff/win-ca#caveats
|
isWindows && winca.inject("+"); // see: https://github.com/ukoloff/win-ca#caveats
|
||||||
}
|
|
||||||
|
export { injectMacCA };
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user