Limit HTTP request bodies before MCP middleware parsing - #3111
Open
SamMorrowDrums wants to merge 3 commits into
Open
Limit HTTP request bodies before MCP middleware parsing#3111SamMorrowDrums wants to merge 3 commits into
SamMorrowDrums wants to merge 3 commits into
Conversation
Add WithMaxBodySize middleware that bounds the request body via http.MaxBytesReader (with a fast Content-Length rejection when known), registered first in RegisterMiddleware so it runs before any other middleware or the MCP SDK reads or buffers the body. WithMCPParse and WithScopeChallenge now return a clear 413 "request body too large" response when their body read hits the limit, instead of silently continuing. Defaults to 10 MiB, overridable via ServerConfig.MaxRequestBodyBytes. Fixes #3102 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an early, configurable HTTP request-body limit to prevent unbounded buffering.
Changes:
- Adds
WithMaxBodySizewith a 10 MiB default. - Returns HTTP 413 for oversized bodies during middleware parsing.
- Adds unit and integration coverage for size limits and body replay.
Show a summary per file
| File | Description |
|---|---|
pkg/http/server.go |
Adds request-size configuration. |
pkg/http/handler.go |
Registers the limiter first. |
pkg/http/handler_test.go |
Tests handler-level enforcement. |
pkg/http/middleware/body_limit.go |
Implements bounded request bodies. |
pkg/http/middleware/body_limit_test.go |
Tests limit boundaries and lengths. |
pkg/http/middleware/mcp_parse.go |
Handles limit errors with 413. |
pkg/http/middleware/mcp_parse_test.go |
Tests parser composition. |
pkg/http/middleware/scope_challenge.go |
Handles limit errors in fallback parsing. |
pkg/http/middleware/scope_challenge_test.go |
Tests scope fallback composition. |
Review details
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 9/9 changed files
- Comments generated: 2
- Review effort level: Balanced
WithMCPParse and WithScopeChallenge tests for oversized requests were using strings.NewReader, which gives httptest.NewRequest a known Content-Length. That let WithMaxBodySize reject the request in its fast path before the request ever reached the middleware's own io.ReadAll/isMaxBytesError handling, leaving those branches untested. Reuse the existing unknownLengthBody helper (body_limit_test.go) so these tests actually reach the fallback read path and cover the *http.MaxBytesError handling added in WithMCPParse and WithScopeChallenge. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…-bodies-before-mcp-mid-d0a507
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an application-level HTTP request-body size limit that is enforced before any middleware or the MCP SDK reads or buffers the body.
Previously
WithMCPParseand theWithScopeChallengefallback path calledio.ReadAll(r.Body)on an unbounded body with no size guard anywhere in the HTTP stack. A large or malicious payload would be fully buffered in memory by those middlewares (and again by the SDK's ownservePOST) before any downstream size enforcement had a chance to apply.Changes
middleware.WithMaxBodySize(maxBytes)(pkg/http/middleware/body_limit.go): wraps the request body withhttp.MaxBytesReader, rejecting immediately via a knownContent-Lengthfast path, or erroring on read once the limit is hit for chunked/unknown-length bodies. Registered as the first middleware inHandler.RegisterMiddleware, ahead ofExtractUserToken,WithMCPParse,WithPATScopes, andWithScopeChallenge.WithMCPParseandWithScopeChallengenow detect the resulting*http.MaxBytesErrorand return a clear413 Request Entity Too Large("request body too large") response, following the existinghttp.Error(w, ..., statusCode)convention used elsewhere in this package, instead of silently falling through.ServerConfig.MaxRequestBodyBytes(new, optional) lets operators override the default; defaults tomiddleware.DefaultMaxRequestBodyBytes(10 MiB) when unset.Tests
pkg/http/middleware/body_limit_test.go: allowed request, boundary size (exact limit), oversized with knownContent-Length(rejected beforenextruns), oversized with unknown length (rejected on downstream read).pkg/http/middleware/mcp_parse_test.go: composition ofWithMaxBodySize+WithMCPParse— oversized body never reaches parsing/next handler; boundary-size body still parses and preserves the body.pkg/http/middleware/scope_challenge_test.go: composition ofWithMaxBodySize+WithScopeChallenge's fallback body-read path.pkg/http/handler_test.go: end-to-end test throughRegisterMiddleware/RegisterRoutesverifying an oversized request never constructs the MCP server, and a boundary-size request succeeds.Verification
script/lint— 0 issuesscript/test— full suite passesFixes #3102