Skip to content

Commit

Permalink
Bound buffer for reading stats (#14542)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan Anderson <[email protected]>
  • Loading branch information
knative-prow-robot and evankanderson authored Oct 19, 2023
1 parent 4ff7168 commit 012ee25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/autoscaler/metrics/http_scrape_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ func statFromProto(body io.Reader) (Stat, error) {
b := pool.Get().(*bytes.Buffer)
b.Reset()
defer pool.Put(b)
_, err := b.ReadFrom(body)
// 6 8-byte fields (+2 bytes marshalling), one hostname, 20 bytes extra space
r := io.LimitedReader{R: body, N: 6*10 + 256 + 20}
_, err := b.ReadFrom(&r)
if err != nil {
return emptyStat, fmt.Errorf("reading body failed: %w", err)
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/autoscaler/metrics/http_scrape_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ func TestHTTPScrapeClientScrapeProtoErrorCases(t *testing.T) {
responseCode: http.StatusOK,
responseType: "text/html",
expectedErr: errUnsupportedMetricType.Error(),
}, {
name: "LongStat",
responseCode: http.StatusOK,
responseType: "application/protobuf",
stat: Stat{
// We don't expect PodName to be 600 characters long
PodName: strings.Repeat("a123456789", 60),
AverageConcurrentRequests: 1.1,
AverageProxiedConcurrentRequests: 1.1,
RequestCount: 33.2,
ProxiedRequestCount: 33.2,
ProcessUptime: 12345.678,
Timestamp: 1697431278,
},
expectedErr: "unmarshalling failed: unexpected EOF",
}}

for _, test := range testCases {
Expand Down

0 comments on commit 012ee25

Please sign in to comment.