From 27fb128c05f7e796e5b6dce6cc5b2bad029e45f5 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 24 Nov 2022 11:28:33 -0800 Subject: [PATCH] Handle globalAgent having a single non-array CA (#6628) * Handle globalAgent having a single non-array CA Signed-off-by: Sebastian Malton * Rewrite ternary as IIFE Signed-off-by: Sebastian Malton Signed-off-by: Sebastian Malton --- .../inject-system-cas.injectable.ts | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/common/certificate-authorities/inject-system-cas.injectable.ts b/src/common/certificate-authorities/inject-system-cas.injectable.ts index b6223fa38e..f75dcd6c70 100644 --- a/src/common/certificate-authorities/inject-system-cas.injectable.ts +++ b/src/common/certificate-authorities/inject-system-cas.injectable.ts @@ -22,15 +22,36 @@ const injectSystemCAsInjectable = getInjectable({ const requestSystemCAs = di.inject(requestSystemCAsInjectionToken); return async () => { - for (const cert of await requestSystemCAs()) { - if (isCertActive(cert)) { - if (Array.isArray(globalAgent.options.ca) && !globalAgent.options.ca.includes(cert)) { - globalAgent.options.ca.push(cert); - } else { - globalAgent.options.ca = [cert]; - } + const certs = await requestSystemCAs(); + + if (certs.length === 0) { + // Leave the global option alone + return; + } + + const cas = (() => { + if (Array.isArray(globalAgent.options.ca)) { + return globalAgent.options.ca; + } + + if (globalAgent.options.ca) { + return [globalAgent.options.ca]; + } + + return []; + })(); + + for (const cert of certs) { + if (!isCertActive(cert)) { + continue; + } + + if (!cas.includes(cert)) { + cas.push(cert); } } + + globalAgent.options.ca = cas; }; }, });