Skip to content

Fix parse_mimetype producing spurious empty-key parameter for whitespace-only segments after semicolons#13010

Merged
Dreamsorcerer merged 1 commit into
aio-libs:masterfrom
JSap0914:fix/parse-mimetype-whitespace-segment
Jul 8, 2026
Merged

Fix parse_mimetype producing spurious empty-key parameter for whitespace-only segments after semicolons#13010
Dreamsorcerer merged 1 commit into
aio-libs:masterfrom
JSap0914:fix/parse-mimetype-whitespace-segment

Conversation

@JSap0914

Copy link
Copy Markdown
Contributor

Summary

parse_mimetype incorrectly 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.py splits on ; and skips empty segments with:

if not item:
    continue

A bare trailing ; (like text/html;) produces an empty-string segment, which is correctly skipped. But text/html; produces a whitespace-only segment (' ') which is truthy — so it is NOT skipped — and ends up as params.add('', '').

Fix

-        if not item:
+        if not item.strip():

Reproduction / before

from aiohttp.helpers import parse_mimetype

dict(parse_mimetype('text/html; ').parameters)         # {'': ''}  ← wrong
dict(parse_mimetype('text/html;  ').parameters)        # {'': ''}  ← wrong
dict(parse_mimetype('text/html;\t').parameters)        # {'': ''}  ← wrong
dict(parse_mimetype('text/html; charset=utf-8; ').parameters)  # {'charset': 'utf-8', '': ''}  ← wrong

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.

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.
Copilot AI review requested due to automatic review settings June 30, 2026 04:08
@JSap0914 JSap0914 requested a review from asvetlov as a code owner June 30, 2026 04:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@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 (08ef461).
⚠️ Report is 25 commits behind head on master.
✅ All tests successful. No failed tests found.

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           
Flag Coverage Δ
Autobahn 22.23% <0.00%> (ø)
CI-GHA 98.90% <100.00%> (+<0.01%) ⬆️
OS-Linux 98.66% <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%> (+<0.01%) ⬆️
Py-3.12 98.50% <100.00%> (ø)
Py-3.13 98.47% <100.00%> (-0.01%) ⬇️
Py-3.14 98.49% <100.00%> (+<0.01%) ⬆️
Py-3.14t 97.58% <100.00%> (ø)
Py-pypy-3.11 97.44% <100.00%> (-0.01%) ⬇️
VM-macos 97.94% <100.00%> (-0.01%) ⬇️
VM-ubuntu 98.66% <100.00%> (-0.01%) ⬇️
VM-windows 97.04% <100.00%> (ø)
cython-coverage 38.02% <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 Jun 30, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 83 untouched benchmarks
⏩ 83 skipped benchmarks1


Comparing JSap0914:fix/parse-mimetype-whitespace-segment (08ef461) with master (5ff087a)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 (6b018d5) during the generation of this report, so 5ff087a was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Dreamsorcerer Dreamsorcerer added bot:chronographer:skip This PR does not need to include a change note backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot labels Jul 7, 2026
@Dreamsorcerer Dreamsorcerer merged commit 9e00085 into aio-libs:master Jul 8, 2026
89 of 92 checks passed
@patchback

patchback Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Backport to 3.14: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.14/9e00085c958fbcd40de37867e773bf7cd3bbee0c/pr-13010

Backported as #13096

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@patchback

patchback Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Backport to 3.15: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.15/9e00085c958fbcd40de37867e773bf7cd3bbee0c/pr-13010

Backported as #13097

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

Dreamsorcerer pushed a commit that referenced this pull request Jul 8, 2026
…ious empty-key parameter for whitespace-only segments after semicolons (#13097)

**This is a backport of PR #13010 as merged into master
(9e00085).**

Co-authored-by: JSap0914 <116227558+JSap0914@users.noreply.github.com>
Dreamsorcerer pushed a commit that referenced this pull request Jul 8, 2026
…ious empty-key parameter for whitespace-only segments after semicolons (#13096)

**This is a backport of PR #13010 as merged into master
(9e00085).**

Co-authored-by: JSap0914 <116227558+JSap0914@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot bot:chronographer:skip This PR does not need to include a change note

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

3 participants