Skip to content

parse_mimetype: skip whitespace-only segments after semicolons#13013

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

parse_mimetype: skip whitespace-only segments after semicolons#13013
HrachShah wants to merge 2 commits into
aio-libs:masterfrom
HrachShah:fix/parse-mimetype-whitespace-only-segment-v2

Conversation

@HrachShah

@HrachShah HrachShah commented Jun 30, 2026

Copy link
Copy Markdown

What do these changes do?

Fix aiohttp.helpers.parse_mimetype returning
MultiDict({'': ''}) for a MIME type string whose
semicolon-delimited segments include whitespace-only entries
(e.g. text/html; or text/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 \t is still truthy as a string, so it falls through to
the `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 and
whitespace-only segments are now skipped, so the spurious
{'': ''} entry is gone. Bare trailing ; (which produced
an empty-string segment) is still handled.

A new parametrized test
test_parse_mimetype_skips_whitespace_only_segments covers
the 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
charset is preserved and no empty key appears). The
existing test_parse_mimetype still passes unchanged.

CHANGES/13009.bugfix.rst and a CONTRIBUTORS.txt entry
added per the project conventions.

Are there changes in behavior for the user?

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

  • 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 keenrose.

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.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jun 30, 2026
@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.96%. Comparing base (6b018d5) to head (078e3de).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

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           
Flag Coverage Δ
Autobahn 22.23% <25.00%> (+<0.01%) ⬆️
CI-GHA 98.90% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.66% <100.00%> (-0.01%) ⬇️
OS-Windows 97.04% <100.00%> (+<0.01%) ⬆️
OS-macOS 97.95% <100.00%> (+<0.01%) ⬆️
Py-3.10 98.14% <100.00%> (+<0.01%) ⬆️
Py-3.11 98.41% <100.00%> (+<0.01%) ⬆️
Py-3.12 98.50% <100.00%> (+<0.01%) ⬆️
Py-3.13 98.47% <100.00%> (+<0.01%) ⬆️
Py-3.14 98.49% <100.00%> (+<0.01%) ⬆️
Py-3.14t 97.59% <100.00%> (+<0.01%) ⬆️
Py-pypy-3.11 97.44% <100.00%> (-0.01%) ⬇️
VM-macos 97.95% <100.00%> (+<0.01%) ⬆️
VM-ubuntu 98.66% <100.00%> (-0.01%) ⬇️
VM-windows 97.04% <100.00%> (+<0.01%) ⬆️
cython-coverage 38.01% <0.00%> (-0.01%) ⬇️

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 Jun 30, 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-segment-v2 (078e3de) with master (6b018d5)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 (664d695) during the generation of this report, so 6b018d5 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@HrachShah HrachShah marked this pull request as ready for review June 30, 2026 15:26
@HrachShah

Copy link
Copy Markdown
Author

Superseded by #13052, which covers this case plus empty-key () and no-equals () segments. Thanks for the prior work!

@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