Skip to content

Commit a2e42eb

Browse files
Maël Nisonrickyes
Maël Nison
authored andcommitted
src: allows escaping NODE_OPTIONS with backslashes
The characters specified within NODE_OPTIONS can now be escaped, which is handy especially in conjunction with `--require` (where the file path might happen to contain spaces that shouldn't cause the option to be split into two). Fixes: nodejs#12971 PR-URL: nodejs#24065 Reviewed-By: Anna Henningsen <[email protected]>
1 parent d065960 commit a2e42eb

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

doc/api/cli.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,13 @@ if they had been specified on the command line before the actual command line
642642
(so they can be overridden). Node.js will exit with an error if an option
643643
that is not allowed in the environment is used, such as `-p` or a script file.
644644

645+
In case an option value happens to contain a space (for example a path listed in
646+
`--require`), it must be escaped using double quotes. For example:
647+
648+
```bash
649+
--require "./my path/file.js"
650+
```
651+
645652
Node.js options that are allowed are:
646653
- `--enable-fips`
647654
- `--experimental-modules`

src/node.cc

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,23 +2610,49 @@ void Init(std::vector<std::string>* argv,
26102610

26112611
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
26122612
std::string node_options;
2613-
if (SafeGetenv("NODE_OPTIONS", &node_options)) {
2613+
2614+
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) {
26142615
std::vector<std::string> env_argv;
26152616
// [0] is expected to be the program name, fill it in from the real argv.
26162617
env_argv.push_back(argv->at(0));
26172618

2618-
// Split NODE_OPTIONS at each ' ' character.
2619-
std::string::size_type index = std::string::npos;
2620-
do {
2621-
std::string::size_type prev_index = index;
2622-
index = node_options.find(' ', index + 1);
2623-
if (index - prev_index == 1) continue;
2624-
2625-
const std::string option = node_options.substr(
2626-
prev_index + 1, index - prev_index - 1);
2627-
if (!option.empty())
2628-
env_argv.emplace_back(std::move(option));
2629-
} while (index != std::string::npos);
2619+
bool is_in_string = false;
2620+
bool will_start_new_arg = true;
2621+
for (std::string::size_type index = 0;
2622+
index < node_options.size();
2623+
++index) {
2624+
char c = node_options.at(index);
2625+
2626+
// Backslashes escape the following character
2627+
if (c == '\\' && is_in_string) {
2628+
if (index + 1 == node_options.size()) {
2629+
errors->push_back("invalid value for NODE_OPTIONS "
2630+
"(invalid escape)\n");
2631+
return 9;
2632+
} else {
2633+
c = node_options.at(++index);
2634+
}
2635+
} else if (c == ' ' && !is_in_string) {
2636+
will_start_new_arg = true;
2637+
continue;
2638+
} else if (c == '"') {
2639+
is_in_string = !is_in_string;
2640+
continue;
2641+
}
2642+
2643+
if (will_start_new_arg) {
2644+
env_argv.push_back(std::string(1, c));
2645+
will_start_new_arg = false;
2646+
} else {
2647+
env_argv.back() += c;
2648+
}
2649+
}
2650+
2651+
if (is_in_string) {
2652+
errors->push_back("invalid value for NODE_OPTIONS "
2653+
"(unterminated string)\n");
2654+
return 9;
2655+
}
26302656

26312657

26322658
ProcessArgv(&env_argv, nullptr, true);

test/fixtures/print A.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('A')

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ tmpdir.refresh();
1515
process.chdir(tmpdir.path);
1616

1717
const printA = require.resolve('../fixtures/printA.js');
18+
const printSpaceA = require.resolve('../fixtures/print A.js');
19+
20+
expect(` -r ${printA} `, 'A\nB\n');
1821
expect(`-r ${printA}`, 'A\nB\n');
22+
expect(`-r ${JSON.stringify(printA)}`, 'A\nB\n');
23+
expect(`-r ${JSON.stringify(printSpaceA)}`, 'A\nB\n');
1924
expect(`-r ${printA} -r ${printA}`, 'A\nB\n');
2025
expect('--no-deprecation', 'B\n');
2126
expect('--no-warnings', 'B\n');

0 commit comments

Comments
 (0)