fix: respect MaxBodyBytes limit for raw bodies - #1095
Open
leonklingele wants to merge 2 commits into
Open
Conversation
Previously, Huma only enforced the request body limit for regular, non-raw bodies. This is a bug. Make Huma check the limit for raw bodies, too. See also danielgtaylor#1094 for a similar fix.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1095 +/- ##
=======================================
Coverage 93.20% 93.20%
=======================================
Files 23 23
Lines 4988 4990 +2
=======================================
+ Hits 4649 4651 +2
Misses 272 272
Partials 67 67 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a bug where Operation.MaxBodyBytes (and the default request limit behavior) was not applied when an operation used RawBody, bringing raw-body request handling in line with regular body handling.
Changes:
- Apply
ensureBodyReadTimeoutandensureMaxBodyBytesduringRawBodyinput type processing. - Add a regression test ensuring the default body size limit rejects oversized raw-body requests.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| huma.go | Ensures default body read timeout and max body bytes are applied for RawBody inputs. |
| huma_test.go | Adds a test verifying oversized raw bodies are rejected when MaxBodyBytes is omitted. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
1469
to
+1472
| initRequestBody(op, setRequestBodyRequired) | ||
| rbt = setRequestBodyFromRawBody(op, registry, f) | ||
| ensureBodyReadTimeout(op) | ||
| ensureMaxBodyBytes(op) |
Comment on lines
+4505
to
+4516
| func TestRawBodyHasDefaultSizeLimit(t *testing.T) { | ||
| mux, api := humatest.New(t, huma.DefaultConfig("Test API", "1.0.0")) | ||
|
|
||
| var handlerCalled bool | ||
| var receivedLen int | ||
|
|
||
| huma.Register(api, huma.Operation{ | ||
| Method: http.MethodPost, | ||
| Path: "/raw", | ||
| // MaxBodyBytes intentionally omitted | ||
| // Default is 1 MB set by "ensureMaxBodyBytes" | ||
| }, func(ctx context.Context, input *struct { |
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.
Previously, Huma only enforced the request body limit for regular, non-raw bodies.
This is a bug. Make Huma check the limit for raw bodies, too.
See also #1094 for a similar fix.