Merge compat_jsonrpc_weirdversions

This commit is contained in:
Luke Dashjr 2025-03-05 03:27:08 +00:00
commit d50d30bf83
3 changed files with 11 additions and 18 deletions

View File

@ -218,18 +218,8 @@ void JSONRPCRequest::parse(const UniValue& valRequest)
m_json_version = JSONRPCVersion::V1_LEGACY;
const UniValue& jsonrpc_version = request.find_value("jsonrpc");
if (!jsonrpc_version.isNull()) {
if (!jsonrpc_version.isStr()) {
throw JSONRPCError(RPC_INVALID_REQUEST, "jsonrpc field must be a string");
}
// The "jsonrpc" key was added in the 2.0 spec, but some older documentation
// incorrectly included {"jsonrpc":"1.0"} in a request object, so we
// maintain that for backwards compatibility.
if (jsonrpc_version.get_str() == "1.0") {
m_json_version = JSONRPCVersion::V1_LEGACY;
} else if (jsonrpc_version.get_str() == "2.0") {
if (jsonrpc_version.isStr() && jsonrpc_version.get_str() == "2.0") {
m_json_version = JSONRPCVersion::V2;
} else {
throw JSONRPCError(RPC_INVALID_REQUEST, "JSON-RPC version not supported");
}
}

View File

@ -172,10 +172,13 @@ class RPCInterfaceTest(BitcoinTestFramework):
self.log.info("Testing nonstandard jsonrpc 1.0 version number is accepted...")
self.test_batch_request(lambda idx: BatchOptions(request_fields={"jsonrpc": "1.0"}))
self.log.info("Testing unrecognized jsonrpc version number is rejected...")
self.log.info("Testing nonstandard jsonrpc 1.0 version number is accepted as a Number...")
self.test_batch_request(lambda idx: BatchOptions(request_fields={"jsonrpc": 1.0}))
self.log.info("Testing unrecognized jsonrpc version number is accepted as if 1.0...")
self.test_batch_request(lambda idx: BatchOptions(
request_fields={"jsonrpc": "2.1"},
response_fields={"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "JSON-RPC version not supported"}}))
))
def test_http_status_codes(self):
self.log.info("Testing HTTP status codes for JSON-RPC 1.1 requests...")
@ -201,11 +204,11 @@ class RPCInterfaceTest(BitcoinTestFramework):
expect_http_rpc_status(200, RPC_INVALID_PARAMETER, self.nodes[0], "getblockhash", [42], 2, False)
# force-send invalidly formatted requests
response, status = send_json_rpc(self.nodes[0], {"jsonrpc": 2, "method": "getblockcount"})
assert_equal(response, {"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "jsonrpc field must be a string"}})
assert_equal(status, 400)
assert_equal(response, {"result": 0, "error": None})
assert_equal(status, 200)
response, status = send_json_rpc(self.nodes[0], {"jsonrpc": "3.0", "method": "getblockcount"})
assert_equal(response, {"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "JSON-RPC version not supported"}})
assert_equal(status, 400)
assert_equal(response, {"result": 0, "error": None})
assert_equal(status, 200)
self.log.info("Testing HTTP status codes for JSON-RPC 2.0 notifications...")
# Not notification: id exists

View File

@ -192,7 +192,7 @@ class AuthServiceProxy():
response = json.loads(responsedata, parse_float=decimal.Decimal)
elapsed = time.time() - req_start_time
if "error" in response and response["error"] is None:
log.debug("<-%s- [%.6f] %s" % (response["id"], elapsed, json.dumps(response["result"], default=serialization_fallback, ensure_ascii=self.ensure_ascii)))
log.debug("<-%s- [%.6f] %s" % (response.get("id"), elapsed, json.dumps(response["result"], default=serialization_fallback, ensure_ascii=self.ensure_ascii)))
else:
log.debug("<-- [%.6f] %s" % (elapsed, responsedata))
return response, http_response.status