Skip to content

Commit 4608da6

Browse files
fjlunclezoro
authored andcommitted
rpc: fix issue with null JSON-RPC messages (ethereum#21497)
1 parent d1f95d5 commit 4608da6

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

rpc/json.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,22 @@ func (c *jsonCodec) remoteAddr() string {
194194
return c.remote
195195
}
196196

197-
func (c *jsonCodec) readBatch() (msg []*jsonrpcMessage, batch bool, err error) {
197+
func (c *jsonCodec) readBatch() (messages []*jsonrpcMessage, batch bool, err error) {
198198
// Decode the next JSON object in the input stream.
199199
// This verifies basic syntax, etc.
200200
var rawmsg json.RawMessage
201201
if err := c.decode(&rawmsg); err != nil {
202202
return nil, false, err
203203
}
204-
msg, batch = parseMessage(rawmsg)
205-
return msg, batch, nil
204+
messages, batch = parseMessage(rawmsg)
205+
for i, msg := range messages {
206+
if msg == nil {
207+
// Message is JSON 'null'. Replace with zero value so it
208+
// will be treated like any other invalid message.
209+
messages[i] = new(jsonrpcMessage)
210+
}
211+
}
212+
return messages, batch, nil
206213
}
207214

208215
func (c *jsonCodec) writeJSON(ctx context.Context, v interface{}) error {

rpc/testdata/invalid-batch.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
--> [1,2,3]
1111
<-- [{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}},{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}},{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}}]
1212

13+
--> [null]
14+
<-- [{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}}]
15+
1316
--> [{"jsonrpc":"2.0","id":1,"method":"test_echo","params":["foo",1]},55,{"jsonrpc":"2.0","id":2,"method":"unknown_method"},{"foo":"bar"}]
1417
<-- [{"jsonrpc":"2.0","id":1,"result":{"String":"foo","Int":1,"Args":null}},{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}},{"jsonrpc":"2.0","id":2,"error":{"code":-32601,"message":"the method unknown_method does not exist/is not available"}},{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}}]

rpc/testdata/invalid-nonobj.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
--> 1
44
<-- {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}}
5+
6+
--> null
7+
<-- {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}}

0 commit comments

Comments
 (0)