Skip to content

Commit bd07714

Browse files
committed
fix: decode message more generically to help avoid known parsing problems
1 parent 537bbcc commit bd07714

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

protocol/protocol.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/cloudstruct/go-ouroboros-network/muxer"
77
"github.com/cloudstruct/go-ouroboros-network/utils"
8+
"github.com/fxamacker/cbor/v2"
89
"io"
910
"sync"
1011
)
@@ -242,9 +243,10 @@ func (p *Protocol) recvLoop() {
242243
leftoverData = false
243244
// Wait until ready to receive based on state map
244245
<-p.recvReadyChan
245-
// Decode message into generic list until we can determine what type of message it is
246-
// This also lets us determine how many bytes the message is
247-
var tmpMsg []interface{}
246+
// Decode message into generic list until we can determine what type of message it is.
247+
// This also lets us determine how many bytes the message is. We use RawMessage here to
248+
// avoid parsing things that we may not be able to parse
249+
var tmpMsg []cbor.RawMessage
248250
numBytesRead, err := utils.CborDecode(p.recvBuffer.Bytes(), &tmpMsg)
249251
if err != nil {
250252
if err == io.EOF && p.recvBuffer.Len() > 0 {
@@ -256,8 +258,12 @@ func (p *Protocol) recvLoop() {
256258
p.SendError(fmt.Errorf("%s: decode error: %s", p.config.Name, err))
257259
return
258260
}
261+
// Decode first list item to determine message type
262+
var msgType uint
263+
if _, err := utils.CborDecode(tmpMsg[0], &msgType); err != nil {
264+
p.SendError(fmt.Errorf("%s: decode error: %s", p.config.Name, err))
265+
}
259266
// Create Message object from CBOR
260-
msgType := uint(tmpMsg[0].(uint64))
261267
msgData := p.recvBuffer.Bytes()[:numBytesRead]
262268
msg, err := p.config.MessageFromCborFunc(msgType, msgData)
263269
if err != nil {

0 commit comments

Comments
 (0)