cli: add NODE_USE_SYSTEM_CA=1 · nodejs/node@8e2076a · GitHub
Skip to content

Commit 8e2076a

Browse files
joyeecheungaduh95
authored andcommitted
cli: add NODE_USE_SYSTEM_CA=1
Similar to how NODE_USE_ENV_PROXY complements --use-env-proxy, this complements --use-system-ca. This will allow the setting to be applied to workers individually in the future. PR-URL: #59276 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 3366e60 commit 8e2076a

5 files changed

Lines changed: 113 additions & 0 deletions

File tree

doc/api/cli.md

Lines changed: 13 additions & 0 deletions

doc/node.1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,12 @@ When set to
809809
.Ar 0 ,
810810
TLS certificate validation is disabled.
811811
.
812+
.It Ev NODE_USE_SYSTEM_CA
813+
Similar to
814+
.Fl -use-system-ca .
815+
Use the trusted CA certificates present in the system store, in addition to the certificates in the
816+
bundled Mozilla CA store and certificates from `NODE_EXTRA_CA_CERTS`.
817+
.
812818
.It Ev NODE_V8_COVERAGE Ar dir
813819
When set, Node.js writes JavaScript code coverage information to
814820
.Ar dir .

src/node.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,15 @@ static ExitCode InitializeNodeWithArgsInternal(
912912
// default value.
913913
V8::SetFlagsFromString("--rehash-snapshot");
914914

915+
#if HAVE_OPENSSL
916+
// TODO(joyeecheung): make this a per-env option and move the normalization
917+
// into HandleEnvOptions.
918+
std::string use_system_ca;
919+
if (credentials::SafeGetenv("NODE_USE_SYSTEM_CA", &use_system_ca) &&
920+
use_system_ca == "1") {
921+
per_process::cli_options->use_system_ca = true;
922+
}
923+
#endif // HAVE_OPENSSL
915924
HandleEnvOptions(per_process::cli_options->per_isolate->per_env);
916925

917926
std::string node_options;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
// This tests that NODE_USE_SYSTEM_CA environment variable works the same
3+
// as --use-system-ca flag by comparing certificate counts.
4+
5+
const common = require('../common');
6+
if (!common.hasCrypto) common.skip('missing crypto');
7+
8+
const tls = require('tls');
9+
const { spawnSyncAndExitWithoutError } = require('../common/child_process');
10+
11+
const systemCerts = tls.getCACertificates('system');
12+
if (systemCerts.length === 0) {
13+
common.skip('no system certificates available');
14+
}
15+
16+
const { child: { stdout: expectedLength } } = spawnSyncAndExitWithoutError(process.execPath, [
17+
'--use-system-ca',
18+
'-p',
19+
`tls.getCACertificates('default').length`,
20+
], {
21+
env: { ...process.env, NODE_USE_SYSTEM_CA: '0' },
22+
});
23+
24+
spawnSyncAndExitWithoutError(process.execPath, [
25+
'-p',
26+
`assert.strictEqual(tls.getCACertificates('default').length, ${expectedLength.toString()})`,
27+
], {
28+
env: { ...process.env, NODE_USE_SYSTEM_CA: '1' },
29+
});
Lines changed: 56 additions & 0 deletions

0 commit comments

Comments
 (0)