From 1e1c027218db4c3c37f32d0902f1444696950c86 Mon Sep 17 00:00:00 2001 From: dbadoy4874 Date: Mon, 22 Aug 2022 16:51:55 +0900 Subject: [PATCH 1/4] rpc: check the JSON-RPC 2.0 spec that `Version` must be `2.0` --- rpc/json.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rpc/json.go b/rpc/json.go index 6024f1e7dc9b..6b2ac2d52a7b 100644 --- a/rpc/json.go +++ b/rpc/json.go @@ -58,21 +58,25 @@ type jsonrpcMessage struct { } func (msg *jsonrpcMessage) isNotification() bool { - return msg.ID == nil && msg.Method != "" + return msg.hasValidVersion() && msg.ID == nil && msg.Method != "" } func (msg *jsonrpcMessage) isCall() bool { - return msg.hasValidID() && msg.Method != "" + return msg.hasValidVersion() && msg.hasValidID() && msg.Method != "" } 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 { 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 { return strings.HasSuffix(msg.Method, subscribeMethodSuffix) } From e5c575664af3e3a316e21356f863de593c610e2a Mon Sep 17 00:00:00 2001 From: dbadoy4874 Date: Mon, 22 Aug 2022 18:29:04 +0900 Subject: [PATCH 2/4] rpc: fix incorrect field name in TestSubscriptions --- rpc/subscription_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/subscription_test.go b/rpc/subscription_test.go index a920205c001f..b2704578291e 100644 --- a/rpc/subscription_test.go +++ b/rpc/subscription_test.go @@ -79,7 +79,7 @@ func TestSubscriptions(t *testing.T) { request := map[string]interface{}{ "id": i, "method": fmt.Sprintf("%s_subscribe", namespace), - "version": "2.0", + "jsonrpc": "2.0", "params": []interface{}{"someSubscription", notificationCount, i}, } if err := out.Encode(&request); err != nil { From 44f404f0c0ce3474b25e1ee024937baffe2ef5fb Mon Sep 17 00:00:00 2001 From: dbadoy4874 Date: Wed, 31 Aug 2022 00:36:42 +0900 Subject: [PATCH 3/4] rpc: add test cases to check invalid version --- rpc/testdata/invalid-badversion.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 rpc/testdata/invalid-badversion.js diff --git a/rpc/testdata/invalid-badversion.js b/rpc/testdata/invalid-badversion.js new file mode 100644 index 000000000000..3a17cdc71114 --- /dev/null +++ b/rpc/testdata/invalid-badversion.js @@ -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"}} From 5875ac71e240a16cdf70db741850c47c4b508d1f Mon Sep 17 00:00:00 2001 From: dbadoy4874 Date: Wed, 31 Aug 2022 00:42:13 +0900 Subject: [PATCH 4/4] remove blank --- rpc/testdata/invalid-badversion.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rpc/testdata/invalid-badversion.js b/rpc/testdata/invalid-badversion.js index 3a17cdc71114..75b5291dc3f0 100644 --- a/rpc/testdata/invalid-badversion.js +++ b/rpc/testdata/invalid-badversion.js @@ -1,19 +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,"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.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":"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":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,"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]} +--> {"id":1,"method":"test_echo","params":["x", 3]} <-- {"jsonrpc":"2.0","id":1,"error":{"code":-32600,"message":"invalid request"}}