fix(middleware): BodyLimit can be bypassed by a single oversized Read - #3092
Open
liuyuyan2717 wants to merge 1 commit into
Open
fix(middleware): BodyLimit can be bypassed by a single oversized Read#3092liuyuyan2717 wants to merge 1 commit into
liuyuyan2717 wants to merge 1 commit into
Conversation
limitedReader had two defects that allowed callers following standard io.Reader semantics (process n>0 bytes before checking err) to read arbitrarily far past the configured limit: 1. Read forwarded the caller's full buffer to the underlying reader unbounded, so a single large Read could return enough data to both cross the limit and complete a value (e.g. a JSON object). 2. After returning the limit-exceeded error once, subsequent Read calls continued returning real data instead of sticking at (0, err). Fix by mirroring net/http.MaxBytesReader: - Bound each Read to at most remaining+1 bytes, so a single call can never both cross the limit and complete a value. - Make the error sticky: once tripped, every subsequent Read returns (0, status 413) with no additional data. Fixes labstack#3071
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.
Description
limitedReader.Readhad two defects that allowed callers following standardio.Readersemantics (processn>0bytes before checkingerr) to read arbitrarily far past the configured BodyLimit:Readforwarded the caller's full buffer to the underlying reader, so a single largeReadcould return enough data to both cross the limit and complete a value (e.g. a JSON object parsed byencoding/json.Decoder).Readcalls continued returning real data instead of sticking at(0, err).Fix
Mirror
net/http.MaxBytesReader:remaining + 1bytes, so a single call can never both cross the limit and complete a value.Readreturns(0, ErrStatusRequestEntityTooLarge)with no additional data.Test coverage
Added three tests:
TestBodyLimitReader_stickyError— verifies post-limit reads always return 0 bytesTestBodyLimitReader_boundedRead— verifies single large reads are capped at remaining+1TestBodyLimit_readAfterLimitNoLeak— integration test with chunked encoding, simulating anio.Reader-compliant consumerFixes #3071
Checklist