Skip to content

Commit d6c526d

Browse files
authored
[aggregator] Fix panic on invalid unaggregated payloads (#3041)
1 parent 3ad9380 commit d6c526d

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/metrics/encoding/protobuf/unaggregated_iterator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func (it *unaggregatedIterator) Next() bool {
102102
it.err = fmt.Errorf("decoded message size %d is larger than supported max message size %d", size, it.maxMessageSize)
103103
return false
104104
}
105+
if size <= 0 {
106+
it.err = fmt.Errorf("decoded message size %d is zero or negative", size)
107+
return false
108+
}
105109
it.ensureBufferSize(size)
106110
if err := it.decodeMessage(size); err != nil {
107111
return false

src/metrics/encoding/protobuf/unaggregated_iterator_test.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package protobuf
2222

2323
import (
2424
"bytes"
25+
"encoding/binary"
2526
"io"
2627
"strings"
2728
"testing"
@@ -103,7 +104,7 @@ func TestUnaggregatedIteratorDecodeBatchTimerWithMetadatas(t *testing.T) {
103104
enc := NewUnaggregatedEncoder(NewUnaggregatedOptions())
104105
for _, input := range inputs {
105106
require.NoError(t, enc.EncodeMessage(encoding.UnaggregatedMessageUnion{
106-
Type: encoding.BatchTimerWithMetadatasType,
107+
Type: encoding.BatchTimerWithMetadatasType,
107108
BatchTimerWithMetadatas: input,
108109
}))
109110
}
@@ -195,7 +196,7 @@ func TestUnaggregatedIteratorDecodeForwardedMetricWithMetadata(t *testing.T) {
195196
enc := NewUnaggregatedEncoder(NewUnaggregatedOptions())
196197
for _, input := range inputs {
197198
require.NoError(t, enc.EncodeMessage(encoding.UnaggregatedMessageUnion{
198-
Type: encoding.ForwardedMetricWithMetadataType,
199+
Type: encoding.ForwardedMetricWithMetadataType,
199200
ForwardedMetricWithMetadata: input,
200201
}))
201202
}
@@ -286,7 +287,7 @@ func TestUnaggregatedIteratorDecodeTimedMetricWithMetadata(t *testing.T) {
286287
enc := NewUnaggregatedEncoder(NewUnaggregatedOptions())
287288
for _, input := range inputs {
288289
require.NoError(t, enc.EncodeMessage(encoding.UnaggregatedMessageUnion{
289-
Type: encoding.TimedMetricWithMetadataType,
290+
Type: encoding.TimedMetricWithMetadataType,
290291
TimedMetricWithMetadata: input,
291292
}))
292293
}
@@ -406,7 +407,7 @@ func TestUnaggregatedIteratorDecodeStress(t *testing.T) {
406407
}
407408
case unaggregated.BatchTimerWithMetadatas:
408409
msg = encoding.UnaggregatedMessageUnion{
409-
Type: encoding.BatchTimerWithMetadatasType,
410+
Type: encoding.BatchTimerWithMetadatasType,
410411
BatchTimerWithMetadatas: input,
411412
}
412413
case unaggregated.GaugeWithMetadatas:
@@ -416,17 +417,17 @@ func TestUnaggregatedIteratorDecodeStress(t *testing.T) {
416417
}
417418
case aggregated.ForwardedMetricWithMetadata:
418419
msg = encoding.UnaggregatedMessageUnion{
419-
Type: encoding.ForwardedMetricWithMetadataType,
420+
Type: encoding.ForwardedMetricWithMetadataType,
420421
ForwardedMetricWithMetadata: input,
421422
}
422423
case aggregated.TimedMetricWithMetadata:
423424
msg = encoding.UnaggregatedMessageUnion{
424-
Type: encoding.TimedMetricWithMetadataType,
425+
Type: encoding.TimedMetricWithMetadataType,
425426
TimedMetricWithMetadata: input,
426427
}
427428
case aggregated.PassthroughMetricWithMetadata:
428429
msg = encoding.UnaggregatedMessageUnion{
429-
Type: encoding.PassthroughMetricWithMetadataType,
430+
Type: encoding.PassthroughMetricWithMetadataType,
430431
PassthroughMetricWithMetadata: input,
431432
}
432433
default:
@@ -554,3 +555,20 @@ func TestUnaggregatedIteratorNextOnClose(t *testing.T) {
554555
// Verify that closing a second time is a no op.
555556
it.Close()
556557
}
558+
559+
func TestUnaggregatedIteratorNextOnInvalid(t *testing.T) {
560+
buf := make([]byte, 32)
561+
binary.PutVarint(buf, 0)
562+
stream := bytes.NewReader(buf)
563+
564+
it := NewUnaggregatedIterator(stream, NewUnaggregatedOptions())
565+
require.False(t, it.Next())
566+
require.False(t, it.Next())
567+
568+
buf = make([]byte, 32)
569+
binary.PutVarint(buf, -1234)
570+
stream = bytes.NewReader(buf)
571+
it = NewUnaggregatedIterator(stream, NewUnaggregatedOptions())
572+
require.False(t, it.Next())
573+
require.False(t, it.Next())
574+
}

0 commit comments

Comments
 (0)