Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix too lax application/json prefix-only matching #6180

Merged
merged 9 commits into from
Nov 3, 2021
1 change: 1 addition & 0 deletions CHANGES/6180.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed matching the JSON media type to not accept arbitrary characters after ``application/json`` or the ``+json`` media type suffix.
4 changes: 3 additions & 1 deletion aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ def iscoroutinefunction(func: Any) -> bool:
return asyncio.iscoroutinefunction(func)


json_re = re.compile(r"(?:application/|[\w.-]+/[\w.+-]+?\+)json", re.IGNORECASE)
json_re = re.compile(
r"(?:application/|[\w.-]+/[\w.+-]+?\+)json(?:\s*;.*)?$", re.IGNORECASE
)


class BasicAuth(namedtuple("BasicAuth", ["login", "password", "encoding"])):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,23 @@ def test_is_expected_content_type_json_non_lowercase():
)


@pytest.mark.paratetrize(
("expected_ct", "response_ct", "should_match"),
(
("application/json", "application/json; charset=UTF-8", True),
("application/json", "application/json-seq", False),
),
ids=("with-charset", "non-plus-suffix-should-not-match"),
)
def test_is_expected_content_type_json(expected_ct, response_ct, should_match):
assert (
is_expected_content_type(
response_content_type=response_ct, expected_content_type=expected_ct
)
is should_match
)


def test_is_expected_content_type_non_json_match_exact():
expected_ct = "text/javascript"
response_ct = "text/javascript"
Expand Down