Skip to content

Enforce response body limits in kubelet-to-gcm#1201

Merged
courageJ merged 1 commit into
GoogleCloudPlatform:masterfrom
courageJ:fix-response-body-limits
May 26, 2026
Merged

Enforce response body limits in kubelet-to-gcm#1201
courageJ merged 1 commit into
GoogleCloudPlatform:masterfrom
courageJ:fix-response-body-limits

Conversation

@courageJ

Copy link
Copy Markdown
Contributor

This PR implements size limits when reading HTTP response bodies in the kubelet-to-gcm monitoring daemon to prevent potential unbounded memory allocation.

Key changes:

  • Added io.LimitReader to getGCEMetaData, doRequestAndParse, and doRequestAndUnmarshal.
  • Set specific limits: 10MB for GCE metadata and Prometheus metrics, 50MB for Kubelet stats summaries.
  • Replaced deprecated ioutil.ReadAll with io.ReadAll.
  • Added unit tests in monitor/controller and monitor/kubelet to verify size limiting behavior and ensure success cases continue to work.

Tested locally with go test ./monitor/... in kubelet-to-gcm.

@courageJ courageJ requested a review from erain May 21, 2026 12:13
@courageJ courageJ force-pushed the fix-response-body-limits branch from 9e09848 to d0d9650 Compare May 21, 2026 12:20

@erain erain left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The implementation caps allocation, but it does not actually enforce the response-size limit. io.LimitReader returns EOF after maxResponseBodySize, so callers cannot tell whether the server sent exactly the allowed size or much more. That can silently accept truncated GCE metadata and can silently drop controller metrics if the first limited chunk is parseable.

Please read at most maxResponseBodySize+1 bytes or use an io.LimitedReader and explicitly return an error when the body exceeds the configured limit. Apply the same helper to monitor/config/initialize.go, monitor/controller/client.go, and monitor/kubelet/client.go. The tests should include oversized-but-otherwise-valid responses and assert the explicit size-limit error, not just parse failures from invalid truncated data.

PROMPT for coding agent: Fix PR #1201 by replacing every io.ReadAll(io.LimitReader(..., maxResponseBodySize)) call with a shared helper that detects bodies larger than the limit and returns an error. Use it for GCE metadata, controller metrics, and kubelet summaries. Update or add unit tests so oversized valid payloads fail with the size-limit error, while under-limit payloads still pass.

- Added util.ReadWithLimit to detect and error when response bodies exceed limits.
- Replaced silent truncation in controller, kubelet, and config packages.
- Added comprehensive unit tests for size limit enforcement and explicit error return.
@courageJ courageJ force-pushed the fix-response-body-limits branch from d0d9650 to 9837a33 Compare May 25, 2026 18:06

@erain erain left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Re-reviewed the updated response-body limit implementation. The new shared util.ReadWithLimit reads limit+1 bytes and returns an explicit ErrBodyTooLarge, which addresses the previous truncation concern, and the callers in metadata, controller, and kubelet paths now use it. Ran go test ./monitor/... from kubelet-to-gcm on the PR head. Looks good.

@courageJ courageJ merged commit b87a7ed into GoogleCloudPlatform:master May 26, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants