Coverage Report

Created: 2025-03-27 15:35

/root/bitcoin/src/httprpc.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2015-2022 The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <httprpc.h>
6
7
#include <common/args.h>
8
#include <crypto/hmac_sha256.h>
9
#include <httpserver.h>
10
#include <logging.h>
11
#include <netaddress.h>
12
#include <rpc/protocol.h>
13
#include <rpc/server.h>
14
#include <util/fs.h>
15
#include <util/fs_helpers.h>
16
#include <util/strencodings.h>
17
#include <util/string.h>
18
#include <walletinitinterface.h>
19
20
#include <algorithm>
21
#include <iterator>
22
#include <map>
23
#include <memory>
24
#include <optional>
25
#include <set>
26
#include <string>
27
#include <vector>
28
29
using util::SplitString;
30
using util::TrimStringView;
31
32
/** WWW-Authenticate to present with 401 Unauthorized response */
33
static const char* WWW_AUTH_HEADER_DATA = "Basic realm=\"jsonrpc\"";
34
35
/** Simple one-shot callback timer to be used by the RPC mechanism to e.g.
36
 * re-lock the wallet.
37
 */
38
class HTTPRPCTimer : public RPCTimerBase
39
{
40
public:
41
    HTTPRPCTimer(struct event_base* eventBase, std::function<void()>& func, int64_t millis) :
42
0
        ev(eventBase, false, func)
43
0
    {
44
0
        struct timeval tv;
45
0
        tv.tv_sec = millis/1000;
46
0
        tv.tv_usec = (millis%1000)*1000;
47
0
        ev.trigger(&tv);
48
0
    }
49
private:
50
    HTTPEvent ev;
51
};
52
53
class HTTPRPCTimerInterface : public RPCTimerInterface
54
{
55
public:
56
0
    explicit HTTPRPCTimerInterface(struct event_base* _base) : base(_base)
57
0
    {
58
0
    }
59
    const char* Name() override
60
0
    {
61
0
        return "HTTP";
62
0
    }
63
    RPCTimerBase* NewTimer(std::function<void()>& func, int64_t millis) override
64
0
    {
65
0
        return new HTTPRPCTimer(base, func, millis);
66
0
    }
67
private:
68
    struct event_base* base;
69
};
70
71
72
/* Pre-base64-encoded authentication token */
73
static std::string strRPCUserColonPass;
74
/* Stored RPC timer interface (for unregistration) */
75
static std::unique_ptr<HTTPRPCTimerInterface> httpRPCTimerInterface;
76
/* List of -rpcauth values */
77
static std::vector<std::vector<std::string>> g_rpcauth;
78
/* RPC Auth Whitelist */
79
static std::map<std::string, std::set<std::string>> g_rpc_whitelist;
80
static bool g_rpc_whitelist_default = false;
81
82
static void JSONErrorReply(HTTPRequest* req, UniValue objError, const JSONRPCRequest& jreq)
83
0
{
84
    // Sending HTTP errors is a legacy JSON-RPC behavior.
85
0
    Assume(jreq.m_json_version != JSONRPCVersion::V2);
86
87
    // Send error reply from json-rpc error object
88
0
    int nStatus = HTTP_INTERNAL_SERVER_ERROR;
89
0
    int code = objError.find_value("code").getInt<int>();
90
91
0
    if (code == RPC_INVALID_REQUEST)
92
0
        nStatus = HTTP_BAD_REQUEST;
93
0
    else if (code == RPC_METHOD_NOT_FOUND)
94
0
        nStatus = HTTP_NOT_FOUND;
95
96
0
    std::string strReply = JSONRPCReplyObj(NullUniValue, std::move(objError), jreq.id, jreq.m_json_version).write() + "\n";
97
98
0
    req->WriteHeader("Content-Type", "application/json");
99
0
    req->WriteReply(nStatus, strReply);
100
0
}
101
102
//This function checks username and password against -rpcauth
103
//entries from config file.
104
static bool multiUserAuthorized(std::string strUserPass)
105
0
{
106
0
    if (strUserPass.find(':') == std::string::npos) {
107
0
        return false;
108
0
    }
109
0
    std::string strUser = strUserPass.substr(0, strUserPass.find(':'));
110
0
    std::string strPass = strUserPass.substr(strUserPass.find(':') + 1);
111
112
0
    for (const auto& vFields : g_rpcauth) {
113
0
        std::string strName = vFields[0];
114
0
        if (!TimingResistantEqual(strName, strUser)) {
115
0
            continue;
116
0
        }
117
118
0
        std::string strSalt = vFields[1];
119
0
        std::string strHash = vFields[2];
120
121
0
        static const unsigned int KEY_SIZE = 32;
122
0
        unsigned char out[KEY_SIZE];
123
124
0
        CHMAC_SHA256(reinterpret_cast<const unsigned char*>(strSalt.data()), strSalt.size()).Write(reinterpret_cast<const unsigned char*>(strPass.data()), strPass.size()).Finalize(out);
125
0
        std::vector<unsigned char> hexvec(out, out+KEY_SIZE);
126
0
        std::string strHashFromPass = HexStr(hexvec);
127
128
0
        if (TimingResistantEqual(strHashFromPass, strHash)) {
129
0
            return true;
130
0
        }
131
0
    }
132
0
    return false;
133
0
}
134
135
static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUsernameOut)
136
0
{
137
0
    if (strAuth.substr(0, 6) != "Basic ")
138
0
        return false;
139
0
    std::string_view strUserPass64 = TrimStringView(std::string_view{strAuth}.substr(6));
140
0
    auto userpass_data = DecodeBase64(strUserPass64);
141
0
    std::string strUserPass;
142
0
    if (!userpass_data) return false;
143
0
    strUserPass.assign(userpass_data->begin(), userpass_data->end());
144
145
0
    if (strUserPass.find(':') != std::string::npos)
146
0
        strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(':'));
