Skip to content

Commit 50913b1

Browse files
sam-githubaddaleax
authored andcommitted
src: whitelist v8 options with '_' or '-'
V8 options allow either '_' or '-' to be used in options as a seperator, such as "--abort-on_uncaught-exception". Allow these case variations when used with NODE_OPTIONS. PR-URL: #14093 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b12b8c2 commit 50913b1

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

doc/api/cli.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ Node options that are allowed are:
466466

467467
V8 options that are allowed are:
468468
- `--abort-on-uncaught-exception`
469-
- `--max_old_space_size`
469+
- `--max-old-space-size`
470470

471471
### `NODE_PENDING_DEPRECATION=1`
472472
<!-- YAML

src/node.cc

+26-7
Original file line numberDiff line numberDiff line change
@@ -3729,15 +3729,34 @@ static void PrintHelp() {
37293729
}
37303730

37313731

3732+
static bool ArgIsAllowed(const char* arg, const char* allowed) {
3733+
for (; *arg && *allowed; arg++, allowed++) {
3734+
// Like normal strcmp(), except that a '_' in `allowed` matches either a '-'
3735+
// or '_' in `arg`.
3736+
if (*allowed == '_') {
3737+
if (!(*arg == '_' || *arg == '-'))
3738+
return false;
3739+
} else {
3740+
if (*arg != *allowed)
3741+
return false;
3742+
}
3743+
}
3744+
3745+
// "--some-arg=val" is allowed for "--some-arg"
3746+
if (*arg == '=')
3747+
return true;
3748+
3749+
// Both must be null, or one string is just a prefix of the other, not a
3750+
// match.
3751+
return !*arg && !*allowed;
3752+
}
3753+
3754+
37323755
static void CheckIfAllowedInEnv(const char* exe, bool is_env,
37333756
const char* arg) {
37343757
if (!is_env)
37353758
return;
37363759

3737-
// Find the arg prefix when its --some_arg=val
3738-
const char* eq = strchr(arg, '=');
3739-
size_t arglen = eq ? eq - arg : strlen(arg);
3740-
37413760
static const char* whitelist[] = {
37423761
// Node options, sorted in `node --help` order for ease of comparison.
37433762
"--require", "-r",
@@ -3765,14 +3784,14 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
37653784
"--openssl-config",
37663785
"--icu-data-dir",
37673786

3768-
// V8 options
3769-
"--abort-on-uncaught-exception",
3787+
// V8 options (define with '_', which allows '-' or '_')
3788+
"--abort_on_uncaught_exception",
37703789
"--max_old_space_size",
37713790
};
37723791

37733792
for (unsigned i = 0; i < arraysize(whitelist); i++) {
37743793
const char* allowed = whitelist[i];
3775-
if (strlen(allowed) == arglen && strncmp(allowed, arg, arglen) == 0)
3794+
if (ArgIsAllowed(arg, allowed))
37763795
return;
37773796
}
37783797

test/parallel/test-cli-node-options.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ disallow('--interactive');
2626
disallow('-i');
2727
disallow('--v8-options');
2828
disallow('--');
29+
disallow('--no_warnings'); // Node options don't allow '_' instead of '-'.
2930

3031
function disallow(opt) {
3132
const options = {env: {NODE_OPTIONS: opt}};
@@ -40,7 +41,6 @@ function disallow(opt) {
4041

4142
const printA = require.resolve('../fixtures/printA.js');
4243

43-
expect('--abort-on-uncaught-exception', 'B\n');
4444
expect(`-r ${printA}`, 'A\nB\n');
4545
expect('--no-deprecation', 'B\n');
4646
expect('--no-warnings', 'B\n');
@@ -59,8 +59,13 @@ if (common.hasCrypto) {
5959
expect('--openssl-config=_ossl_cfg', 'B\n');
6060
}
6161

62-
// V8 options
62+
// V8 options
63+
expect('--abort-on-uncaught-exception', 'B\n');
64+
expect('--abort_on_uncaught_exception', 'B\n');
65+
expect('--abort_on-uncaught_exception', 'B\n');
6366
expect('--max_old_space_size=0', 'B\n');
67+
expect('--max-old_space-size=0', 'B\n');
68+
expect('--max-old-space-size=0', 'B\n');
6469

6570
function expect(opt, want) {
6671
const printB = require.resolve('../fixtures/printB.js');

0 commit comments

Comments
 (0)