Skip to content

Commit bd5cb0d

Browse files
authored
fix: guard muxer segment creation from oversized payload (#895)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 8b2690c commit bd5cb0d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

muxer/segment.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Blink Labs Software
1+
// Copyright 2025 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
package muxer
1616

1717
import (
18+
"math"
1819
"time"
1920
)
2021

@@ -45,14 +46,21 @@ type Segment struct {
4546
// NewSegment returns a new Segment given a protocol ID, payload bytes, and whether the segment
4647
// is a response
4748
func NewSegment(protocolId uint16, payload []byte, isResponse bool) *Segment {
49+
// time since unix epoch even as nanoseconds will not overflow soon
50+
// #nosec G115
4851
header := SegmentHeader{
4952
Timestamp: uint32(time.Now().UnixNano() & 0xffffffff),
5053
ProtocolId: protocolId,
5154
}
5255
if isResponse {
5356
header.ProtocolId = header.ProtocolId + segmentProtocolIdResponseFlag
5457
}
55-
header.PayloadLength = uint16(len(payload))
58+
size := len(payload)
59+
if size > SegmentMaxPayloadLength || size > math.MaxUint16 {
60+
return nil
61+
}
62+
// payload size fits within length
63+
header.PayloadLength = uint16(size)
5664
segment := &Segment{
5765
SegmentHeader: header,
5866
Payload: payload,

0 commit comments

Comments
 (0)