Skip to content

Commit f659e49

Browse files
sam-githubMylesBorins
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 <riclau@uk.ibm.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 79171e0 commit f659e49

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

doc/api/cli.md

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

373373
V8 options that are allowed are:
374374
- `--abort-on-uncaught-exception`
375-
- `--max_old_space_size`
375+
- `--max-old-space-size`
376376

377377

378378
### `NODE_REPL_HISTORY=file`

src/node.cc

+26-7
Original file line numberDiff line numberDiff line change
@@ -3786,15 +3786,34 @@ static void PrintHelp() {
37863786
}
37873787

37883788

3789+
static bool ArgIsAllowed(const char* arg, const char* allowed) {
3790+
for (; *arg && *allowed; arg++, allowed++) {
3791+
// Like normal strcmp(), except that a '_' in `allowed` matches either a '-'
3792+
// or '_' in `arg`.
3793+
if (*allowed == '_') {
3794+
if (!(*arg == '_' || *arg == '-'))
3795+
return false;
3796+
} else {
3797+
if (*arg != *allowed)
3798+
return false;
3799+
}
3800+
}
3801+
3802+
// "--some-arg=val" is allowed for "--some-arg"
3803+
if (*arg == '=')
3804+
return true;
3805+
3806+
// Both must be null, or one string is just a prefix of the other, not a
3807+
// match.
3808+
return !*arg && !*allowed;
3809+
}
3810+
3811+
37893812
static void CheckIfAllowedInEnv(const char* exe, bool is_env,
37903813
const char* arg) {
37913814
if (!is_env)
37923815
return;
37933816

3794-
// Find the arg prefix when its --some_arg=val
3795-
const char* eq = strchr(arg, '=');
3796-
size_t arglen = eq ? eq - arg : strlen(arg);
3797-
37983817
static const char* whitelist[] = {
37993818
// Node options, sorted in `node --help` order for ease of comparison.
38003819
"--require", "-r",
@@ -3820,14 +3839,14 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
38203839
"--openssl-config",
38213840
"--icu-data-dir",
38223841

3823-
// V8 options
3824-
"--abort-on-uncaught-exception",
3842+
// V8 options (define with '_', which allows '-' or '_')
3843+
"--abort_on_uncaught_exception",
38253844
"--max_old_space_size",
38263845
};
38273846

38283847
for (unsigned i = 0; i < arraysize(whitelist); i++) {
38293848
const char* allowed = whitelist[i];
3830-
if (strlen(allowed) == arglen && strncmp(allowed, arg, arglen) == 0)
3849+
if (ArgIsAllowed(arg, allowed))
38313850
return;
38323851
}
38333852

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

+4
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}};
@@ -43,6 +44,7 @@ const printA = require.resolve('../fixtures/printA.js');
4344

4445
expect(`-r ${printA}`, 'A\nB\n');
4546
expect('--abort-on-uncaught-exception', 'B\n');
47+
expect('--abort_on_uncaught_exception', 'B\n');
4648
expect('--no-deprecation', 'B\n');
4749
expect('--no-warnings', 'B\n');
4850
expect('--trace-warnings', 'B\n');
@@ -61,6 +63,8 @@ expect('--icu-data-dir=_d', 'B\n');
6163

6264
// V8 options
6365
expect('--max_old_space_size=0', 'B\n');
66+
expect('--max-old_space-size=0', 'B\n');
67+
expect('--max-old-space-size=0', 'B\n');
6468

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

0 commit comments

Comments
 (0)