Skip to content

Bound the metrics response body size - #305

Open
benthecarman wants to merge 1 commit into
lightningdevkit:mainfrom
benthecarman:fix-15-bound-metrics-response
Open

benthecarman wants to merge 1 commit into
lightningdevkit:mainfrom
benthecarman:fix-15-bound-metrics-response

Conversation

@benthecarman

Copy link
Copy Markdown
Collaborator

get_metrics_with_auth read the full metrics response with response.bytes() and no size limit, so a malicious or impersonated server could exhaust client memory with an endless body. The extra to_vec() copy also doubled peak memory.

This caps the metrics body at 10 MiB, matching the unary gRPC limit. The response is rejected early if its Content-Length is over the cap, and the chunked read stops as soon as the running total goes over it. The collected bytes are decoded with String::from_utf8 without another copy.

Found by Project Loupe

get_metrics_with_auth read the whole metrics response with no size
limit, so a malicious or impersonated server could send an endless
body and run the client out of memory. The extra to_vec() copy also
doubled peak memory.

Cap the metrics body at 10 MiB. The response is rejected early if its
Content-Length is too large, and the read stops as soon as the streamed
total goes over the cap. The collected bytes are decoded as UTF-8
without another copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ldk-reviews-bot

ldk-reviews-bot commented Oct 7, 2026 •

Copy link
Copy Markdown

I've assigned @TheBlueMatt as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@Anyitechs Anyitechs 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.

Looks good, thank you!

// constrained by this limit.
const MAX_GRPC_STREAM_MESSAGE_LEN: usize = 4 * 1024 * 1024;

// Applies to the complete Prometheus metrics response body.

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.

Should we update the docs for this as well? Prometheus exposes a body_size_limit field in the config file that users can use to limit the number of bytes acceptable in the response body per scrape. Given that this change supersedes that, I believe it's worth documenting.

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.

3 participants