diff --git a/packet.go b/packet.go index d7a5e08..8a91bc6 100644 --- a/packet.go +++ b/packet.go @@ -239,6 +239,9 @@ func (p *Packet) Unmarshal(buf []byte) error { return errTooSmall } p.Header.PaddingSize = buf[end-1] + if p.Header.PaddingSize == 0 { + return errInvalidRTPPadding + } end -= int(p.Header.PaddingSize) } else { p.Header.PaddingSize = 0 diff --git a/packet_test.go b/packet_test.go index 9c8138e..a868df7 100644 --- a/packet_test.go +++ b/packet_test.go @@ -143,6 +143,14 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop assert.Equal(t, packet, parsedPacket) assert.Len(t, packet.Payload, 0, "Unmarshal of padding only packet has payload of non-zero length") + // packet with padding bit set but zero padding byte + rawPkt = []byte{ + 0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64, + 0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x36, 0xbe, 0x88, 0x00, + } + err := packet.Unmarshal(rawPkt) + assert.ErrorIs(t, err, errInvalidRTPPadding) + // packet with excessive padding rawPkt = []byte{ 0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64, @@ -168,7 +176,7 @@ func TestBasic(t *testing.T) { // nolint:maintidx,cyclop }, Payload: []byte{}, } - err := packet.Unmarshal(rawPkt) + err = packet.Unmarshal(rawPkt) assert.Error(t, err, "Unmarshal did not error on packet with excessive padding") assert.ErrorIs(t, err, errTooSmall)