Fix _parse_http_header crash on a valueless media-type parameter - #1532
Open
uttam12331 wants to merge 1 commit into
Open
uttam12331 wants to merge 1 commit into
uttam12331 wants to merge 1 commit into
Conversation
The fast path (no quotes) unpacked `name, value = attr.split('=', 1)`, which
raises `ValueError: not enough values to unpack` when a parameter has no `=`
(a flag parameter). The slow path already tolerates this and stores an empty
value, and the function's docstring promises graceful handling of broken
input.
For example a request with `Content-Type: text/plain; charset` reaches this
via `request.POST`/`forms`/`params` and raises, surfacing as a 500 instead of
being parsed as `[('text/plain', {'charset': ''})]`.
Use `str.partition('=')`, which always returns a 3-tuple (empty value for a
valueless parameter), matching the slow path. Add a regression test.
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.
Summary
_parse_http_headercrashes on a media-type parameter that has no value (a "flag" parameter), e.g.Content-Type: text/plain; charset.The function has two branches. The fast path (no
"in the header) unpacks each parameter directly:When
attrhas no=,attr.split('=', 1)returns a 1-element list and the tuple unpack raisesValueError: not enough values to unpack. The slow path (quotes present) already tolerates this and stores an empty value, and the docstring says the parser should return partial results for broken input.Impact
request.POST/request.forms/request.paramscall_parse_http_headeron the requestContent-Typebefore any multipart check, so a request like:raises inside the
POSTproperty, which surfaces to the client as a 500 Internal Server Error (with a traceback inwsgi.errors) instead of being parsed as[('text/plain', {'charset': ''})]and handled normally.Fix
Use
str.partition('='), which always returns a 3-tuple (empty value for a valueless parameter) — matching the slow path:Tests
Added
test_valueless_parameter(intest/test_html_helper.py), asserting a valueless parameter degrades to an empty value on both the fast and slow paths. It fails on the current code (ValueError) and passes with the fix.