parse_mimetype: skip whitespace-only segments after semicolons#13013
parse_mimetype: skip whitespace-only segments after semicolons#13013HrachShah wants to merge 2 commits into
Conversation
A trailing or interior ';' followed by nothing but spaces/tabs was being
treated as a valid empty-key parameter (e.g. parse_mimetype('text/html; ')
yielded parameters={'': ''}). Treat whitespace-only segments the same as
empty segments so they are silently dropped, matching RFC 2045's
restriction that parameters must have a name.
Fixes aio-libs#13009.
for more information, see https://pre-commit.ci
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #13013 +/- ##
=======================================
Coverage 98.96% 98.96%
=======================================
Files 131 131
Lines 48096 48103 +7
Branches 2496 2497 +1
=======================================
+ Hits 47596 47603 +7
Misses 376 376
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
|
|
Superseded by #13052, which covers this case plus empty-key () and no-equals () segments. Thanks for the prior work! |
What do these changes do?
Fix
aiohttp.helpers.parse_mimetypereturningMultiDict({'': ''})for a MIME type string whosesemicolon-delimited segments include whitespace-only entries
(e.g.
text/html;ortext/html; charset=utf-8; \t).The pre-fix loop skips segments that are completely empty with
if not item: continue. A whitespace-only segment like ``or
\tis still truthy as a string, so it falls through tothe `partition('=')` branch and produces an `{'': ''}`
parameter entry — violating RFC 2045's requirement that
parameters have a name.
The fix is a one-character widening of the guard:
if not item.strip(): continue. Both empty andwhitespace-only segments are now skipped, so the spurious
{'': ''}entry is gone. Bare trailing;(which producedan empty-string segment) is still handled.
A new parametrized test
test_parse_mimetype_skips_whitespace_only_segmentscoversthe cases from the issue (trailing space, multiple spaces,
tab, mixed spaces/tabs) and the mixed case where a real
parameter precedes the whitespace-only segment (verifying
charsetis preserved and no empty key appears). Theexisting
test_parse_mimetypestill passes unchanged.CHANGES/13009.bugfix.rstand aCONTRIBUTORS.txtentryadded per the project conventions.
Are there changes in behavior for the user?
Yes.
parse_mimetype("text/html; ").parameterspreviouslyreturned
MultiDict({'': ''}); it now returns an empty-paramsMultiDict, matching the existing bare-trailing-semicoloncase. Downstream code that iterates the params dict no longer
sees the spurious empty-key entry.
Is it a substantial burden for the maintainers to support this?
No. One-character guard widening plus a parametrized test
case and a towncrier fragment. No public API change.
Related issue number
Fixes #13009.
Checklist
CONTRIBUTORS.txtCHANGES/folder13009.bugfix.rstbugfixDrafted with Zo Bot; reviewed by keenrose.