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

fix(dot/subscription): check websocket message from untrusted data #2527

Merged
merged 15 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion dot/rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (h *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
wsc := NewWSConn(ws, h.serverConfig)
h.wsConns = append(h.wsConns, wsc)

go wsc.HandleComm()
go wsc.HandleConn()
}

// NewWSConn to create new WebSocket Connection struct
Expand Down
19 changes: 9 additions & 10 deletions dot/rpc/subscription/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,27 @@ type WSConn struct {
}

// readWebsocketMessage will read and parse the message data to a string->interface{} data
func (c *WSConn) readWebsocketMessage() ([]byte, *websocketMessage, error) {
_, mbytes, err := c.Wsconn.ReadMessage()
func (c *WSConn) readWebsocketMessage() (bytes []byte, msg websocketMessage, err error) {
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
_, bytes, err = c.Wsconn.ReadMessage()
if err != nil {
logger.Debugf("websocket failed to read message: %s", err)
return nil, nil, errCannotReadFromWebsocket
return bytes, msg, errCannotReadFromWebsocket
}

logger.Tracef("websocket message received: %s", string(mbytes))
logger.Tracef("websocket message received: %s", string(bytes))

msg := new(websocketMessage)
err = json.Unmarshal(mbytes, &msg)
err = json.Unmarshal(bytes, &msg)

if err != nil {
logger.Debugf("websocket failed to unmarshal request message: %s", err)
return nil, nil, errCannotUnmarshalMessage
return bytes, msg, errCannotUnmarshalMessage
}

return mbytes, msg, nil
return bytes, msg, nil
}

//HandleComm handles messages received on websocket connections
func (c *WSConn) HandleComm() {
//HandleConn handles messages received on websocket connections
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
func (c *WSConn) HandleConn() {
for {
mbytes, msg, err := c.readWebsocketMessage()
if errors.Is(err, errCannotReadFromWebsocket) {
Expand Down
8 changes: 4 additions & 4 deletions dot/rpc/subscription/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestWSConn_CheckWebsocketInvalidData(t *testing.T) {
wsconn.Subscriptions = make(map[uint32]Listener)
defer cancel()

go wsconn.HandleComm()
go wsconn.HandleConn()

tests := []struct {
sentMessage []byte
Expand Down Expand Up @@ -68,12 +68,12 @@ func TestWSConn_CheckWebsocketInvalidData(t *testing.T) {
}
}

func TestWSConn_HandleComm(t *testing.T) {
func TestWSConn_HandleConn(t *testing.T) {
wsconn, c, cancel := setupWSConn(t)
wsconn.Subscriptions = make(map[uint32]Listener)
defer cancel()

go wsconn.HandleComm()
go wsconn.HandleConn()
time.Sleep(time.Second * 2)

// test storageChangeListener
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestSubscribeAllHeads(t *testing.T) {
wsconn.Subscriptions = make(map[uint32]Listener)
defer cancel()

go wsconn.HandleComm()
go wsconn.HandleConn()
time.Sleep(time.Second * 2)

_, err := wsconn.initAllBlocksListerner(1, nil)
Expand Down