@@ -99,14 +99,8 @@ static void JSONErrorReply(HTTPRequest* req, UniValue objError, const JSONRPCReq
99
99
100
100
// This function checks username and password against -rpcauth
101
101
// 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 )
103
103
{
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
-
110
104
for (const auto & fields : g_rpcauth) {
111
105
if (!TimingResistantEqual (std::string_view (fields[0 ]), user)) {
112
106
continue ;
@@ -136,10 +130,14 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
136
130
if (!userpass_data) return false ;
137
131
strUserPass.assign (userpass_data->begin (), userpass_data->end ());
138
132
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);
143
141
}
144
142
145
143
static bool HTTPReq_JSONRPC (const std::any& context, HTTPRequest* req)
0 commit comments