rpc: fix issue with null JSON-RPC messages (#21497)
This commit is contained in:
parent
05280a7ae3
commit
5883afb3ef
13
rpc/json.go
13
rpc/json.go
@ -202,15 +202,22 @@ func (c *jsonCodec) remoteAddr() string {
|
|||||||
return c.remote
|
return c.remote
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) readBatch() (msg []*jsonrpcMessage, batch bool, err error) {
|
func (c *jsonCodec) readBatch() (messages []*jsonrpcMessage, batch bool, err error) {
|
||||||
// Decode the next JSON object in the input stream.
|
// Decode the next JSON object in the input stream.
|
||||||
// This verifies basic syntax, etc.
|
// This verifies basic syntax, etc.
|
||||||
var rawmsg json.RawMessage
|
var rawmsg json.RawMessage
|
||||||
if err := c.decode(&rawmsg); err != nil {
|
if err := c.decode(&rawmsg); err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
msg, batch = parseMessage(rawmsg)
|
messages, batch = parseMessage(rawmsg)
|
||||||
return msg, batch, nil
|
for i, msg := range messages {
|
||||||
|
if msg == nil {
|
||||||
|
// Message is JSON 'null'. Replace with zero value so it
|
||||||
|
// will be treated like any other invalid message.
|
||||||
|
messages[i] = new(jsonrpcMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return messages, batch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *jsonCodec) writeJSON(ctx context.Context, v interface{}) error {
|
func (c *jsonCodec) writeJSON(ctx context.Context, v interface{}) error {
|
||||||
|
3
rpc/testdata/invalid-batch.js
vendored
3
rpc/testdata/invalid-batch.js
vendored
@ -10,5 +10,8 @@
|
|||||||
--> [1,2,3]
|
--> [1,2,3]
|
||||||
<-- [{"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"}}]
|
<-- [{"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"}}]
|
||||||
|
|
||||||
|
--> [null]
|
||||||
|
<-- [{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}}]
|
||||||
|
|
||||||
--> [{"jsonrpc":"2.0","id":1,"method":"test_echo","params":["foo",1]},55,{"jsonrpc":"2.0","id":2,"method":"unknown_method"},{"foo":"bar"}]
|
--> [{"jsonrpc":"2.0","id":1,"method":"test_echo","params":["foo",1]},55,{"jsonrpc":"2.0","id":2,"method":"unknown_method"},{"foo":"bar"}]
|
||||||
<-- [{"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"}}]
|
<-- [{"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"}}]
|
||||||
|
3
rpc/testdata/invalid-nonobj.js
vendored
3
rpc/testdata/invalid-nonobj.js
vendored
@ -2,3 +2,6 @@
|
|||||||
|
|
||||||
--> 1
|
--> 1
|
||||||
<-- {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}}
|
<-- {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}}
|
||||||
|
|
||||||
|
--> null
|
||||||
|
<-- {"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"invalid request"}}
|
||||||
|
Loading…
Reference in New Issue
Block a user