Repository navigation
fix: accept request bodies of exactly MaxBodyBytes - #1131
Merged
Merged
Conversation
readBody limited the reader to maxBytes and treated count == maxBytes as an overflow, so a body of exactly the configured limit was rejected with 413 although the request limits documentation promises that status only for larger bodies. Read one byte past the limit and reject on count > maxBytes, leave a limit of math.MaxInt64 unchanged so the extra byte cannot overflow, add table-driven cases for the exact size, one byte over, the -1 opt-out and the largest limit, and align the MaxBodyBytes comment with the documentation.
Stdlib already accepts exactly n bytes and handles MaxInt64 without overflow, so the +1 read and the guard can go. A MaxBytesError from an upstream reader (e.g. chi RequestSize) now returns 413 with its own limit instead of 500.
Collaborator
|
Thanks! I pushed a small follow-up that swaps the +1 read for |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1131 +/- ##
==========================================
+ Coverage 93.19% 93.21% +0.01%
==========================================
Files 24 24
Lines 5012 5011 -1
==========================================
Hits 4671 4671
Misses 273 273
+ Partials 68 67 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
readBodywraps the request body inio.LimitReader(reader, maxBytes)and treatscount == maxBytesas an overflow, so a body whose size is exactlyMaxBodyBytesis rejected with 413 even though it does not exceed the limit. The request limits documentation promises a 413 only for bodies larger than the limit, and the comment onOperation.MaxBodyByteswas the one place describing the inclusive behaviour. With the default of 1 MiB this is easy to hit: an upload sized to the advertised limit fails.The fix reads one byte past the limit and rejects on
count > maxBytes, so a body of exactly the limit passes while anything longer is still detected without buffering more than one extra byte. A limit ofmath.MaxInt64is left alone so that the extra byte cannot overflow. TheMaxBodyBytescomment now matches the documentation. This is a behaviour change for anyone who relied on the old rejection: an operation that used to refuse a body of exactlyMaxBodyBytesnow accepts it, and a limit that was set one byte high to work around this now accepts one more byte than intended.Four cases join the table-driven
TestFeaturessuite next to the existingrequest-body-too-largecase: a 12-byte JSON body against a 12-byte limit is accepted, the same body against an 11-byte limit gets 413,MaxBodyBytes: -1accepts a body just over 2 MiB, andMaxBodyBytes: math.MaxInt64is accepted as unlimited.go test -race ./...passes.This is the off-by-one that robtme pointed out while reviewing #1095 (and noted again on #1094); it predates both pull requests, and the review there suggested a separate PR for it. #1094 applies the same inclusive check (
r.N == 0) to multipart bodies, so if it lands first that check would need the same adjustment; this change only touchesreadBody.