Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for nil before returning invalid json in rpc streaming calls #7104

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ func (h *handler) runMethod(ctx context.Context, msg *jsonrpcMessage, callb *cal
stream.WriteObjectField("result")
_, err := callb.call(ctx, msg.Method, args, stream)
if err != nil {
stream.WriteNil()
writeNilIfNotPresent(stream)
stream.WriteMore()
HandleError(err, stream)
}
Expand All @@ -519,6 +519,29 @@ func (h *handler) runMethod(ctx context.Context, msg *jsonrpcMessage, callb *cal
return nil
}

var nullAsBytes = []byte{110, 117, 108, 108}

// there are many avenues that could lead to an error being handled in runMethod, so we need to check
// if nil has already been written to the stream before writing it again here
func writeNilIfNotPresent(stream *jsoniter.Stream) {
b := stream.Buffer()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. if len of b < 4 then hasNil is true. Is it bug (or i miss-understand)?
  2. It must work in most cases, but what if buffer already was flushed to client. Unlikely - in case of error - json is small. Maybe it’s fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. good catch! I've added an else there now to set it to false. If we've not got at least 4 bytes we can't possibly have written null.
  2. I think in the case of a flush already having happened we've got more serious problems as we can't backtrack on that to the client. Probably need to deal with those in a case by case basis if we ever found one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hexoscott do we need check “stream” variable for nil?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case we've already used stream before this call so can safely assume it won't be. But nothing stopping other code calling this where it could be nil. I'll add the check into another PR shortly.

hasNil := true
if len(b) >= 4 {
b = b[len(b)-4:]
for i, v := range nullAsBytes {
if v != b[i] {
hasNil = false
break
}
}
} else {
hasNil = false
}
if !hasNil {
stream.WriteNil()
}
}

// unsubscribe is the callback function for all *_unsubscribe calls.
func (h *handler) unsubscribe(ctx context.Context, id ID) (bool, error) {
h.subLock.Lock()
Expand Down
84 changes: 84 additions & 0 deletions rpc/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package rpc

import (
"bytes"
"context"
"fmt"
"reflect"
"testing"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
)

func TestHandlerDoesNotDoubleWriteNull(t *testing.T) {

tests := map[string]struct {
params []byte
expected string
}{
"error_with_stream_write": {
params: []byte("[1]"),
expected: `{"jsonrpc":"2.0","id":1,"result":null,"error":{"code":-32000,"message":"id 1"}}`,
},
"error_without_stream_write": {
params: []byte("[2]"),
expected: `{"jsonrpc":"2.0","id":1,"result":null,"error":{"code":-32000,"message":"id 2"}}`,
},
"no_error": {
params: []byte("[3]"),
expected: `{"jsonrpc":"2.0","id":1,"result":{}}`,
},
}

for name, testParams := range tests {
t.Run(name, func(t *testing.T) {
msg := jsonrpcMessage{
Version: "2.0",
ID: []byte{49},
Method: "test_test",
Params: testParams.params,
Error: nil,
Result: nil,
}

dummyFunc := func(id int, stream *jsoniter.Stream) error {
if id == 1 {
stream.WriteNil()
return fmt.Errorf("id 1")
}
if id == 2 {
return fmt.Errorf("id 2")
}
stream.WriteEmptyObject()
return nil
}

var arg1 int
cb := &callback{
fn: reflect.ValueOf(dummyFunc),
rcvr: reflect.Value{},
argTypes: []reflect.Type{reflect.TypeOf(arg1)},
hasCtx: false,
errPos: 0,
isSubscribe: false,
streamable: true,
}

args, err := parsePositionalArguments((msg).Params, cb.argTypes)
if err != nil {
t.Fatal(err)
}

var buf bytes.Buffer
stream := jsoniter.NewStream(jsoniter.ConfigDefault, &buf, 4096)

h := handler{}
h.runMethod(context.Background(), &msg, cb, args, stream)

output := buf.String()
assert.Equal(t, testParams.expected, output, "expected output should match")
})
}

}