Skip to content

Commit 88c18ce

Browse files
committed
fix(@angular/build): support NODE_EXTRA_CA_CERTS in SSR SSL plugin
This commit adds support for the 'NODE_EXTRA_CA_CERTS' environment variable when configuring the global dispatcher for the SSR SSL plugin. This ensures that custom CA certificates specified via this environment variable are correctly trusted. Closes #31983 (cherry picked from commit 6d21220)
1 parent c3d70bc commit 88c18ce

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

packages/angular/build/src/tools/vite/plugins/ssr-ssl-plugin.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { rootCertificates } from 'node:tls';
9+
import { readFile } from 'node:fs/promises';
10+
import { getCACertificates, rootCertificates, setDefaultCACertificates } from 'node:tls';
1011
import type { Plugin } from 'vite';
1112

1213
export function createAngularServerSideSSLPlugin(): Plugin {
@@ -35,17 +36,30 @@ export function createAngularServerSideSSLPlugin(): Plugin {
3536
httpServer.ALPNProtocols = ['http/1.1'];
3637
}
3738

38-
// TODO(alanagius): Replace `undici` with `tls.setDefaultCACertificates` once we only support Node.js 22.18.0+ and 24.5.0+.
39-
// See: https://nodejs.org/api/tls.html#tlssetdefaultcacertificatescerts
39+
const { cert } = https;
40+
const additionalCerts = Array.isArray(cert) ? cert : [cert];
41+
42+
// TODO(alanagius): Remove the `if` check once we only support Node.js 22.18.0+ and 24.5.0+.
43+
if (getCACertificates && setDefaultCACertificates) {
44+
const currentCerts = getCACertificates('default');
45+
setDefaultCACertificates([...currentCerts, ...additionalCerts]);
46+
47+
return;
48+
}
49+
50+
// TODO(alanagius): Remove the below and `undici` dependency once we only support Node.js 22.18.0+ and 24.5.0+.
4051
const { getGlobalDispatcher, setGlobalDispatcher, Agent } = await import('undici');
4152
const originalDispatcher = getGlobalDispatcher();
42-
const { cert } = https;
43-
const certificates = Array.isArray(cert) ? cert : [cert];
53+
const ca = [...rootCertificates, ...additionalCerts];
54+
const extraNodeCerts = process.env['NODE_EXTRA_CA_CERTS'];
55+
if (extraNodeCerts) {
56+
ca.push(await readFile(extraNodeCerts));
57+
}
4458

4559
setGlobalDispatcher(
4660
new Agent({
4761
connect: {
48-
ca: [...rootCertificates, ...certificates],
62+
ca,
4963
},
5064
}),
5165
);

0 commit comments

Comments
 (0)