Skip to content

Commit 162dc78

Browse files
fanquakeknst
authored andcommitted
Merge bitcoin#25043: Reject invalid rpcauth formats
fa12706 Reject invalid rpcauth formats (MacroFake) Pull request description: This was added in commit 438ee59, but I couldn't determine if it was intentional. One reason to accept `foo:bar:baz` over `foo:bar$baz` is that `$` may be eaten by the shell. Though, I don't think many users pass `rpcauth` via the shell. Also it should be easy to avoid by passing `'-rpcauth=foo:bar$baz'` or `"-rpcauth=foo:bar\$baz"`. Can be tested with the added test. ACKs for top commit: pk-b2: ACK fa12706 Tree-SHA512: 9998cbb295c79f7b0342bf86e1d3e5b5ab90851c627662ad6495b699a65a9035998173cf1debfd94325387faba184de683407b609fe86acdd8f6749157644441
1 parent cb45d8c commit 162dc78

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/httprpc.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
#include <httprpc.h>
66

7-
#include <chainparams.h>
87
#include <crypto/hmac_sha256.h>
98
#include <httpserver.h>
109
#include <rpc/protocol.h>
1110
#include <rpc/server.h>
1211
#include <util/strencodings.h>
1312
#include <util/string.h>
1413
#include <util/system.h>
15-
#include <util/translation.h>
1614
#include <walletinitinterface.h>
1715

1816
#include <algorithm>
@@ -21,6 +19,7 @@
2119
#include <memory>
2220
#include <set>
2321
#include <string>
22+
#include <vector>
2423

2524
/** WWW-Authenticate to present with 401 Unauthorized response */
2625
static const char* WWW_AUTH_HEADER_DATA = "Basic realm=\"jsonrpc\"";
@@ -288,12 +287,14 @@ static bool InitRPCAuthentication()
288287
LogPrintf("Config options rpcuser and rpcpassword will soon be deprecated. Locally-run instances may remove rpcuser to use cookie-based auth, or may be replaced with rpcauth. Please see share/rpcauth for rpcauth auth generation.\n");
289288
strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + gArgs.GetArg("-rpcpassword", "");
290289
}
291-
if (gArgs.GetArg("-rpcauth","") != "")
292-
{
290+
if (gArgs.GetArg("-rpcauth", "") != "") {
293291
LogPrintf("Using rpcauth authentication.\n");
294292
for (const std::string& rpcauth : gArgs.GetArgs("-rpcauth")) {
295-
std::vector<std::string> fields = SplitString(rpcauth, ":$");
296-
if (fields.size() == 3) {
293+
std::vector<std::string> fields{SplitString(rpcauth, ':')};
294+
const std::vector<std::string> salt_hmac{SplitString(fields.back(), '$')};
295+
if (fields.size() == 2 && salt_hmac.size() == 2) {
296+
fields.pop_back();
297+
fields.insert(fields.end(), salt_hmac.begin(), salt_hmac.end());
297298
g_rpcauth.push_back(fields);
298299
} else {
299300
LogPrintf("Invalid -rpcauth argument.\n");

test/functional/rpc_users.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def run_test(self):
106106
self.stop_node(0)
107107
self.nodes[0].assert_start_raises_init_error(expected_msg=init_error, extra_args=['-rpcauth=foo'])
108108
self.nodes[0].assert_start_raises_init_error(expected_msg=init_error, extra_args=['-rpcauth=foo:bar'])
109+
self.nodes[0].assert_start_raises_init_error(expected_msg=init_error, extra_args=['-rpcauth=foo:bar:baz'])
110+
self.nodes[0].assert_start_raises_init_error(expected_msg=init_error, extra_args=['-rpcauth=foo$bar:baz'])
111+
self.nodes[0].assert_start_raises_init_error(expected_msg=init_error, extra_args=['-rpcauth=foo$bar$baz'])
109112

110113
self.log.info('Check that failure to write cookie file will abort the node gracefully')
111114
cookie_file = os.path.join(get_datadir_path(self.options.tmpdir, 0), self.chain, '.cookie.tmp')

0 commit comments

Comments
 (0)