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 @@
Fix JSON media type matching to not accept everything starting with application/json.
scop marked this conversation as resolved.
Show resolved Hide resolved
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
16 changes: 16 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,22 @@ def test_is_expected_content_type_json_non_lowercase():
)


def test_is_expected_content_type_json_with_charset():
scop marked this conversation as resolved.
Show resolved Hide resolved
expected_ct = "application/json"
response_ct = "application/json; charset=UTF-8"
assert is_expected_content_type(
response_content_type=response_ct, expected_content_type=expected_ct
)


def test_is_expected_content_type_json_match_not_just_prefix():
expected_ct = "application/json"
response_ct = "application/json-seq"
assert not is_expected_content_type(
response_content_type=response_ct, expected_content_type=expected_ct
)
scop marked this conversation as resolved.
Show resolved Hide resolved


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