Skip to content

Commit 7b15f5d

Browse files
dbadoygzliudan
authored andcommitted
rpc: check that version is 2.0 in request objects ethereum#25570
The JSON-RPC spec requires the "version" field to be exactly "2.0", so we should verify that. This change is not backwards-compatible with sloppy client implementations, but I decided to go ahead with it anyway because the failure will be caught via the returned error.
1 parent 69323a2 commit 7b15f5d

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

rpc/json.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,25 @@ type jsonrpcMessage struct {
5858
}
5959

6060
func (msg *jsonrpcMessage) isNotification() bool {
61-
return msg.ID == nil && msg.Method != ""
61+
return msg.hasValidVersion() && msg.ID == nil && msg.Method != ""
6262
}
6363

6464
func (msg *jsonrpcMessage) isCall() bool {
65-
return msg.hasValidID() && msg.Method != ""
65+
return msg.hasValidVersion() && msg.hasValidID() && msg.Method != ""
6666
}
6767

6868
func (msg *jsonrpcMessage) isResponse() bool {
69-
return msg.hasValidID() && msg.Method == "" && msg.Params == nil && (msg.Result != nil || msg.Error != nil)
69+
return msg.hasValidVersion() && msg.hasValidID() && msg.Method == "" && msg.Params == nil && (msg.Result != nil || msg.Error != nil)
7070
}
7171

7272
func (msg *jsonrpcMessage) hasValidID() bool {
7373
return len(msg.ID) > 0 && msg.ID[0] != '{' && msg.ID[0] != '['
7474
}
7575

76+
func (msg *jsonrpcMessage) hasValidVersion() bool {
77+
return msg.Version == vsn
78+
}
79+
7680
func (msg *jsonrpcMessage) isSubscribe() bool {
7781
return strings.HasSuffix(msg.Method, subscribeMethodSuffix)
7882
}

rpc/subscription_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestSubscriptions(t *testing.T) {
7979
request := map[string]interface{}{
8080
"id": i,
8181
"method": fmt.Sprintf("%s_subscribe", namespace),
82-
"version": "2.0",
82+
"jsonrpc": "2.0",
8383
"params": []interface{}{"someSubscription", notificationCount, i},
8484
}
8585
if err := out.Encode(&request); err != nil {

rpc/testdata/invalid-badversion.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This test checks processing of messages with invalid Version.
2+
3+
--> {"jsonrpc":"2.0","id":1,"method":"test_echo","params":["x", 3]}
4+
<-- {"jsonrpc":"2.0","id":1,"result":{"String":"x","Int":3,"Args":null}}
5+
6+
--> {"jsonrpc":"2.1","id":1,"method":"test_echo","params":["x", 3]}
7+
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}
8+
9+
--> {"jsonrpc":"go-ethereum","id":1,"method":"test_echo","params":["x", 3]}
10+
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}
11+
12+
--> {"jsonrpc":1,"id":1,"method":"test_echo","params":["x", 3]}
13+
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}
14+
15+
--> {"jsonrpc":2.0,"id":1,"method":"test_echo","params":["x", 3]}
16+
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}
17+
18+
--> {"id":1,"method":"test_echo","params":["x", 3]}
19+
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}

0 commit comments

Comments
 (0)