Skip to content

Commit

Permalink
Fix RTP padding length validation
Browse files Browse the repository at this point in the history
Added validation of RTP padding length in received packets. Also check
for zero padding length when marshaling.
  • Loading branch information
[email protected] authored and [email protected] committed Jul 6, 2024
1 parent bc5124c commit 7f81103
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ var (
errRFC8285TwoByteHeaderSize = errors.New("header extension payload must be 255bytes or less for RFC 5285 two byte extensions")

errRFC3550HeaderIDRange = errors.New("header extension id must be 0 for non-RFC 5285 extensions")

errInvalidRTPPadding = errors.New("invalid RTP padding")
)
10 changes: 10 additions & 0 deletions packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,13 @@ func (p *Packet) Unmarshal(buf []byte) error {

end := len(buf)
if p.Header.Padding {
if end <= n {
return errTooSmall

Check warning on line 219 in packet.go

View check run for this annotation

Codecov / codecov/patch

packet.go#L219

Added line #L219 was not covered by tests
}
p.PaddingSize = buf[end-1]
if p.PaddingSize == 0 || end < n+int(p.PaddingSize) {
return errTooSmall
}
end -= int(p.PaddingSize)
}
if end < n {
Expand Down Expand Up @@ -475,6 +481,10 @@ func (p Packet) Marshal() (buf []byte, err error) {

// MarshalTo serializes the packet and writes to the buffer.
func (p *Packet) MarshalTo(buf []byte) (n int, err error) {
if p.Header.Padding && p.PaddingSize == 0 {
return 0, errInvalidRTPPadding

Check warning on line 485 in packet.go

View check run for this annotation

Codecov / codecov/patch

packet.go#L485

Added line #L485 was not covered by tests
}

n, err = p.Header.MarshalTo(buf)
if err != nil {
return 0, err
Expand Down

0 comments on commit 7f81103

Please sign in to comment.