Skip to content

Commit 49c08b8

Browse files
committed
fix: fix payload overflow
1 parent 25aed33 commit 49c08b8

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

client/finalizer/payload_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func ExtractPayloads(data []byte) ([]payloadMessage, error) {
3434
protocol := data[0] // 1 byte protocol ID
3535
votingRound := binary.BigEndian.Uint32(data[1:5]) // 4 bytes votingRoundID
3636
length := binary.BigEndian.Uint16(data[5:7]) // 2 bytes length of payload in bytes
37-
end := 7 + length
38-
if len(data) < int(end) {
37+
end := 7 + int(length)
38+
if len(data) < end {
3939
return nil, errors.New("wrongly formatted tx input")
4040
}
4141

client/finalizer/payload_utils_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@ func TestRoundTripWithEncodePayload(t *testing.T) {
197197
}
198198
}
199199

200-
// TestExtractPayloadsZeroLength confirms ExtractPayloads accepts a header with
201-
// length=0 and that FromSignedPayload rejects the resulting empty payload
202-
// rather than panicking.
203200
func TestExtractPayloadsZeroLength(t *testing.T) {
204201
// 4-byte selector + 1-byte protocol + 4-byte votingRound + 2-byte length=0
205202
data := make([]byte, 4+1+4+2)
@@ -213,3 +210,21 @@ func TestExtractPayloadsZeroLength(t *testing.T) {
213210
var s submitSignaturesPayload
214211
require.Error(t, s.FromSignedPayload(payloads[0]))
215212
}
213+
214+
func TestExtractUint16Overflow(t *testing.T) {
215+
// 4-byte selector + 1-byte protocol + 4-byte votingRound + 2-byte length=0
216+
data := make([]byte, 4+1+4+2)
217+
binary.BigEndian.PutUint32(data[5:9], 1)
218+
binary.BigEndian.PutUint16(data[9:11], 0xffff)
219+
220+
_, err := ExtractPayloads(data)
221+
require.Error(t, err)
222+
223+
dataTrue := make([]byte, 4+1+4+2+0xffff)
224+
binary.BigEndian.PutUint32(dataTrue[5:9], 1)
225+
binary.BigEndian.PutUint16(dataTrue[9:11], 0xffff)
226+
227+
payloads, err := ExtractPayloads(dataTrue)
228+
require.NoError(t, err)
229+
require.Len(t, payloads, 1)
230+
}

0 commit comments

Comments
 (0)