Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metrics-batch-str-data-index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"github.com/livekit/protocol": patch
---

`utils.MetricsBatchBuilder.Merge` rejects out-of-range `str_data` indexes on 32-bit builds.
4 changes: 2 additions & 2 deletions utils/metrics_batch_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (m *MetricsBatchBuilder) translateStrDataIndex(strData []string, index uint
}

baseIdx := index - uint32(livekit.MetricLabel_METRIC_LABEL_PREDEFINED_MAX_VALUE)
if len(strData) <= int(baseIdx) {
if uint64(len(strData)) <= uint64(baseIdx) {
return 0, false
}

Expand All @@ -325,7 +325,7 @@ func getStrDataForIndex(mb *livekit.MetricsBatch, index uint32) (string, bool) {
}

baseIdx := index - uint32(livekit.MetricLabel_METRIC_LABEL_PREDEFINED_MAX_VALUE)
if len(mb.StrData) <= int(baseIdx) {
if uint64(len(mb.StrData)) <= uint64(baseIdx) {
return "", false
}

Expand Down
27 changes: 27 additions & 0 deletions utils/metrics_batch_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,31 @@ func TestMetricsBatchBuilder(t *testing.T) {
mb := mbb.ToProto()
require.True(t, proto.Equal(expected, mb))
})

t.Run("merge str data index beyond int32", func(t *testing.T) {
// index past the end must be rejected even where int is 32 bits
const huge = ^uint32(0)
mbb := NewMetricsBatchBuilder()
require.NotPanics(t, func() {
mbb.Merge(&livekit.MetricsBatch{
StrData: []string{"PA"},
TimeSeries: []*livekit.TimeSeriesMetric{
{Label: huge, ParticipantIdentity: huge, TrackSid: huge, Rid: huge},
{Label: uint32(livekit.MetricLabel_SUBSCRIBER_RTT), ParticipantIdentity: huge},
},
Events: []*livekit.EventMetric{
{Label: huge, ParticipantIdentity: huge, TrackSid: huge, Rid: huge},
{Label: uint32(livekit.MetricLabel_SUBSCRIBER_RTT), ParticipantIdentity: huge},
},
})
})
mb := mbb.ToProto()
require.Len(t, mb.TimeSeries, 2)
require.Len(t, mb.Events, 2)
require.Empty(t, mb.StrData)
for _, ts := range mb.TimeSeries {
require.Zero(t, ts.ParticipantIdentity)
require.Zero(t, ts.TrackSid)
}
})
}
Loading