fix: reject oversized body via Content-Length before reading it - #228
Merged
Merged
Conversation
…g it Granian's protocol() reads the entire request body into memory in one shot; the OXYROUTE_MAX_BODY_BYTES check ran only after that read and after a second copy into PooledBuffer, so a client could force a multi-GB allocation per request regardless of the configured limit. Check the declared Content-Length against the limit before awaiting protocol() and reject with 413 up front. Bodies without a Content-Length (chunked transfer-encoding) still fall through to the existing post-read check. Closes #206
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.
Problem
Granian's
protocol()reads the entire request body into memory in one shot.OXYROUTE_MAX_BODY_BYTESwas only checked after that read (and after a second copy intoPooledBuffer), so a client sending a multi-GB body forces that full allocation before being rejected — trivial memory-exhaustion DoS regardless of the configured limit.Fix
Check the request's declared
Content-Lengthagainst the limit before awaitingprotocol(), and return 413 immediately if it's too large. Bodies withoutContent-Length(chunked transfer-encoding) still fall through to the existing post-read check, so this closes the common/most exploitable case (an attacker declaring an oversized body) without buffering it first.Testing
cargo build --libandmaturin develop --releasesucceed.tests/test_form_body.py::test_payload_too_large_413,tests/test_json_body.py,tests/test_db_query.pypass.Closes #206