Fix parse_mimetype producing spurious empty-key parameter for whitespace-only segments after semicolons#13010
Conversation
When a MIME type string contains a trailing semicolon followed by
whitespace (e.g. 'text/html; ' or 'text/html; charset=utf-8; '),
the whitespace-only segment was not being skipped, resulting in a
spurious empty-key parameter ({"": ""}) being added to the
parsed MimeType's parameters dict.
The existing check 'if not item: continue' only skips truly empty
strings (from a trailing bare semicolon like 'text/html;'), but
not whitespace-only strings produced by 'text/html; '.
Fix: use 'if not item.strip(): continue' to also skip whitespace-only
segments, consistent with RFC 2045 which requires parameter names
to be non-empty tokens.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #13010 +/- ##
=======================================
Coverage 98.96% 98.96%
=======================================
Files 131 131
Lines 48096 48096
Branches 2496 2496
=======================================
Hits 47596 47596
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
|
Backport to 3.14: 💚 backport PR created✅ Backport PR branch: Backported as #13096 🤖 @patchback |
Backport to 3.15: 💚 backport PR created✅ Backport PR branch: Backported as #13097 🤖 @patchback |
Summary
parse_mimetypeincorrectly adds a spurious{'': ''}parameter entry when a MIME type string has a trailing semicolon followed by whitespace (e.g.'text/html; ').Closes #13009
Root cause
aiohttp/helpers.pysplits on;and skips empty segments with:A bare trailing
;(liketext/html;) produces an empty-string segment, which is correctly skipped. Buttext/html;produces a whitespace-only segment (' ') which is truthy — so it is NOT skipped — and ends up asparams.add('', '').Fix
Reproduction / before
After fix
All four return the expected values with no empty-key parameter.
Testing
Four new parametrize cases added to
tests/test_helpers.py::test_parse_mimetype. All 141 existing tests continue to pass.