From 08ef4617e7034cbb1d967fa41e8507c759653887 Mon Sep 17 00:00:00 2001 From: JSap0914 Date: Tue, 30 Jun 2026 13:07:38 +0900 Subject: [PATCH] Fix parse_mimetype ignoring whitespace-only segments after semicolons 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. --- aiohttp/helpers.py | 2 +- tests/test_helpers.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 1a45a060ff1..fe5ea9b5ab6 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -332,7 +332,7 @@ def parse_mimetype(mimetype: str) -> MimeType: parts = mimetype.split(";") params: MultiDict[str] = MultiDict() for item in parts[1:]: - if not item: + if not item.strip(): continue key, _, value = item.partition("=") params.add(key.lower().strip(), value.strip(' "')) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 4d6b9e34e1d..deaea0f9fa0 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -74,6 +74,27 @@ "text", "plain", "", MultiDictProxy(MultiDict({"base64": ""})) ), ), + ( + "text/html; ", + helpers.MimeType("text", "html", "", MultiDictProxy(MultiDict())), + ), + ( + "text/html; ", + helpers.MimeType("text", "html", "", MultiDictProxy(MultiDict())), + ), + ( + "text/html;\t", + helpers.MimeType("text", "html", "", MultiDictProxy(MultiDict())), + ), + ( + "text/html; charset=utf-8; ", + helpers.MimeType( + "text", + "html", + "", + MultiDictProxy(MultiDict({"charset": "utf-8"})), + ), + ), ], ) def test_parse_mimetype(mimetype: str, expected: helpers.MimeType) -> None: