Skip to content

Commit 98ff38a

Browse files
committed
rpc: Perform HTTP user:pass split once in RPCAuthorized
1 parent 879a17b commit 98ff38a

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/httprpc.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,8 @@ static void JSONErrorReply(HTTPRequest* req, UniValue objError, const JSONRPCReq
9999

100100
//This function checks username and password against -rpcauth
101101
//entries from config file.
102-
static bool CheckUserAuthorized(std::string_view user_pass)
102+
static bool CheckUserAuthorized(std::string_view user, std::string_view pass)
103103
{
104-
if (user_pass.find(':') == std::string::npos) {
105-
return false;
106-
}
107-
std::string_view user = user_pass.substr(0, user_pass.find(':'));
108-
std::string_view pass = user_pass.substr(user_pass.find(':') + 1);
109-
110104
for (const auto& fields : g_rpcauth) {
111105
if (!TimingResistantEqual(std::string_view(fields[0]), user)) {
112106
continue;
@@ -136,10 +130,14 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
136130
if (!userpass_data) return false;
137131
strUserPass.assign(userpass_data->begin(), userpass_data->end());
138132

139-
if (strUserPass.find(':') != std::string::npos)
140-
strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(':'));
141-
142-
return CheckUserAuthorized(strUserPass);
133+
size_t colon_pos = strUserPass.find(':');
134+
if (colon_pos == std::string::npos) {
135+
return false; // Invalid basic auth.
136+
}
137+
std::string user = strUserPass.substr(0, colon_pos);
138+
std::string pass = strUserPass.substr(colon_pos + 1);
139+
strAuthUsernameOut = user;
140+
return CheckUserAuthorized(user, pass);
143141
}
144142

145143
static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)

0 commit comments

Comments
 (0)