Skip to content

Commit 4bb5473

Browse files
committed
JSON-RPC: Tolerate non-standard "jsonrpc" versions (treat as 1.1)
1 parent 1248d0d commit 4bb5473

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

src/rpc/request.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,8 @@ void JSONRPCRequest::parse(const UniValue& valRequest)
203203
m_json_version = JSONRPCVersion::V1_LEGACY;
204204
const UniValue& jsonrpc_version = request.find_value("jsonrpc");
205205
if (!jsonrpc_version.isNull()) {
206-
if (!jsonrpc_version.isStr()) {
207-
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-
if (jsonrpc_version.get_str() == "1.0") {
213-
m_json_version = JSONRPCVersion::V1_LEGACY;
214-
} else if (jsonrpc_version.get_str() == "2.0") {
206+
if (jsonrpc_version.isStr() && jsonrpc_version.get_str() == "2.0") {
215207
m_json_version = JSONRPCVersion::V2;
216-
} else {
217-
throw JSONRPCError(RPC_INVALID_REQUEST, "JSON-RPC version not supported");
218208
}
219209
}
220210

test/functional/interface_rpc.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,13 @@ def test_batch_requests(self):
172172
self.log.info("Testing nonstandard jsonrpc 1.0 version number is accepted...")
173173
self.test_batch_request(lambda idx: BatchOptions(request_fields={"jsonrpc": "1.0"}))
174174

175-
self.log.info("Testing unrecognized jsonrpc version number is rejected...")
175+
self.log.info("Testing nonstandard jsonrpc 1.0 version number is accepted as a Number...")
176+
self.test_batch_request(lambda idx: BatchOptions(request_fields={"jsonrpc": 1.0}))
177+
178+
self.log.info("Testing unrecognized jsonrpc version number is accepted as if 1.0...")
176179
self.test_batch_request(lambda idx: BatchOptions(
177180
request_fields={"jsonrpc": "2.1"},
178-
response_fields={"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "JSON-RPC version not supported"}}))
181+
))
179182

180183
def test_http_status_codes(self):
181184
self.log.info("Testing HTTP status codes for JSON-RPC 1.1 requests...")
@@ -201,11 +204,11 @@ def test_http_status_codes(self):
201204
expect_http_rpc_status(200, RPC_INVALID_PARAMETER, self.nodes[0], "getblockhash", [42], 2, False)
202205
# force-send invalidly formatted requests
203206
response, status = send_json_rpc(self.nodes[0], {"jsonrpc": 2, "method": "getblockcount"})
204-
assert_equal(response, {"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "jsonrpc field must be a string"}})
205-
assert_equal(status, 400)
207+
assert_equal(response, {"result": 0, "error": None})
208+
assert_equal(status, 200)
206209
response, status = send_json_rpc(self.nodes[0], {"jsonrpc": "3.0", "method": "getblockcount"})
207-
assert_equal(response, {"result": None, "error": {"code": RPC_INVALID_REQUEST, "message": "JSON-RPC version not supported"}})
208-
assert_equal(status, 400)
210+
assert_equal(response, {"result": 0, "error": None})
211+
assert_equal(status, 200)
209212

210213
self.log.info("Testing HTTP status codes for JSON-RPC 2.0 notifications...")
211214
# Not notification: id exists

test/functional/test_framework/authproxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def _get_response(self):
192192
response = json.loads(responsedata, parse_float=decimal.Decimal)
193193
elapsed = time.time() - req_start_time
194194
if "error" in response and response["error"] is None:
195-
log.debug("<-%s- [%.6f] %s" % (response["id"], elapsed, json.dumps(response["result"], default=serialization_fallback, ensure_ascii=self.ensure_ascii)))
195+
log.debug("<-%s- [%.6f] %s" % (response.get("id"), elapsed, json.dumps(response["result"], default=serialization_fallback, ensure_ascii=self.ensure_ascii)))
196196
else:
197197
log.debug("<-- [%.6f] %s" % (elapsed, responsedata))
198198
return response, http_response.status

0 commit comments

Comments
 (0)