Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 35003cc

Browse files
millkendustinxie
authored andcommittedApr 17, 2024
[api]fix JsonRPC api response id type doesn't match request type (#4168)
1 parent ecb2faf commit 35003cc

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed
 

‎api/web3server.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,19 @@ func (svr *web3Handler) handleWeb3Req(ctx context.Context, web3Req *gjson.Result
243243
} else {
244244
log.Logger("api").Debug("web3Debug", zap.String("response", fmt.Sprintf("%+v", res)))
245245
}
246+
var id any
247+
reqID := web3Req.Get("id")
248+
switch reqID.Type {
249+
case gjson.String:
250+
id = reqID.String()
251+
case gjson.Number:
252+
id = reqID.Int()
253+
default:
254+
id = 0
255+
res, err = nil, errors.New("invalid id type")
256+
}
246257
size, err1 = writer.Write(&web3Response{
247-
id: int(web3Req.Get("id").Int()),
258+
id: id,
248259
result: res,
249260
err: err,
250261
})

‎api/web3server_marshal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424

2525
type (
2626
web3Response struct {
27-
id int
27+
id any
2828
result interface{}
2929
err error
3030
}
@@ -95,7 +95,7 @@ func (obj *web3Response) MarshalJSON() ([]byte, error) {
9595
if obj.err == nil {
9696
return json.Marshal(&struct {
9797
Jsonrpc string `json:"jsonrpc"`
98-
ID int `json:"id"`
98+
ID any `json:"id"`
9999
Result interface{} `json:"result"`
100100
}{
101101
Jsonrpc: "2.0",
@@ -117,7 +117,7 @@ func (obj *web3Response) MarshalJSON() ([]byte, error) {
117117

118118
return json.Marshal(&struct {
119119
Jsonrpc string `json:"jsonrpc"`
120-
ID int `json:"id"`
120+
ID any `json:"id"`
121121
Error errMessage `json:"error"`
122122
}{
123123
Jsonrpc: "2.0",

‎api/web3server_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,3 +1266,37 @@ func TestDebugTraceCall(t *testing.T) {
12661266
require.Empty(rlt.Revert)
12671267
require.Equal(0, len(rlt.StructLogs))
12681268
}
1269+
1270+
func TestResponseIDMatchTypeWithRequest(t *testing.T) {
1271+
require := require.New(t)
1272+
ctrl := gomock.NewController(t)
1273+
defer ctrl.Finish()
1274+
core := mock_apicoreservice.NewMockCoreService(ctrl)
1275+
core.EXPECT().TipHeight().Return(uint64(1)).AnyTimes()
1276+
core.EXPECT().Track(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return().AnyTimes()
1277+
svr := newHTTPHandler(NewWeb3Handler(core, "", _defaultBatchRequestLimit))
1278+
getServerResp := func(svr *hTTPHandler, req *http.Request) *httptest.ResponseRecorder {
1279+
req.Header.Set("Content-Type", "application/json")
1280+
resp := httptest.NewRecorder()
1281+
svr.ServeHTTP(resp, req)
1282+
return resp
1283+
}
1284+
tests := []struct {
1285+
req string
1286+
sub string
1287+
}{
1288+
{`{"jsonrpc":"2.0","method":"eth_blockNumber","id":1}`, `"id":1`},
1289+
{`{"jsonrpc":"2.0","method":"eth_blockNumber","id":"1"}`, `"id":"1"`},
1290+
{`{"jsonrpc":"2.0","method":"eth_blockNumber","id":"0x32"}`, `"id":"0x32"`},
1291+
{`{"jsonrpc":"2.0","method":"eth_blockNumber","id":[]}`, `error`},
1292+
{`{"jsonrpc":"2.0","method":"eth_blockNumber","id":[1]}`, `error`},
1293+
{`{"jsonrpc":"2.0","method":"eth_blockNumber","id":0x32}`, `error`},
1294+
{`{"jsonrpc":"2.0","method":"eth_blockNumber","id":{1}}`, `error`},
1295+
}
1296+
for _, tt := range tests {
1297+
request, _ := http.NewRequest(http.MethodPost, "http://url.com", strings.NewReader(tt.req))
1298+
response := getServerResp(svr, request)
1299+
bodyBytes, _ := io.ReadAll(response.Body)
1300+
require.Contains(string(bodyBytes), tt.sub)
1301+
}
1302+
}

0 commit comments

Comments
 (0)
Please sign in to comment.