mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
Check gossip message decoded length before decoding (#6663)
* Check gossip message decoded length before decoding * gofmt * Merge refs/heads/master into snappy-decodedlen-check * Merge refs/heads/master into snappy-decodedlen-check * Merge refs/heads/master into snappy-decodedlen-check * @nisdas feedback
This commit is contained in:
parent
367738e83b
commit
784f4169ef
@ -99,14 +99,14 @@ func (e SszNetworkEncoder) doDecode(b []byte, to interface{}) error {
|
||||
|
||||
// DecodeGossip decodes the bytes to the protobuf gossip message provided.
|
||||
func (e SszNetworkEncoder) DecodeGossip(b []byte, to interface{}) error {
|
||||
var err error
|
||||
size, err := snappy.DecodedLen(b)
|
||||
if uint64(size) > MaxGossipSize {
|
||||
return errors.Errorf("gossip message exceeds max gossip size: %d bytes > %d bytes", size, MaxGossipSize)
|
||||
}
|
||||
b, err = snappy.Decode(nil /*dst*/, b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if uint64(len(b)) > MaxGossipSize {
|
||||
return errors.Errorf("gossip message exceeds max gossip size: %d bytes > %d bytes", len(b), MaxGossipSize)
|
||||
}
|
||||
return e.doDecode(b, to)
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package encoder_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
@ -19,10 +20,13 @@ func TestSszNetworkEncoder_RoundTrip(t *testing.T) {
|
||||
testRoundTripWithGossip(t, e)
|
||||
}
|
||||
|
||||
func TestSszNetworkEncoder_RoundTrip_Snappy(t *testing.T) {
|
||||
func TestSszNetworkEncoder_FailsSnappyLength(t *testing.T) {
|
||||
e := &encoder.SszNetworkEncoder{}
|
||||
testRoundTripWithLength(t, e)
|
||||
testRoundTripWithGossip(t, e)
|
||||
att := &testpb.TestSimpleMessage{}
|
||||
data := make([]byte, 32)
|
||||
binary.PutUvarint(data, encoder.MaxGossipSize+32)
|
||||
err := e.DecodeGossip(data, att)
|
||||
require.ErrorContains(t, "gossip message exceeds max gossip size", err)
|
||||
}
|
||||
|
||||
func testRoundTripWithLength(t *testing.T, e *encoder.SszNetworkEncoder) {
|
||||
|
Loading…
Reference in New Issue
Block a user