Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bound buffer for reading stats #14523

Merged
merged 1 commit into from
Oct 16, 2023
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
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}
Copy link
Contributor

@skonto skonto Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should not this be 255 for the max hostname? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra-nit: you might need two bytes for the tag + length if the string is actually 256 bytes long, plus another possible tag + length for the entire message.

Proto doesn't have a way to express / enforce that a string is less than a certain size. The goal here is simply to avoid accidentally reading e.g. 5MB or more into an in-memory buffer when we know the actual report should be less than 1KB.

Copy link
Contributor

@skonto skonto Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity (haven't checked), what about using grpc instead?

_, 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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: any way to distinguish we hit the limit vs EOF on the wire?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not easily. Technically, we could also end up skipping a bunch of extra garbage after a valid proto, but since the proto contains a length at the start, we can tell whether we read the whole proto or not just by reading the beginning. (In fact, it would be nice if the generated proto code had a method that read from an io.Reader, but it doesn't seem to.)

}}

for _, test := range testCases {
Expand Down
Loading