Skip to content

Commit 012ee25

Browse files
Bound buffer for reading stats (#14542)
Co-authored-by: Evan Anderson <[email protected]>
1 parent 4ff7168 commit 012ee25

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pkg/autoscaler/metrics/http_scrape_client.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ func statFromProto(body io.Reader) (Stat, error) {
8282
b := pool.Get().(*bytes.Buffer)
8383
b.Reset()
8484
defer pool.Put(b)
85-
_, err := b.ReadFrom(body)
85+
// 6 8-byte fields (+2 bytes marshalling), one hostname, 20 bytes extra space
86+
r := io.LimitedReader{R: body, N: 6*10 + 256 + 20}
87+
_, err := b.ReadFrom(&r)
8688
if err != nil {
8789
return emptyStat, fmt.Errorf("reading body failed: %w", err)
8890
}

pkg/autoscaler/metrics/http_scrape_client_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ func TestHTTPScrapeClientScrapeProtoErrorCases(t *testing.T) {
9595
responseCode: http.StatusOK,
9696
responseType: "text/html",
9797
expectedErr: errUnsupportedMetricType.Error(),
98+
}, {
99+
name: "LongStat",
100+
responseCode: http.StatusOK,
101+
responseType: "application/protobuf",
102+
stat: Stat{
103+
// We don't expect PodName to be 600 characters long
104+
PodName: strings.Repeat("a123456789", 60),
105+
AverageConcurrentRequests: 1.1,
106+
AverageProxiedConcurrentRequests: 1.1,
107+
RequestCount: 33.2,
108+
ProxiedRequestCount: 33.2,
109+
ProcessUptime: 12345.678,
110+
Timestamp: 1697431278,
111+
},
112+
expectedErr: "unmarshalling failed: unexpected EOF",
98113
}}
99114

100115
for _, test := range testCases {

0 commit comments

Comments
 (0)