147
148
    // Check if authorized under single-user field.
149
    // (strRPCUserColonPass is empty when -norpccookiefile is specified).
150
0
    if (!strRPCUserColonPass.empty() && TimingResistantEqual(strUserPass, strRPCUserColonPass)) {
151
0
        return true;
152
0
    }
153
0
    return multiUserAuthorized(strUserPass);
154
0
}
155
156
static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
157
0
{
158
    // JSONRPC handles only POST
159
0
    if (req->GetRequestMethod() != HTTPRequest::POST) {
160
0
        req->WriteReply(HTTP_BAD_METHOD, "JSONRPC server handles only POST requests");
161
0
        return false;
162
0
    }
163
    // Check authorization
164
0
    std::pair<bool, std::string> authHeader = req->GetHeader("authorization");
165
0
    if (!authHeader.first) {
166
0
        req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA);
167
0
        req->WriteReply(HTTP_UNAUTHORIZED);
168
0
        return false;
169
0
    }
170
171
0
    JSONRPCRequest jreq;
172
0
    jreq.context = context;
173
0
    jreq.peerAddr = req->GetPeer().ToStringAddrPort();
174
0
    if (!RPCAuthorized(authHeader.second, jreq.authUser)) {
175
0
        LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", jreq.peerAddr);
176
177
        /* Deter brute-forcing
178
           If this results in a DoS the user really
179
           shouldn't have their RPC port exposed. */
180
0
        UninterruptibleSleep(std::chrono::milliseconds{250});
181
182
0
        req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA);
183
0
        req->WriteReply(HTTP_UNAUTHORIZED);
184
0
        return false;
185
0
    }
