From 0ecb9bf7e9d5c79825565bcd8d24cc126503920a Mon Sep 17 00:00:00 2001 From: hemtilva Date: Mon, 16 Nov 2026 03:47:33 +0530 Subject: [PATCH] Return 400 instead of crashing on JSON with an unknown charset PyramidParser passes the request charset to core.parse_json and aiohttp's Request.json() decodes with the request charset. When the Content-Type header names a codec Python does not know (e.g. 'application/json; charset=not-a-charset'), bytes.decode() raises LookupError, which was not handled and surfaced as a 500. parse_json now converts LookupError into JSONDecodeError (as it already does for UnicodeDecodeError), and AIOHTTPParser.load_json routes LookupError to _handle_invalid_json_error, so both frameworks respond with the documented 400 'Invalid JSON body.' error. --- CHANGELOG.rst | 9 +++++++++ src/webargs/aiohttpparser.py | 10 +++++++++- src/webargs/core.py | 8 ++++++++ tests/test_aiohttpparser.py | 10 ++++++++++ tests/test_core.py | 7 ++++++- tests/test_pyramidparser.py | 10 ++++++++++ 6 files changed, 52 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index dc165b6fe..d45a2a883 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,15 @@ Features: ``__version_info__`` constants from ``__init__.py`` file. Users should use ``importlib.metadata`` instead (:pr:`1024`). +Bug fixes: + +* `PyramidParser ` and + `AIOHTTPParser `: Return a 400 "Invalid JSON + body." error instead of crashing with an uncaught ``LookupError`` (HTTP 500) when a + JSON request's ``Content-Type`` header names an unknown charset, e.g. + ``application/json; charset=not-a-charset``. `webargs.core.parse_json` now raises + ``JSONDecodeError`` for an unknown ``encoding``. + Other changes: diff --git a/src/webargs/aiohttpparser.py b/src/webargs/aiohttpparser.py index 774e1b9a2..72e2429bc 100644 --- a/src/webargs/aiohttpparser.py +++ b/src/webargs/aiohttpparser.py @@ -108,6 +108,10 @@ async def load_json(self, req, schema: Schema): return self._handle_invalid_json_error(exc, req) except UnicodeDecodeError as exc: return self._handle_invalid_json_error(exc, req) + except LookupError as exc: + # aiohttp decodes the body with the Content-Type charset, which may + # name an unknown codec + return self._handle_invalid_json_error(exc, req) def load_headers(self, req, schema: Schema) -> MultiDictProxy: """Return headers from the request as a MultiDictProxy.""" @@ -170,7 +174,11 @@ def handle_error( ) def _handle_invalid_json_error( - self, error: json.JSONDecodeError | UnicodeDecodeError, req, *args, **kwargs + self, + error: json.JSONDecodeError | UnicodeDecodeError | LookupError, + req, + *args, + **kwargs, ) -> typing.NoReturn: error_class = exception_map[400] messages = {"json": ["Invalid JSON body."]} diff --git a/src/webargs/core.py b/src/webargs/core.py index 96a4516e6..2bbfedb60 100644 --- a/src/webargs/core.py +++ b/src/webargs/core.py @@ -113,6 +113,14 @@ def parse_json(s: typing.AnyStr, *, encoding: str = "utf-8") -> typing.Any: doc=str(exc.object), pos=exc.start, ) from exc + except LookupError as exc: + # the encoding (typically the charset from a Content-Type header) is not + # a known codec + raise json.JSONDecodeError( + f"Unknown encoding : {encoding}", + doc=str(s), + pos=0, + ) from exc return json.loads(decoded) diff --git a/tests/test_aiohttpparser.py b/tests/test_aiohttpparser.py index 6c7856856..7debd298e 100644 --- a/tests/test_aiohttpparser.py +++ b/tests/test_aiohttpparser.py @@ -84,6 +84,16 @@ def test_validation_error_returns_422_response(self, testapp): assert res.status_code == 422 assert res.json == {"json": {"name": ["Shorter than minimum length 3."]}} + def test_parse_json_with_unknown_charset(self, testapp): + res = testapp.post( + "/echo_json", + b'{"name": "Steve"}', + headers={"Content-Type": "application/json; charset=not-a-charset"}, + expect_errors=True, + ) + assert res.status_code == 400 + assert res.json == {"json": ["Invalid JSON body."]} + @pytest.mark.asyncio async def test_aiohttpparser_synchronous_error_handler(web_request): diff --git a/tests/test_core.py b/tests/test_core.py index 4839a61af..fcac59d01 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -17,7 +17,7 @@ from werkzeug.datastructures import MultiDict as WerkMultiDict from webargs import ValidationError, fields -from webargs.core import Parser, get_mimetype, is_json +from webargs.core import Parser, get_mimetype, is_json, json, parse_json from webargs.multidictproxy import MultiDictProxy @@ -1300,6 +1300,11 @@ def test_get_mimetype(): assert get_mimetype("application/json;charset=utf8") == "application/json" +def test_parse_json_with_unknown_encoding_raises_json_decode_error(): + with pytest.raises(json.JSONDecodeError): + parse_json(b'{"foo": 42}', encoding="not-a-charset") + + class MockRequestParserWithErrorHandler(MockRequestParser): def handle_error(self, error, req, schema, *, error_status_code, error_headers): assert isinstance(error, ValidationError) diff --git a/tests/test_pyramidparser.py b/tests/test_pyramidparser.py index fb8577cc1..0249b31ca 100644 --- a/tests/test_pyramidparser.py +++ b/tests/test_pyramidparser.py @@ -12,3 +12,13 @@ def test_use_args_with_callable_view(self, testapp): def test_parse_matchdict(self, testapp): assert testapp.get("/echo_matchdict/42").json == {"mymatch": 42} + + def test_parse_json_with_unknown_charset(self, testapp): + res = testapp.post( + "/echo_json", + b'{"name": "Steve"}', + headers={"Content-Type": "application/json; charset=not-a-charset"}, + expect_errors=True, + ) + assert res.status_code == 400 + assert res.json == {"json": ["Invalid JSON body."]}