parse_mimetype: skip whitespace-only segments after a semicolon#13031
parse_mimetype: skip whitespace-only segments after a semicolon#13031HrachShah wants to merge 2 commits into
Conversation
Issue aio-libs#13009: a MIME-type string like 'text/html; ' or 'text/html; charset=utf-8; ' split on ';' yielded a truthy whitespace-only segment, and the existing 'if not item: continue' guard did not catch it, so a spurious empty-key parameter ('{'': ''}') ended up in the parsed parameters dict. Tightened the guard to 'if not item.strip(): continue' so any segment that is empty or whitespace-only is dropped, matching the existing behaviour for a bare trailing ';' and the RFC 2045 notion that whitespace between parameters is non-semantic.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #13031 +/- ##
=======================================
Coverage 98.96% 98.96%
=======================================
Files 131 131
Lines 48156 48156
Branches 2499 2499
=======================================
Hits 47656 47656
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. |
What do these changes do?
Fix
aiohttp.helpers.parse_mimetypereturningMultiDict({'': ''})for a MIME type with a whitespace-onlysegment after the semicolon. The helper splits the string on
;and skips empty segments withif not item: continue,which handles
text/html;correctly (the empty-string segmentis falsy) but lets
text/html;andtext/html;\tfallthrough to the
partition('=')branch and produce a{'': ''}parameter entry.The fix is one character:
if not item.strip(): continue, soboth empty and whitespace-only segments are skipped. Bare
trailing
;still works (the segment is the empty string,which
strip()s to ``).Three new parametrized cases on the existing
test_parse_mimetypeintests/test_helpers.pycoverapplication/json;,application/json;\t, andapplication/json; charset=utf-8;. The tests fail on thepre-fix code (extra
''key in.parameters) and pass withthe fix. All 11 cases in
test_parse_mimetypepass; thebroader
tests/test_helpers.pyruns clean (139 passed).CHANGES/13009.bugfix.rstadded per the project convention.Are there changes in behavior for the user?
Yes.
parse_mimetype("text/html; ")previously returned aMultiDictwith a spurious{'': ''}entry; it now returnsan empty params
MultiDict, matching the existingtrailing-semicolon case.
Is it a substantial burden for the maintainers to support this?
No. One-line guard plus three small parametrized test cases.
Related issue number
Fixes #13009.
Checklist
CONTRIBUTORS.txtCHANGES/folder13009.bugfix.rstbugfixDrafted with Zo Bot; reviewed by @HrachShah.