186
187
0
    try {
188
        // Parse request
189
0
        UniValue valRequest;
190
0
        if (!valRequest.read(req->ReadBody()))
191
0
            throw JSONRPCError(RPC_PARSE_ERROR, "Parse error");
192
193
        // Set the URI
194
0
        jreq.URI = req->GetURI();
195
196
0
        UniValue reply;
197
0
        bool user_has_whitelist = g_rpc_whitelist.count(jreq.authUser);
198
0
        if (!user_has_whitelist && g_rpc_whitelist_default) {
199
0
            LogPrintf("RPC User %s not allowed to call any methods\n", jreq.authUser);
200
0
            req->WriteReply(HTTP_FORBIDDEN);
201
0
            return false;
202
203
        // singleton request
204
0
        } else if (valRequest.isObject()) {
205
0
            jreq.parse(valRequest);
206
0
            if (user_has_whitelist && !g_rpc_whitelist[jreq.authUser].count(jreq.strMethod)) {
207
0
                LogPrintf("RPC User %s not allowed to call method %s\n", jreq.authUser, jreq.strMethod);
208
0
                req->WriteReply(HTTP_FORBIDDEN);
209
0
                return false;
210
0
            }
211
212
            // Legacy 1.0/1.1 behavior is for failed requests to throw
213
            // exceptions which return HTTP errors and RPC errors to the client.
214
            // 2.0 behavior is to catch exceptions and return HTTP success with
215
            // RPC errors, as long as there is not an actual HTTP server error.
216
0
            const bool catch_errors{jreq.m_json_version == JSONRPCVersion::V2};
217
0
            reply = JSONRPCExec(jreq, catch_errors);
218
219
0
            if (jreq.IsNotification()) {
220
                // Even though we do execute notifications, we do not respond to them
221
0
                req->WriteReply(HTTP_NO_CONTENT);
222
0
                return true;
223
0
            }
224
225
        // array of requests
226
0
        } else if (valRequest.isArray()) {
227
            // Check authorization for each request's method
228
0
            if (user_has_whitelist) {
229
0
                for (unsigned int reqIdx = 0; reqIdx < valRequest.size(); reqIdx++) {
230
0
                    if (!valRequest[reqIdx].isObject()) {
231
0
                        throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
232
0
                    } else {
233
0
                        const UniValue& request = valRequest[reqIdx].get_obj();
234
                        // Parse method
235
0
                        std::string strMethod = request.find_value("method").get_str();
236
0
                        if (!g_rpc_whitelist[jreq.authUser].count(strMethod)) {
237
0
                            LogPrintf("RPC User %s not allowed to call method %s\n", jreq.authUser, strMethod);
238
0
                            req->WriteReply(HTTP_FORBIDDEN);
239
0
                            return false;
240
0
                        }
241
0
                    }
242
0
                }
243
0
            }
244
245
            // Execute each request
246
0
            reply = UniValue::VARR;
247
0
            for (size_t i{0}; i < valRequest.size(); ++i) {
248
                // Batches never throw HTTP errors, they are always just included
249
                // in "HTTP OK" responses. Notifications never get any response.
250
0
                UniValue response;
251
0
                try {
252
0
                    jreq.parse(valRequest[i]);
253
0
                    response = JSONRPCExec(jreq, /*catch_errors=*/true);
254
0
                } catch (UniValue& e) {
255
0
                    response = JSONRPCReplyObj(NullUniValue, std::move(e), jreq.id, jreq.m_json_version);
256
0
                } catch (const std::exception& e) {
257
0
                    response = JSONRPCReplyObj(NullUniValue, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq.id, jreq.m_json_version);
258
0
                }
259
0
                if (!jreq.IsNotification()) {
260
0
                    reply.push_back(std::move(response));
261
0
                }
262
0
            }
263
            // Return no response for an all-notification batch, but only if the
264
            // batch request is non-empty. Technically according to the JSON-RPC
265
            // 2.0 spec, an empty batch request should also return no response,
266
            // However, if the batch request is empty, it means the request did
267
            // not contain any JSON-RPC version numbers, so returning an empty
268
            // response could break backwards compatibility with old RPC clients
269
            // relying on previous behavior. Return an empty array instead of an
270
            // empty response in this case to favor being backwards compatible
271
            // over complying with the JSON-RPC 2.0 spec in this case.
272
0
            if (reply.size() == 0 && valRequest.size() > 0) {
273
0
                req->WriteReply(HTTP_NO_CONTENT);
274
0
                return true;
275
0
            }
276
0
        }
277
0
        else
278
0
            throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error");
279
280
0
        req->WriteHeader("Content-Type", "application/json");
281
0
        req->WriteReply(HTTP_OK, reply.write() + "\n");
282
0
    } catch (UniValue& e) {
283
0
        JSONErrorReply(req, std::move(e), jreq);
284
0
        return false;
285
0
    } catch (const std::exception& e) {
286
0
        JSONErrorReply(req, JSONRPCError(RPC_PARSE_ERROR, e.what()), jreq);
287
0
        return false;
288
0
    }
289
0
    return true;
