Skip to content

Cap webhook middleware request body at 1 MB#5192

Open
JAORMX wants to merge 1 commit intomainfrom
pr/webhook-request-size-limit
Open

Cap webhook middleware request body at 1 MB#5192
JAORMX wants to merge 1 commit intomainfrom
pr/webhook-request-size-limit

Conversation

@JAORMX
Copy link
Copy Markdown
Collaborator

@JAORMX JAORMX commented May 5, 2026

Summary

Follow-up to the round-2 review of #4564 (item #9). pkg/webhook/validating/middleware.go and pkg/webhook/mutating/middleware.go both called io.ReadAll on the inbound HTTP request body with no size cap before forwarding to the configured webhook server. The client side correctly limited response bodies via io.LimitReader to MaxResponseSize; the server side missed the symmetric limit on inbound requests, so the webhook package would buffer arbitrarily large bodies into memory.

This PR wraps r.Body with http.MaxBytesReader at MaxRequestSize (1 MB, symmetric to MaxResponseSize) and returns HTTP 413 with a JSON-RPC error envelope when the limit is exceeded. The read is rejected before any forwarding.

Type of change

  • Bug fix

Test plan

  • Unit tests (task testpkg/webhook/... passes with -race)
  • Linting (scoped golangci-lint reports 0 issues)
  • New TestValidatingMiddleware_RequestBodySizeLimit and TestMutatingMiddleware_RequestBodySizeLimit cover the over-limit (413 + JSON-RPC envelope) and exact-boundary (accepted) cases.

Special notes for reviewers

  • This is the webhook-layer cap. mcp.ParsingMiddleware (pkg/mcp/parser.go:85) sits earlier in the proxy middleware chain and currently reads the body unbounded itself, so the structural fix against upstream DoS is to also cap at the MCP parsing layer. That's a separate concern and a separate change set; the cap here still bounds the webhook package's own re-read buffer and lays the symmetry groundwork. I will open a tracking issue for the MCP-layer cap.
  • 1 MB matches MaxResponseSize and is comfortable for typical MCP tools/call payloads. If a real workload needs larger bodies we can lift the cap with a config knob; today there's no such use case in tree.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label May 5, 2026
The validating and mutating webhook middlewares both called io.ReadAll
on the inbound HTTP request body with no size cap before forwarding to
the configured webhook server. The client side correctly limited the
response body via io.LimitReader to MaxResponseSize, but the server
side missed the symmetric limit on inbound requests, so the webhook
package would buffer arbitrarily large bodies into memory.

Wrap r.Body with http.MaxBytesReader at MaxRequestSize (1 MB, symmetric
to MaxResponseSize) and return HTTP 413 with a JSON-RPC error envelope
when the limit is exceeded. Reject the read before any forwarding.

Note: this is the webhook-layer cap. mcp.ParsingMiddleware sits earlier
in the proxy chain and currently reads the body unbounded; capping
inbound bodies at the MCP parsing layer is tracked separately and is
the load-bearing fix against upstream DoS. This change still bounds
the webhook package's own re-read buffer and lays the symmetry
groundwork.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@JAORMX JAORMX force-pushed the pr/webhook-request-size-limit branch from 5ddece0 to db55923 Compare May 5, 2026 10:17
@github-actions github-actions Bot added size/S Small PR: 100-299 lines changed and removed size/S Small PR: 100-299 lines changed labels May 5, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.69%. Comparing base (1f138ee) to head (db55923).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5192      +/-   ##
==========================================
+ Coverage   67.64%   67.69%   +0.05%     
==========================================
  Files         606      606              
  Lines       61797    61807      +10     
==========================================
+ Hits        41801    41843      +42     
+ Misses      16839    16805      -34     
- Partials     3157     3159       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant