Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Malicious JSON RPC request unit test #1884

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
1 change: 0 additions & 1 deletion jsonrpc/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ func (j *JSONRPC) handleJSONRPCRequest(w http.ResponseWriter, req *http.Request)
j.logger.Debug("handle", "request", string(data))

resp, err := j.dispatcher.Handle(data)

if err != nil {
_, _ = w.Write([]byte(err.Error()))
} else {
Expand Down
89 changes: 68 additions & 21 deletions jsonrpc/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,20 @@ import (
"bytes"
"encoding/json"
"net"
"net/http/httptest"
"strings"
"testing"

"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"

"github.com/0xPolygon/polygon-edge/helper/tests"
"github.com/0xPolygon/polygon-edge/versioning"
"github.com/stretchr/testify/assert"

"github.com/hashicorp/go-hclog"
)

func TestHTTPServer(t *testing.T) {
store := newMockStore()
port, portErr := tests.GetFreePort()

if portErr != nil {
t.Fatalf("Unable to fetch free port, %v", portErr)
}

config := &Config{
Store: store,
Addr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: port},
}
_, err := NewJSONRPC(hclog.NewNullLogger(), config)

if err != nil {
t.Fatal(err)
}
_, err := newTestJSONRPC(t)
require.NoError(t, err)
}

func Test_handleGetRequest(t *testing.T) {
Expand All @@ -51,12 +39,12 @@ func Test_handleGetRequest(t *testing.T) {

response := &GetResponse{}

assert.NoError(
require.NoError(
t,
json.Unmarshal(mockWriter.Bytes(), response),
)

assert.Equal(
require.Equal(
t,
&GetResponse{
Name: chainName,
Expand All @@ -66,3 +54,62 @@ func Test_handleGetRequest(t *testing.T) {
response,
)
}

func TestJSONRPC_handleJSONRPCRequest(t *testing.T) {
t.Parallel()

j, err := newTestJSONRPC(t)
require.NoError(t, err)

cases := []struct {
name string
request string
expectedResponse string
}{
{
name: "malicious input (XSS attack)",
request: `{"jsonrpc":"2.0","id":0,"method":"eth_getBlockByNumber","params":["latest",false]}
<script>alert("XSS attack");</script>`,
expectedResponse: `{"jsonrpc":"2.0","error":{"code":-32600,"message":"Invalid json request"}}`,
},
{
name: "valid input",
request: `{"jsonrpc":"2.0","id":0,"method":"eth_getBlockByNumber","params":["latest",false]}`,
expectedResponse: `{"jsonrpc":"2.0","id":0,"result":{`,
},
}

for _, c := range cases {
c := c

t.Run(c.name, func(t *testing.T) {
t.Parallel()

req := httptest.NewRequest("POST", "/eth_getBlockByNumber", strings.NewReader(c.request))
req.Header.Set("Content-Type", "application/json")

w := httptest.NewRecorder()

j.handleJSONRPCRequest(w, req)

response := w.Body.String()
require.Contains(t, response, c.expectedResponse)
})
}
}

func newTestJSONRPC(t *testing.T) (*JSONRPC, error) {
t.Helper()

store := newMockStore()

port, err := tests.GetFreePort()
require.NoError(t, err, "Unable to fetch free port, %v", err)

config := &Config{
Store: store,
Addr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: port},
}

return NewJSONRPC(hclog.NewNullLogger(), config)
}
Loading