Skip to content

parse_mimetype: skip whitespace-only segments after a semicolon#13031

Closed
HrachShah wants to merge 2 commits into
aio-libs:masterfrom
HrachShah:fix/parse-mimetype-whitespace-only-segments
Closed

parse_mimetype: skip whitespace-only segments after a semicolon#13031
HrachShah wants to merge 2 commits into
aio-libs:masterfrom
HrachShah:fix/parse-mimetype-whitespace-only-segments

Conversation

@HrachShah

@HrachShah HrachShah commented Jul 1, 2026

Copy link
Copy Markdown

What do these changes do?

Fix aiohttp.helpers.parse_mimetype returning
MultiDict({'': ''}) for a MIME type with a whitespace-only
segment after the semicolon. The helper splits the string on
; and skips empty segments with if not item: continue,
which handles text/html; correctly (the empty-string segment
is falsy) but lets text/html; and text/html;\t fall
through to the partition('=') branch and produce a
{'': ''} parameter entry.

The fix is one character: if not item.strip(): continue, so
both 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_mimetype in tests/test_helpers.py cover
application/json; , application/json;\t, and
application/json; charset=utf-8; . The tests fail on the
pre-fix code (extra '' key in .parameters) and pass with
the fix. All 11 cases in test_parse_mimetype pass; the
broader tests/test_helpers.py runs clean (139 passed).

CHANGES/13009.bugfix.rst added per the project convention.

Are there changes in behavior for the user?

Yes. parse_mimetype("text/html; ") previously returned a
MultiDict with a spurious {'': ''} entry; it now returns
an empty params MultiDict, matching the existing
trailing-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

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes (N/A — internal helper)
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
  • Add a new news fragment into the CHANGES/ folder
    • name: 13009.bugfix.rst
    • category: bugfix

Drafted with Zo Bot; reviewed by @HrachShah.

Hrach added 2 commits July 1, 2026 19:40
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.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jul 1, 2026
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.96%. Comparing base (8ef76ab) to head (c978579).
⚠️ Report is 2 commits behind head on master.
✅ All tests successful. No failed tests found.

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           
Flag Coverage Δ
Autobahn 22.21% <0.00%> (ø)
CI-GHA 98.90% <100.00%> (ø)
OS-Linux 98.67% <100.00%> (+<0.01%) ⬆️
OS-Windows 97.04% <100.00%> (ø)
OS-macOS 97.94% <100.00%> (-0.01%) ⬇️
Py-3.10 98.14% <100.00%> (ø)
Py-3.11 98.41% <100.00%> (ø)
Py-3.12 98.50% <100.00%> (ø)
Py-3.13 98.48% <100.00%> (ø)
Py-3.14 98.49% <100.00%> (-0.01%) ⬇️
Py-3.14t 97.58% <100.00%> (-0.01%) ⬇️
Py-pypy-3.11 97.45% <100.00%> (+<0.01%) ⬆️
VM-macos 97.94% <100.00%> (-0.01%) ⬇️
VM-ubuntu 98.67% <100.00%> (+<0.01%) ⬆️
VM-windows 97.04% <100.00%> (ø)
cython-coverage 38.11% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@codspeed-hq

codspeed-hq Bot commented Jul 1, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 83 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing HrachShah:fix/parse-mimetype-whitespace-only-segments (c978579) with master (8ef76ab)2

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on master (f24eced) during the generation of this report, so 8ef76ab was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@HrachShah

Copy link
Copy Markdown
Author

Superseded by #13052, which covers this case plus empty-key () and no-equals () segments.

@HrachShah HrachShah closed this Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parse_mimetype produces spurious empty-key parameter for whitespace-only segments after semicolons

1 participant