rpc: check that "version" is "2.0" in request objects (#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.
This commit is contained in:
Seungbae Yu 2022-09-03 00:47:29 +09:00 committed by GitHub
parent 90711efb0a
commit 38e002f464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 4 deletions

View File

@ -58,21 +58,25 @@ type jsonrpcMessage struct {
} }
func (msg *jsonrpcMessage) isNotification() bool { func (msg *jsonrpcMessage) isNotification() bool {
return msg.ID == nil && msg.Method != "" return msg.hasValidVersion() && msg.ID == nil && msg.Method != ""
} }
func (msg *jsonrpcMessage) isCall() bool { func (msg *jsonrpcMessage) isCall() bool {
return msg.hasValidID() && msg.Method != "" return msg.hasValidVersion() && msg.hasValidID() && msg.Method != ""
} }
func (msg *jsonrpcMessage) isResponse() bool { func (msg *jsonrpcMessage) isResponse() bool {
return msg.hasValidID() && msg.Method == "" && msg.Params == nil && (msg.Result != nil || msg.Error != nil) return msg.hasValidVersion() && msg.hasValidID() && msg.Method == "" && msg.Params == nil && (msg.Result != nil || msg.Error != nil)
} }
func (msg *jsonrpcMessage) hasValidID() bool { func (msg *jsonrpcMessage) hasValidID() bool {
return len(msg.ID) > 0 && msg.ID[0] != '{' && msg.ID[0] != '[' return len(msg.ID) > 0 && msg.ID[0] != '{' && msg.ID[0] != '['
} }
func (msg *jsonrpcMessage) hasValidVersion() bool {
return msg.Version == vsn
}
func (msg *jsonrpcMessage) isSubscribe() bool { func (msg *jsonrpcMessage) isSubscribe() bool {
return strings.HasSuffix(msg.Method, subscribeMethodSuffix) return strings.HasSuffix(msg.Method, subscribeMethodSuffix)
} }

View File

@ -79,7 +79,7 @@ func TestSubscriptions(t *testing.T) {
request := map[string]interface{}{ request := map[string]interface{}{
"id": i, "id": i,
"method": fmt.Sprintf("%s_subscribe", namespace), "method": fmt.Sprintf("%s_subscribe", namespace),
"version": "2.0", "jsonrpc": "2.0",
"params": []interface{}{"someSubscription", notificationCount, i}, "params": []interface{}{"someSubscription", notificationCount, i},
} }
if err := out.Encode(&request); err != nil { if err := out.Encode(&request); err != nil {

19
rpc/testdata/invalid-badversion.js vendored Normal file
View File

@ -0,0 +1,19 @@
// This test checks processing of messages with invalid Version.
--> {"jsonrpc":"2.0","id":1,"method":"test_echo","params":["x", 3]}
<-- {"jsonrpc":"2.0","id":1,"result":{"String":"x","Int":3,"Args":null}}
--> {"jsonrpc":"2.1","id":1,"method":"test_echo","params":["x", 3]}
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}
--> {"jsonrpc":"go-ethereum","id":1,"method":"test_echo","params":["x", 3]}
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}
--> {"jsonrpc":1,"id":1,"method":"test_echo","params":["x", 3]}
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}
--> {"jsonrpc":2.0,"id":1,"method":"test_echo","params":["x", 3]}
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}
--> {"id":1,"method":"test_echo","params":["x", 3]}
<-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}