Skip to content

Commit 900d8f6

Browse files
committed
util: Disallow network-qualified command line options
Previously these were allowed but ignored.
1 parent 6677be6 commit 900d8f6

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

doc/release-notes.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ Wallet
104104
Low-level changes
105105
=================
106106

107+
Command line
108+
------------
109+
110+
Command line options prefixed with main/test/regtest network names like
111+
`-main.port=8333` `-test.server=1` previously were allowed but ignored. Now
112+
they trigger "Invalid parameter" errors on startup.
113+
107114
Tests
108115
-----
109116

src/test/util_tests.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,27 @@ BOOST_AUTO_TEST_CASE(util_ParseParameters)
340340
BOOST_CHECK(testArgs.GetArgs("-ccc").size() == 2);
341341
}
342342

343+
BOOST_AUTO_TEST_CASE(util_ParseInvalidParameters)
344+
{
345+
TestArgsManager test;
346+
test.SetupArgs({{"-registered", ArgsManager::ALLOW_ANY}});
347+
348+
const char* argv[] = {"ignored", "-registered"};
349+
std::string error;
350+
BOOST_CHECK(test.ParseParameters(2, (char**)argv, error));
351+
BOOST_CHECK_EQUAL(error, "");
352+
353+
argv[1] = "-unregistered";
354+
BOOST_CHECK(!test.ParseParameters(2, (char**)argv, error));
355+
BOOST_CHECK_EQUAL(error, "Invalid parameter -unregistered");
356+
357+
// Make sure registered parameters prefixed with a chain name trigger errors.
358+
// (Previously, they were accepted and ignored.)
359+
argv[1] = "-test.registered";
360+
BOOST_CHECK(!test.ParseParameters(2, (char**)argv, error));
361+
BOOST_CHECK_EQUAL(error, "Invalid parameter -test.registered");
362+
}
363+
343364
static void TestParse(const std::string& str, bool expected_bool, int64_t expected_int)
344365
{
345366
TestArgsManager test;
@@ -835,7 +856,8 @@ struct ArgsMergeTestingSetup : public BasicTestingSetup {
835856
void ForEachMergeSetup(Fn&& fn)
836857
{
837858
ActionList arg_actions = {};
838-
ForEachNoDup(arg_actions, SET, SECTION_NEGATE, [&] {
859+
// command_line_options do not have sections. Only iterate over SET and NEGATE
860+
ForEachNoDup(arg_actions, SET, NEGATE, [&] {
839861
ActionList conf_actions = {};
840862
ForEachNoDup(conf_actions, SET, SECTION_NEGATE, [&] {
841863
for (bool soft_set : {false, true}) {
@@ -995,7 +1017,7 @@ BOOST_FIXTURE_TEST_CASE(util_ArgsMerge, ArgsMergeTestingSetup)
9951017
// Results file is formatted like:
9961018
//
9971019
// <input> || <IsArgSet/IsArgNegated/GetArg output> | <GetArgs output> | <GetUnsuitable output>
998-
BOOST_CHECK_EQUAL(out_sha_hex, "b835eef5977d69114eb039a976201f8c7121f34fe2b7ea2b73cafb516e5c9dc8");
1020+
BOOST_CHECK_EQUAL(out_sha_hex, "8fd4877bb8bf337badca950ede6c917441901962f160e52514e06a60dea46cde");
9991021
}
10001022

10011023
// Similar test as above, but for ArgsManager::GetChainName function.

src/util/system.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -311,21 +311,18 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
311311
std::string section;
312312
util::SettingsValue value = InterpretOption(section, key, val);
313313
Optional<unsigned int> flags = GetArgFlags('-' + key);
314-
if (flags) {
315-
if (!CheckValid(key, value, *flags, error)) {
316-
return false;
317-
}
318-
// Weird behavior preserved for backwards compatibility: command
319-
// line options with section prefixes are allowed but ignored. It
320-
// would be better if these options triggered the Invalid parameter
321-
// error below.
322-
if (section.empty()) {
323-
m_settings.command_line_options[key].push_back(value);
324-
}
325-
} else {
326-
error = strprintf("Invalid parameter -%s", key);
314+
315+
// Unknown command line options and command line options with dot
316+
// characters (which are returned from InterpretOption with nonempty
317+
// section strings) are not valid.
318+
if (!flags || !section.empty()) {
319+
error = strprintf("Invalid parameter %s", argv[i]);
327320
return false;
328321
}
322+
323+
if (!CheckValid(key, value, *flags, error)) return false;
324+
325+
m_settings.command_line_options[key].push_back(value);
329326
}
330327

331328
// we do not allow -includeconf from command line

test/functional/feature_config_args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_config_file_parser(self):
2323
conf.write('includeconf={}\n'.format(inc_conf_file_path))
2424

2525
self.nodes[0].assert_start_raises_init_error(
26-
expected_msg='Error: Error parsing command line arguments: Invalid parameter -dash_cli',
26+
expected_msg='Error: Error parsing command line arguments: Invalid parameter -dash_cli=1',
2727
extra_args=['-dash_cli=1'],
2828
)
2929
with open(inc_conf_file_path, 'w', encoding='utf-8') as conf:

0 commit comments

Comments
 (0)