BaseRequest.text() and .post(): map invalid body bytes to HTTP 415#13100
Draft
HrachShah wants to merge 1 commit into
Draft
BaseRequest.text() and .post(): map invalid body bytes to HTTP 415#13100HrachShah wants to merge 1 commit into
HrachShah wants to merge 1 commit into
Conversation
The except clause around the bytes.decode() call in BaseRequest.text() and the application/x-www-form-urlencoded branch of BaseRequest.post() only catches LookupError, so an invalid-bytes UnicodeDecodeError leaks out of the helper and into the user's request handler. Mirror the existing 415 contract for unknown charset names: a body that cannot be decoded with the declared (or default) charset is 'unsupported media type' from aiohttp's perspective. The test pair covers the default-charset path and the explicit-charset path; both fail on the pre-fix code with UnicodeDecodeError and pass with the fix.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #13100 +/- ##
=======================================
Coverage 98.97% 98.97%
=======================================
Files 131 131
Lines 48635 48652 +17
Branches 2525 2525
=======================================
+ Hits 48135 48154 +19
+ Misses 376 374 -2
Partials 124 124
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Merging this PR will not alter performance
Comparing Footnotes
|
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.
Fixes #13099.
BaseRequest.text()and theapplication/x-www-form-urlencodedbranch ofBaseRequest.post()catchLookupErrorfrombytes.decode(charset)and re-raise it asHTTPUnsupportedMediaType(HTTP 415). The intent is to surface any decode problem as a 415 so the user gets a clear status code instead of an unhandledUnicodeDecodeErrorin their handler.But the catch only covers
LookupError, which is what an unknown codec raises (LookupError: unknown encoding: ...). It misses the more common case: the charset name is known (utf-8, latin-1, etc.) but the body is not valid in that encoding.bytes.decode()raisesUnicodeDecodeErrorfor that, which is a subclass ofValueError, so it propagates out oftext()/post()and crashes the handler with a traceback that the user has no way to handle cleanly.Fix: extend the
exceptclause to also catchUnicodeDecodeError, so an invalid byte sequence in the body maps to the same HTTP 415 a missing codec does. The body is still consumed and the existing_read_bytescache is populated, so subsequentreq.read()/req.json()calls don't re-decode.Tests in
tests/test_web_request.py:test_request_text_raises_415_for_invalid_utf8_bytes—req.text()with bodyb'\xff'andContent-Type: text/plain; charset=utf-8now raisesHTTPUnsupportedMediaType(pre-fix: leaksUnicodeDecodeError).test_request_post_urlencoded_raises_415_for_invalid_utf8_bytes—req.post()with bodyb'a=1&b=\xff'andContent-Type: application/x-www-form-urlencodednow raisesHTTPUnsupportedMediaType(pre-fix: leaksUnicodeDecodeError).Both tests fail on the pre-fix code with the original
UnicodeDecodeErrorand pass with the fix applied.Full
tests/test_web_request.pyrun: 135 passed.CHANGES fragment at
CHANGES/13099.bugfix.rst.