LCOV - code coverage report
Current view: top level - src/rpc - request.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 104 120 86.7 %
Date: 2021-06-29 14:35:33 Functions: 10 10 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 28 40 70.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2020 The Bitcoin Core developers
#       3                 :            : // Distributed under the MIT software license, see the accompanying
#       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       5                 :            : 
#       6                 :            : #include <rpc/request.h>
#       7                 :            : 
#       8                 :            : #include <fs.h>
#       9                 :            : 
#      10                 :            : #include <random.h>
#      11                 :            : #include <rpc/protocol.h>
#      12                 :            : #include <util/system.h>
#      13                 :            : #include <util/strencodings.h>
#      14                 :            : 
#      15                 :            : /**
#      16                 :            :  * JSON-RPC protocol.  Bitcoin speaks version 1.0 for maximum compatibility,
#      17                 :            :  * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
#      18                 :            :  * unspecified (HTTP errors and contents of 'error').
#      19                 :            :  *
#      20                 :            :  * 1.0 spec: http://json-rpc.org/wiki/specification
#      21                 :            :  * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
#      22                 :            :  */
#      23                 :            : 
#      24                 :            : UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
#      25                 :        719 : {
#      26                 :        719 :     UniValue request(UniValue::VOBJ);
#      27                 :        719 :     request.pushKV("method", strMethod);
#      28                 :        719 :     request.pushKV("params", params);
#      29                 :        719 :     request.pushKV("id", id);
#      30                 :        719 :     return request;
#      31                 :        719 : }
#      32                 :            : 
#      33                 :            : UniValue JSONRPCReplyObj(const UniValue& result, const UniValue& error, const UniValue& id)
#      34                 :     111368 : {
#      35                 :     111368 :     UniValue reply(UniValue::VOBJ);
#      36         [ +  + ]:     111368 :     if (!error.isNull())
#      37                 :       6163 :         reply.pushKV("result", NullUniValue);
#      38                 :     105205 :     else
#      39                 :     105205 :         reply.pushKV("result", result);
#      40                 :     111368 :     reply.pushKV("error", error);
#      41                 :     111368 :     reply.pushKV("id", id);
#      42                 :     111368 :     return reply;
#      43                 :     111368 : }
#      44                 :            : 
#      45                 :            : std::string JSONRPCReply(const UniValue& result, const UniValue& error, const UniValue& id)
#      46                 :     111203 : {
#      47                 :     111203 :     UniValue reply = JSONRPCReplyObj(result, error, id);
#      48                 :     111203 :     return reply.write() + "\n";
#      49                 :     111203 : }
#      50                 :            : 
#      51                 :            : UniValue JSONRPCError(int code, const std::string& message)
#      52                 :       6255 : {
#      53                 :       6255 :     UniValue error(UniValue::VOBJ);
#      54                 :       6255 :     error.pushKV("code", code);
#      55                 :       6255 :     error.pushKV("message", message);
#      56                 :       6255 :     return error;
#      57                 :       6255 : }
#      58                 :            : 
#      59                 :            : /** Username used when cookie authentication is in use (arbitrary, only for
#      60                 :            :  * recognizability in debugging/logging purposes)
#      61                 :            :  */
#      62                 :            : static const std::string COOKIEAUTH_USER = "__cookie__";
#      63                 :            : /** Default name for auth cookie file */
#      64                 :            : static const std::string COOKIEAUTH_FILE = ".cookie";
#      65                 :            : 
#      66                 :            : /** Get name of RPC authentication cookie file */
#      67                 :            : static fs::path GetAuthCookieFile(bool temp=false)
#      68                 :       2666 : {
#      69                 :       2666 :     std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE);
#      70         [ +  + ]:       2666 :     if (temp) {
#      71                 :        658 :         arg += ".tmp";
#      72                 :        658 :     }
#      73                 :       2666 :     return AbsPathForConfigVal(fs::path(arg));
#      74                 :       2666 : }
#      75                 :            : 
#      76                 :            : bool GenerateAuthCookie(std::string *cookie_out)
#      77                 :        658 : {
#      78                 :        658 :     const size_t COOKIE_SIZE = 32;
#      79                 :        658 :     unsigned char rand_pwd[COOKIE_SIZE];
#      80                 :        658 :     GetRandBytes(rand_pwd, COOKIE_SIZE);
#      81                 :        658 :     std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
#      82                 :            : 
#      83                 :            :     /** the umask determines what permissions are used to create this file -
#      84                 :            :      * these are set to 077 in init.cpp unless overridden with -sysperms.
#      85                 :            :      */
#      86                 :        658 :     fsbridge::ofstream file;
#      87                 :        658 :     fs::path filepath_tmp = GetAuthCookieFile(true);
#      88                 :        658 :     file.open(filepath_tmp);
#      89         [ +  + ]:        658 :     if (!file.is_open()) {
#      90                 :          1 :         LogPrintf("Unable to open cookie authentication file %s for writing\n", filepath_tmp.string());
#      91                 :          1 :         return false;
#      92                 :          1 :     }
#      93                 :        657 :     file << cookie;
#      94                 :        657 :     file.close();
#      95                 :            : 
#      96                 :        657 :     fs::path filepath = GetAuthCookieFile(false);
#      97         [ -  + ]:        657 :     if (!RenameOver(filepath_tmp, filepath)) {
#      98                 :          0 :         LogPrintf("Unable to rename cookie authentication file %s to %s\n", filepath_tmp.string(), filepath.string());
#      99                 :          0 :         return false;
#     100                 :          0 :     }
#     101                 :        657 :     LogPrintf("Generated RPC authentication cookie %s\n", filepath.string());
#     102                 :            : 
#     103         [ +  - ]:        657 :     if (cookie_out)
#     104                 :        657 :         *cookie_out = cookie;
#     105                 :        657 :     return true;
#     106                 :        657 : }
#     107                 :            : 
#     108                 :            : bool GetAuthCookie(std::string *cookie_out)
#     109                 :        688 : {
#     110                 :        688 :     fsbridge::ifstream file;
#     111                 :        688 :     std::string cookie;
#     112                 :        688 :     fs::path filepath = GetAuthCookieFile();
#     113                 :        688 :     file.open(filepath);
#     114         [ +  + ]:        688 :     if (!file.is_open())
#     115                 :          1 :         return false;
#     116                 :        687 :     std::getline(file, cookie);
#     117                 :        687 :     file.close();
#     118                 :            : 
#     119         [ +  - ]:        687 :     if (cookie_out)
#     120                 :        687 :         *cookie_out = cookie;
#     121                 :        687 :     return true;
#     122                 :        687 : }
#     123                 :            : 
#     124                 :            : void DeleteAuthCookie()
#     125                 :        663 : {
#     126                 :        663 :     try {
#     127                 :        663 :         fs::remove(GetAuthCookieFile());
#     128                 :        663 :     } catch (const fs::filesystem_error& e) {
#     129                 :          0 :         LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
#     130                 :          0 :     }
#     131                 :        663 : }
#     132                 :            : 
#     133                 :            : std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue& in)
#     134                 :         10 : {
#     135         [ -  + ]:         10 :     if (!in.isArray()) {
#     136                 :          0 :         throw std::runtime_error("Batch must be an array");
#     137                 :          0 :     }
#     138                 :         10 :     const size_t num {in.size()};
#     139                 :         10 :     std::vector<UniValue> batch(num);
#     140         [ +  + ]:         40 :     for (const UniValue& rec : in.getValues()) {
#     141         [ -  + ]:         40 :         if (!rec.isObject()) {
#     142                 :          0 :             throw std::runtime_error("Batch member must be an object");
#     143                 :          0 :         }
#     144                 :         40 :         size_t id = rec["id"].get_int();
#     145         [ -  + ]:         40 :         if (id >= num) {
#     146                 :          0 :             throw std::runtime_error("Batch member id is larger than batch size");
#     147                 :          0 :         }
#     148                 :         40 :         batch[id] = rec;
#     149                 :         40 :     }
#     150                 :         10 :     return batch;
#     151                 :         10 : }
#     152                 :            : 
#     153                 :            : void JSONRPCRequest::parse(const UniValue& valRequest)
#     154                 :     111356 : {
#     155                 :            :     // Parse request
#     156         [ -  + ]:     111356 :     if (!valRequest.isObject())
#     157                 :          0 :         throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
#     158                 :     111356 :     const UniValue& request = valRequest.get_obj();
#     159                 :            : 
#     160                 :            :     // Parse id now so errors from here on will have the id
#     161                 :     111356 :     id = find_value(request, "id");
#     162                 :            : 
#     163                 :            :     // Parse method
#     164                 :     111356 :     UniValue valMethod = find_value(request, "method");
#     165         [ -  + ]:     111356 :     if (valMethod.isNull())
#     166                 :          0 :         throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
#     167         [ -  + ]:     111356 :     if (!valMethod.isStr())
#     168                 :          0 :         throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
#     169                 :     111356 :     strMethod = valMethod.get_str();
#     170         [ +  + ]:     111356 :     if (fLogIPs)
#     171         [ +  - ]:     111356 :         LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod),
#     172                 :     111356 :             this->authUser, this->peerAddr);
#     173                 :     111356 :     else
#     174         [ +  - ]:     111356 :         LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
#     175                 :            : 
#     176                 :            :     // Parse params
#     177                 :     111356 :     UniValue valParams = find_value(request, "params");
#     178 [ +  + ][ +  + ]:     111356 :     if (valParams.isArray() || valParams.isObject())
#     179                 :     111292 :         params = valParams;
#     180         [ +  - ]:         64 :     else if (valParams.isNull())
#     181                 :         64 :         params = UniValue(UniValue::VARR);
#     182                 :          0 :     else
#     183                 :          0 :         throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object");
#     184                 :     111356 : }

Generated by: LCOV version 1.14