LCOV - code coverage report
Current view: top level - src/rpc - request.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 86.5 % 126 109
Test Date: 2024-07-04 05:05:02 Functions: 100.0 % 9 9
Branches: 48.9 % 284 139

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 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 <common/args.h>
       9                 :             : #include <logging.h>
      10                 :             : #include <random.h>
      11                 :             : #include <rpc/protocol.h>
      12                 :             : #include <util/fs.h>
      13                 :             : #include <util/fs_helpers.h>
      14                 :             : #include <util/strencodings.h>
      15                 :             : 
      16                 :             : #include <fstream>
      17                 :             : #include <stdexcept>
      18                 :             : #include <string>
      19                 :             : #include <vector>
      20                 :             : 
      21                 :             : /**
      22                 :             :  * JSON-RPC protocol.  Bitcoin speaks version 1.0 for maximum compatibility,
      23                 :             :  * but uses JSON-RPC 1.1/2.0 standards for parts of the 1.0 standard that were
      24                 :             :  * unspecified (HTTP errors and contents of 'error').
      25                 :             :  *
      26                 :             :  * 1.0 spec: http://json-rpc.org/wiki/specification
      27                 :             :  * 1.2 spec: http://jsonrpc.org/historical/json-rpc-over-http.html
      28                 :             :  *
      29                 :             :  * If the server receives a request with the JSON-RPC 2.0 marker `{"jsonrpc": "2.0"}`
      30                 :             :  * then Bitcoin will respond with a strictly specified response.
      31                 :             :  * It will only return an HTTP error code if an actual HTTP error is encountered
      32                 :             :  * such as the endpoint is not found (404) or the request is not formatted correctly (500).
      33                 :             :  * Otherwise the HTTP code is always OK (200) and RPC errors will be included in the
      34                 :             :  * response body.
      35                 :             :  *
      36                 :             :  * 2.0 spec: https://www.jsonrpc.org/specification
      37                 :             :  *
      38                 :             :  * Also see http://www.simple-is-better.org/rpc/#differences-between-1-0-and-2-0
      39                 :             :  */
      40                 :             : 
      41                 :         670 : UniValue JSONRPCRequestObj(const std::string& strMethod, const UniValue& params, const UniValue& id)
      42                 :             : {
      43                 :         670 :     UniValue request(UniValue::VOBJ);
      44   [ +  -  +  -  :        1340 :     request.pushKV("method", strMethod);
                   +  - ]
      45   [ +  -  +  -  :        1340 :     request.pushKV("params", params);
                   +  - ]
      46   [ +  -  +  -  :        1340 :     request.pushKV("id", id);
                   +  - ]
      47   [ +  -  +  -  :        1340 :     request.pushKV("jsonrpc", "2.0");
                   +  - ]
      48                 :         670 :     return request;
      49                 :           0 : }
      50                 :             : 
      51                 :      196023 : UniValue JSONRPCReplyObj(UniValue result, UniValue error, std::optional<UniValue> id, JSONRPCVersion jsonrpc_version)
      52                 :             : {
      53                 :      196023 :     UniValue reply(UniValue::VOBJ);
      54                 :             :     // Add JSON-RPC version number field in v2 only.
      55   [ +  +  +  -  :      391898 :     if (jsonrpc_version == JSONRPCVersion::V2) reply.pushKV("jsonrpc", "2.0");
             +  -  +  - ]
      56                 :             : 
      57                 :             :     // Add both result and error fields in v1, even though one will be null.
      58                 :             :     // Omit the null field in v2.
      59         [ +  + ]:      196023 :     if (error.isNull()) {
      60   [ +  -  +  - ]:      379048 :         reply.pushKV("result", std::move(result));
      61   [ +  +  +  -  :      189652 :         if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("error", NullUniValue);
             +  -  +  - ]
      62                 :             :     } else {
      63   [ +  +  +  -  :        6519 :         if (jsonrpc_version == JSONRPCVersion::V1_LEGACY) reply.pushKV("result", NullUniValue);
             +  -  +  - ]
      64   [ +  -  +  - ]:       12998 :         reply.pushKV("error", std::move(error));
      65                 :             :     }
      66   [ +  +  +  -  :      392016 :     if (id.has_value()) reply.pushKV("id", std::move(id.value()));
                   +  - ]
      67                 :      196023 :     return reply;
      68                 :           0 : }
      69                 :             : 
      70                 :        6583 : UniValue JSONRPCError(int code, const std::string& message)
      71                 :             : {
      72                 :        6583 :     UniValue error(UniValue::VOBJ);
      73   [ +  -  +  -  :       13166 :     error.pushKV("code", code);
                   +  - ]
      74   [ +  -  +  -  :       13166 :     error.pushKV("message", message);
                   +  - ]
      75                 :        6583 :     return error;
      76                 :           0 : }
      77                 :             : 
      78                 :             : /** Username used when cookie authentication is in use (arbitrary, only for
      79                 :             :  * recognizability in debugging/logging purposes)
      80                 :             :  */
      81                 :             : static const std::string COOKIEAUTH_USER = "__cookie__";
      82                 :             : /** Default name for auth cookie file */
      83                 :             : static const char* const COOKIEAUTH_FILE = ".cookie";
      84                 :             : 
      85                 :             : /** Get name of RPC authentication cookie file */
      86                 :        4068 : static fs::path GetAuthCookieFile(bool temp=false)
      87                 :             : {
      88   [ +  -  +  - ]:        8136 :     fs::path arg = gArgs.GetPathArg("-rpccookiefile", COOKIEAUTH_FILE);
      89         [ +  + ]:        4068 :     if (temp) {
      90         [ +  - ]:        1162 :         arg += ".tmp";
      91                 :             :     }
      92         [ +  - ]:        4068 :     return AbsPathForConfigVal(gArgs, arg);
      93                 :        4068 : }
      94                 :             : 
      95                 :             : static bool g_generated_cookie = false;
      96                 :             : 
      97                 :        1162 : bool GenerateAuthCookie(std::string* cookie_out, std::optional<fs::perms> cookie_perms)
      98                 :             : {
      99                 :        1162 :     const size_t COOKIE_SIZE = 32;
     100                 :        1162 :     unsigned char rand_pwd[COOKIE_SIZE];
     101                 :        1162 :     GetRandBytes(rand_pwd);
     102   [ +  -  +  - ]:        2324 :     std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
     103                 :             : 
     104                 :             :     /** the umask determines what permissions are used to create this file -
     105                 :             :      * these are set to 0077 in common/system.cpp.
     106                 :             :      */
     107         [ +  - ]:        1162 :     std::ofstream file;
     108         [ +  - ]:        1162 :     fs::path filepath_tmp = GetAuthCookieFile(true);
     109         [ +  - ]:        1162 :     file.open(filepath_tmp);
     110         [ +  + ]:        1162 :     if (!file.is_open()) {
     111   [ +  -  +  -  :           3 :         LogInfo("Unable to open cookie authentication file %s for writing\n", fs::PathToString(filepath_tmp));
             +  -  +  - ]
     112                 :           1 :         return false;
     113                 :             :     }
     114         [ +  - ]:        1161 :     file << cookie;
     115         [ +  - ]:        1161 :     file.close();
     116                 :             : 
     117         [ +  - ]:        1161 :     fs::path filepath = GetAuthCookieFile(false);
     118   [ +  -  +  -  :        3483 :     if (!RenameOver(filepath_tmp, filepath)) {
             +  -  -  + ]
     119   [ #  #  #  #  :           0 :         LogInfo("Unable to rename cookie authentication file %s to %s\n", fs::PathToString(filepath_tmp), fs::PathToString(filepath));
          #  #  #  #  #  
                      # ]
     120                 :           0 :         return false;
     121                 :             :     }
     122         [ +  + ]:        1161 :     if (cookie_perms) {
     123                 :           3 :         std::error_code code;
     124                 :           3 :         fs::permissions(filepath, cookie_perms.value(), fs::perm_options::replace, code);
     125         [ -  + ]:           3 :         if (code) {
     126   [ #  #  #  #  :           0 :             LogInfo("Unable to set permissions on cookie authentication file %s\n", fs::PathToString(filepath_tmp));
             #  #  #  # ]
     127                 :           0 :             return false;
     128                 :             :         }
     129                 :             :     }
     130                 :             : 
     131                 :        1161 :     g_generated_cookie = true;
     132   [ +  -  +  -  :        3483 :     LogInfo("Generated RPC authentication cookie %s\n", fs::PathToString(filepath));
             +  -  +  - ]
     133   [ +  -  +  -  :        2322 :     LogInfo("Permissions used for cookie: %s\n", PermsToSymbolicString(fs::status(filepath).permissions()));
          +  -  +  -  +  
                      - ]
     134                 :             : 
     135         [ +  - ]:        1161 :     if (cookie_out)
     136         [ +  - ]:        2322 :         *cookie_out = cookie;
     137                 :             :     return true;
     138                 :        2323 : }
     139                 :             : 
     140                 :         584 : bool GetAuthCookie(std::string *cookie_out)
     141                 :             : {
     142                 :         584 :     std::ifstream file;
     143         [ +  - ]:         584 :     std::string cookie;
     144         [ +  - ]:         584 :     fs::path filepath = GetAuthCookieFile();
     145         [ +  - ]:         584 :     file.open(filepath);
     146         [ +  + ]:         584 :     if (!file.is_open())
     147                 :             :         return false;
     148         [ +  - ]:         570 :     std::getline(file, cookie);
     149         [ +  - ]:         570 :     file.close();
     150                 :             : 
     151         [ +  - ]:         570 :     if (cookie_out)
     152         [ +  - ]:        1154 :         *cookie_out = cookie;
     153                 :             :     return true;
     154                 :         584 : }
     155                 :             : 
     156                 :        1188 : void DeleteAuthCookie()
     157                 :             : {
     158                 :        1188 :     try {
     159         [ +  + ]:        1188 :         if (g_generated_cookie) {
     160                 :             :             // Delete the cookie file if it was generated by this process
     161   [ +  -  +  - ]:        2322 :             fs::remove(GetAuthCookieFile());
     162                 :             :         }
     163         [ -  - ]:           0 :     } catch (const fs::filesystem_error& e) {
     164   [ -  -  -  -  :           0 :         LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
             -  -  -  - ]
     165                 :           0 :     }
     166                 :        1188 : }
     167                 :             : 
     168                 :          28 : std::vector<UniValue> JSONRPCProcessBatchReply(const UniValue& in)
     169                 :             : {
     170         [ -  + ]:          28 :     if (!in.isArray()) {
     171         [ #  # ]:           0 :         throw std::runtime_error("Batch must be an array");
     172                 :             :     }
     173                 :          28 :     const size_t num {in.size()};
     174                 :          28 :     std::vector<UniValue> batch(num);
     175   [ +  -  +  + ]:         140 :     for (const UniValue& rec : in.getValues()) {
     176         [ -  + ]:         112 :         if (!rec.isObject()) {
     177         [ #  # ]:           0 :             throw std::runtime_error("Batch member must be an object");
     178                 :             :         }
     179   [ +  -  +  -  :         112 :         size_t id = rec["id"].getInt<int>();
                   +  - ]
     180         [ -  + ]:         112 :         if (id >= num) {
     181         [ #  # ]:           0 :             throw std::runtime_error("Batch member id is larger than batch size");
     182                 :             :         }
     183         [ +  - ]:         112 :         batch[id] = rec;
     184                 :             :     }
     185                 :          28 :     return batch;
     186                 :           0 : }
     187                 :             : 
     188                 :      195984 : void JSONRPCRequest::parse(const UniValue& valRequest)
     189                 :             : {
     190                 :             :     // Parse request
     191         [ -  + ]:      195984 :     if (!valRequest.isObject())
     192   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INVALID_REQUEST, "Invalid Request object");
     193                 :      195984 :     const UniValue& request = valRequest.get_obj();
     194                 :             : 
     195                 :             :     // Parse id now so errors from here on will have the id
     196         [ +  + ]:      391968 :     if (request.exists("id")) {
     197                 :      195949 :         id = request.find_value("id");
     198                 :             :     } else {
     199                 :          35 :         id = std::nullopt;
     200                 :             :     }
     201                 :             : 
     202                 :             :     // Check for JSON-RPC 2.0 (default 1.1)
     203                 :      195984 :     m_json_version = JSONRPCVersion::V1_LEGACY;
     204                 :      195984 :     const UniValue& jsonrpc_version = request.find_value("jsonrpc");
     205         [ +  + ]:      195984 :     if (!jsonrpc_version.isNull()) {
     206         [ +  + ]:      195843 :         if (!jsonrpc_version.isStr()) {
     207   [ +  -  +  - ]:           2 :             throw JSONRPCError(RPC_INVALID_REQUEST, "jsonrpc field must be a string");
     208                 :             :         }
     209                 :             :         // The "jsonrpc" key was added in the 2.0 spec, but some older documentation
     210                 :             :         // incorrectly included {"jsonrpc":"1.0"} in a request object, so we
     211                 :             :         // maintain that for backwards compatibility.
     212         [ +  + ]:      195842 :         if (jsonrpc_version.get_str() == "1.0") {
     213                 :           4 :             m_json_version = JSONRPCVersion::V1_LEGACY;
     214         [ +  + ]:      195838 :         } else if (jsonrpc_version.get_str() == "2.0") {
     215                 :      195833 :             m_json_version = JSONRPCVersion::V2;
     216                 :             :         } else {
     217   [ +  -  +  - ]:          10 :             throw JSONRPCError(RPC_INVALID_REQUEST, "JSON-RPC version not supported");
     218                 :             :         }
     219                 :             :     }
     220                 :             : 
     221                 :             :     // Parse method
     222                 :      195978 :     const UniValue& valMethod{request.find_value("method")};
     223         [ +  + ]:      195978 :     if (valMethod.isNull())
     224   [ +  -  +  - ]:          16 :         throw JSONRPCError(RPC_INVALID_REQUEST, "Missing method");
     225         [ -  + ]:      195970 :     if (!valMethod.isStr())
     226   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
     227                 :      195970 :     strMethod = valMethod.get_str();
     228         [ +  + ]:      195970 :     if (fLogIPs)
     229   [ +  -  +  -  :          56 :         LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s peeraddr=%s\n", SanitizeString(strMethod),
             +  -  +  - ]
     230                 :             :             this->authUser, this->peerAddr);
     231                 :             :     else
     232   [ +  -  +  -  :      391884 :         LogPrint(BCLog::RPC, "ThreadRPCServer method=%s user=%s\n", SanitizeString(strMethod), this->authUser);
             +  -  +  - ]
     233                 :             : 
     234                 :             :     // Parse params
     235                 :      195970 :     const UniValue& valParams{request.find_value("params")};
     236   [ +  +  +  + ]:      195970 :     if (valParams.isArray() || valParams.isObject())
     237                 :      195819 :         params = valParams;
     238         [ +  - ]:         151 :     else if (valParams.isNull())
     239                 :         151 :         params = UniValue(UniValue::VARR);
     240                 :             :     else
     241   [ #  #  #  # ]:           0 :         throw JSONRPCError(RPC_INVALID_REQUEST, "Params must be an array or object");
     242                 :      195970 : }
        

Generated by: LCOV version 2.0-1