Skip to content

Commit 97c7f73

Browse files
committed
Merge #8788: [RPC] Give RPC commands more information about the RPC request
e7156ad [RPC] pass HTTP basic authentication username to the JSONRequest object (Jonas Schnelli) 69d1c25 [RPC] Give RPC commands more information about the RPC request (Jonas Schnelli) 23c32a9 rpc: Change JSONRPCRequest to JSONRPCRequestObj (Wladimir J. van der Laan)
2 parents d736a6e + e7156ad commit 97c7f73

16 files changed

+570
-554
lines changed

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ UniValue CallRPC(const string& strMethod, const UniValue& params)
223223
evhttp_add_header(output_headers, "Authorization", (std::string("Basic ") + EncodeBase64(strRPCUserColonPass)).c_str());
224224

225225
// Attach request data
226-
std::string strRequest = JSONRPCRequest(strMethod, params, 1);
226+
std::string strRequest = JSONRPCRequestObj(strMethod, params, 1).write() + "\n";
227227
struct evbuffer * output_buffer = evhttp_request_get_output_buffer(req);
228228
assert(output_buffer);
229229
evbuffer_add(output_buffer, strRequest.data(), strRequest.size());

src/httprpc.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static bool multiUserAuthorized(std::string strUserPass)
127127
return false;
128128
}
129129

130-
static bool RPCAuthorized(const std::string& strAuth)
130+
static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUsernameOut)
131131
{
132132
if (strRPCUserColonPass.empty()) // Belt-and-suspenders measure if InitRPCAuthentication was not called
133133
return false;
@@ -136,7 +136,10 @@ static bool RPCAuthorized(const std::string& strAuth)
136136
std::string strUserPass64 = strAuth.substr(6);
137137
boost::trim(strUserPass64);
138138
std::string strUserPass = DecodeBase64(strUserPass64);
139-
139+
140+
if (strUserPass.find(":") != std::string::npos)
141+
strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(":"));
142+
140143
//Check if authorized under single-user field
141144
if (TimingResistantEqual(strUserPass, strRPCUserColonPass)) {
142145
return true;
@@ -159,7 +162,8 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
159162
return false;
160163
}
161164

162-
if (!RPCAuthorized(authHeader.second)) {
165+
JSONRPCRequest jreq;
166+
if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
163167
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", req->GetPeer().ToString());
164168

165169
/* Deter brute-forcing
@@ -172,19 +176,21 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
172176
return false;
173177
}
174178

175-
JSONRequest jreq;
176179
try {
177180
// Parse request
178181
UniValue valRequest;
179182
if (!valRequest.read(req->ReadBody()))
180183
throw JSONRPCError(RPC_PARSE_ERROR, "Parse error");
181184

185+
// Set the URI
186+
jreq.URI = req->GetURI();
187+
182188
std::string strReply;
183189
// singleton request
184190
if (valRequest.isObject()) {
185191
jreq.parse(valRequest);
186192

187-
UniValue result = tableRPC.execute(jreq.strMethod, jreq.params);
193+
UniValue result = tableRPC.execute(jreq);
188194

189195
// Send reply
190196
strReply = JSONRPCReply(result, NullUniValue, jreq.id);

src/qt/rpcconsole.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,10 @@ bool RPCConsole::RPCExecuteCommandLine(std::string &strResult, const std::string
246246
std::string strPrint;
247247
// Convert argument list to JSON objects in method-dependent way,
248248
// and pass it along with the method name to the dispatcher.
249-
lastResult = tableRPC.execute(stack.back()[0], RPCConvertValues(stack.back()[0], std::vector<std::string>(stack.back().begin() + 1, stack.back().end())));
249+
JSONRPCRequest req;
250+
req.params = RPCConvertValues(stack.back()[0], std::vector<std::string>(stack.back().begin() + 1, stack.back().end()));
251+
req.strMethod = stack.back()[0];
252+
lastResult = tableRPC.execute(req);
250253

251254
state = STATE_COMMAND_EXECUTED;
252255
curarg.clear();

src/rest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ static bool rest_block_notxdetails(HTTPRequest* req, const std::string& strURIPa
274274
}
275275

276276
// A bit of a hack - dependency on a function defined in rpc/blockchain.cpp
277-
UniValue getblockchaininfo(const UniValue& params, bool fHelp);
277+
UniValue getblockchaininfo(const JSONRPCRequest& request);
278278

279279
static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
280280
{
@@ -285,8 +285,9 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
285285

286286
switch (rf) {
287287
case RF_JSON: {
288-
UniValue rpcParams(UniValue::VARR);
289-
UniValue chainInfoObject = getblockchaininfo(rpcParams, false);
288+
JSONRPCRequest jsonRequest;
289+
jsonRequest.params = UniValue(UniValue::VARR);
290+
UniValue chainInfoObject = getblockchaininfo(jsonRequest);
290291
string strJSON = chainInfoObject.write() + "\n";
291292
req->WriteHeader("Content-Type", "application/json");
292293
req->WriteReply(HTTP_OK, strJSON);

0 commit comments

Comments
 (0)