Skip to content

Commit 8a56f79

Browse files
committed
Merge bitcoin#17482: util: Disallow network-qualified command line options
900d8f6 util: Disallow network-qualified command line options (Russell Yanofsky) Pull request description: Previously these were allowed but ignored. This change implements one of the settings simplifications listed in bitcoin#17508. Change includes release notes. ACKs for top commit: laanwj: ACK 900d8f6 Tree-SHA512: ab020a16a86c1e8ec709fbf798d533879d32c565eceeb7eb785c33042c49c6b4d1108c5453d8166e4a2abffc2c8802fbb6d3b895e0ddeefa8f274fd647e3c8ad
2 parents 712b7d9 + 900d8f6 commit 8a56f79

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

doc/release-notes.md

+7
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

+24-2
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

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

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

test/functional/feature_config_args.py

+1-1
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)