290
0
}
291
292
static bool InitRPCAuthentication()
293
0
{
294
0
    if (gArgs.GetArg("-rpcpassword", "") == "")
295
0
    {
296
0
        std::optional<fs::perms> cookie_perms{std::nullopt};
297
0
        auto cookie_perms_arg{gArgs.GetArg("-rpccookieperms")};
298
0
        if (cookie_perms_arg) {
299
0
            auto perm_opt = InterpretPermString(*cookie_perms_arg);
300
0
            if (!perm_opt) {
301
0
                LogError("Invalid -rpccookieperms=%s; must be one of 'owner', 'group', or 'all'.", *cookie_perms_arg);
302
0
                return false;
303
0
            }
304
0
            cookie_perms = *perm_opt;
305
0
        }
306
307
0
        assert(strRPCUserColonPass.empty()); // Only support initializing once
308
0
        if (!GenerateAuthCookie(&strRPCUserColonPass, cookie_perms)) {
309
0
            return false;
310
0
        }
311
0
        if (strRPCUserColonPass.empty()) {
312
0
            LogInfo("RPC authentication cookie file generation is disabled.");
313
0
        } else {
314
0
            LogInfo("Using random cookie authentication.");
315
0
        }
316
0
    } else {
317
0
        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");
318
0
        strRPCUserColonPass = gArgs.GetArg("-rpcuser", "") + ":" + gArgs.GetArg("-rpcpassword", "");
319
0
    }
320
321
0
    if (!gArgs.GetArgs("-rpcauth").empty()) {
322
0
        LogInfo("Using rpcauth authentication.\n");
323
0
        for (const std::string& rpcauth : gArgs.GetArgs("-rpcauth")) {
324
0
            std::vector<std::string> fields{SplitString(rpcauth, ':')};
325
0
            const std::vector<std::string> salt_hmac{SplitString(fields.back(), '$')};
326
0
            if (fields.size() == 2 && salt_hmac.size() == 2) {
327
0
                fields.pop_back();
328
0
                fields.insert(fields.end(), salt_hmac.begin(), salt_hmac.end());
329
0
                g_rpcauth.push_back(fields);
330
0
            } else {
331
0
                LogPrintf("Invalid -rpcauth argument.\n");
332
0
                return false;
333
0
            }
334
0
        }
335
0
    }
336
337
0
    g_rpc_whitelist_default = gArgs.GetBoolArg("-rpcwhitelistdefault", !gArgs.GetArgs("-rpcwhitelist").empty());
338
0
    for (const std::string& strRPCWhitelist : gArgs.GetArgs("-rpcwhitelist")) {
339
0
        auto pos = strRPCWhitelist.find(':');
340
0
        std::string strUser = strRPCWhitelist.substr(0, pos);
341
0
        bool intersect = g_rpc_whitelist.count(strUser);
342
0
        std::set<std::string>& whitelist = g_rpc_whitelist[strUser];
343
0
        if (pos != std::string::npos) {
344
0
            std::string strWhitelist = strRPCWhitelist.substr(pos + 1);
345
0
            std::vector<std::string> whitelist_split = SplitString(strWhitelist, ", ");
346
0
            std::set<std::string> new_whitelist{
347
0
                std::make_move_iterator(whitelist_split.begin()),
348
0
                std::make_move_iterator(whitelist_split.end())};
349
0
            if (intersect) {
350
0
                std::set<std::string> tmp_whitelist;
351
0
                std::set_intersection(new_whitelist.begin(), new_whitelist.end(),
352
0
                       whitelist.begin(), whitelist.end(), std::inserter(tmp_whitelist, tmp_whitelist.end()));
353
0
                new_whitelist = std::move(tmp_whitelist);
354
0
            }
355
0
            whitelist = std::move(new_whitelist);
356
0
        }
357
0
    }
358
359
0
    return true;
360
0
}
361
362
bool StartHTTPRPC(const std::any& context)
363
0
{
364
0
    LogDebug(BCLog::RPC, "Starting HTTP RPC server\n");
365
0
    if (!InitRPCAuthentication())
366
0
        return false;
367
368
0
    auto handle_rpc = [context](HTTPRequest* req, const std::string&) { return HTTPReq_JSONRPC(context, req); };
369
0
    RegisterHTTPHandler("/", true, handle_rpc);
370
0
    if (g_wallet_init_interface.HasWalletSupport()) {
371
0
        RegisterHTTPHandler("/wallet/", false, handle_rpc);
372
0
    }
373
0
    struct event_base* eventBase = EventBase();
374
0
    assert(eventBase);
375
0
    httpRPCTimerInterface = std::make_unique<HTTPRPCTimerInterface>(eventBase);
376
0
    RPCSetTimerInterface(httpRPCTimerInterface.get());
377
0
    return true;
378
0
}
379
380
void InterruptHTTPRPC()
381
0
{
382
0
    LogDebug(BCLog::RPC, "Interrupting HTTP RPC server\n");
383
0
}
384
385
void StopHTTPRPC()
386
0
{
387
0
    LogDebug(BCLog::RPC, "Stopping HTTP RPC server\n");
388
0
    UnregisterHTTPHandler("/", true);
389
0
    if (g_wallet_init_interface.HasWalletSupport()) {
390
0
        UnregisterHTTPHandler("/wallet/", false);
391
0
    }
392
0
    if (httpRPCTimerInterface) {
393
0
        RPCUnsetTimerInterface(httpRPCTimerInterface.get());
394
0
        httpRPCTimerInterface.reset();
395
0
    }
396
0
}