Skip to content

Commit 608c277

Browse files
committed
src: add validation for duplicate options in node and namespace options
1 parent 8f6b9ad commit 608c277

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

src/node_config_file.cc

+16
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ ParseResult ConfigReader::ParseNodeOptions(
5252
std::string prefix = "--";
5353
auto it = env_options_map.find(prefix.append(key));
5454
if (it != env_options_map.end()) {
55+
// If the option has already been set in the namespace options
56+
// we return an invalid content error
57+
if (unique_namespace_options_.contains(it->first)) {
58+
FPrintF(stderr,
59+
"Option %s is already set in namespace options\n",
60+
it->first.c_str());
61+
return ParseResult::InvalidContent;
62+
}
5563
ParseResult result = ProcessOptionValue(key,
5664
it->first,
5765
ondemand_value,
@@ -283,6 +291,14 @@ ParseResult ConfigReader::ParseNamespaceOptions(
283291
std::string prefix = "--";
284292
auto it = options_map.find(prefix.append(key));
285293
if (it != options_map.end()) {
294+
// If the option has already been set in the nodeOptions
295+
// we return an invalid content error
296+
if (unique_node_options_.contains(it->first)) {
297+
FPrintF(stderr,
298+
"Option %s is already set in nodeOptions\n",
299+
it->first.c_str());
300+
return ParseResult::InvalidContent;
301+
}
286302
ParseResult result = ProcessOptionValue(key,
287303
it->first,
288304
ondemand_value,

test/parallel/test-config-file.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -401,29 +401,29 @@ describe('namespaced options', () => {
401401
strictEqual(result.code, 0);
402402
});
403403

404-
it('should override a non-namespaced option with a namespaced option', async () => {
404+
it('should throw an error if a namespaced option has already been set in node options', async () => {
405405
const result = await spawnPromisified(process.execPath, [
406406
'--no-warnings',
407407
'--expose-internals',
408408
'--experimental-config-file',
409-
fixtures.path('rc/override-namespace.json'),
409+
fixtures.path('rc/override-node-option-with-namespace.json'),
410410
'-p', 'require("internal/options").getOptionValue("--test-isolation")',
411411
]);
412-
strictEqual(result.stderr, '');
413-
strictEqual(result.stdout, 'process\n');
414-
strictEqual(result.code, 0);
412+
match(result.stderr, /Option --test-isolation is already set in nodeOptions/);
413+
strictEqual(result.stdout, '');
414+
strictEqual(result.code, 9);
415415
});
416416

417-
it('should override a non-namespaced option with a namespaced option inverse order', async () => {
417+
it('should throw an error if a node option has already been set in a namespaced option', async () => {
418418
const result = await spawnPromisified(process.execPath, [
419419
'--no-warnings',
420420
'--expose-internals',
421421
'--experimental-config-file',
422-
fixtures.path('rc/override-namespace-inverse.json'),
422+
fixtures.path('rc/override-namespace.json'),
423423
'-p', 'require("internal/options").getOptionValue("--test-isolation")',
424424
]);
425-
strictEqual(result.stderr, '');
426-
strictEqual(result.stdout, 'process\n');
427-
strictEqual(result.code, 0);
425+
match(result.stderr, /Option --test-isolation is already set in namespace options/);
426+
strictEqual(result.stdout, '');
427+
strictEqual(result.code, 9);
428428
});
429429
});

0 commit comments

Comments
 (0)