From 27f5902bec203d9223d072121ddbcf12fbaa0722 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:10:01 -0600 Subject: [PATCH] Return 400 for unknown charset in JSON Content-Type PyramidParser and AIOHTTPParser raised an uncaught LookupError when a request declared a charset Python does not recognise, producing a 500 instead of the documented 400 "Invalid JSON body." response. Catch LookupError alongside UnicodeDecodeError in core's load_json and in AIOHTTPParser's load_json so it is routed to _handle_invalid_json_error. Fixes #1120 --- src/webargs/aiohttpparser.py | 8 ++++++-- src/webargs/core.py | 4 ++-- src/webargs/flaskparser.py | 2 +- src/webargs/pyramidparser.py | 2 +- src/webargs/tornadoparser.py | 2 +- tests/test_aiohttpparser.py | 11 +++++++++++ tests/test_pyramidparser.py | 11 +++++++++++ 7 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/webargs/aiohttpparser.py b/src/webargs/aiohttpparser.py index 774e1b9a2..c04f0b806 100644 --- a/src/webargs/aiohttpparser.py +++ b/src/webargs/aiohttpparser.py @@ -106,7 +106,7 @@ async def load_json(self, req, schema: Schema): if exc.doc == "": return core.missing return self._handle_invalid_json_error(exc, req) - except UnicodeDecodeError as exc: + except (UnicodeDecodeError, LookupError) as exc: return self._handle_invalid_json_error(exc, req) def load_headers(self, req, schema: Schema) -> MultiDictProxy: @@ -170,7 +170,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..36d9d437d 100644 --- a/src/webargs/core.py +++ b/src/webargs/core.py @@ -777,7 +777,7 @@ def pre_load( def _handle_invalid_json_error( self, - error: json.JSONDecodeError | UnicodeDecodeError, + error: json.JSONDecodeError | UnicodeDecodeError | LookupError, req: Request, *args: typing.Any, **kwargs: typing.Any, @@ -806,7 +806,7 @@ def load_json(self, req: Request, schema: ma.Schema) -> typing.Any: if exc.doc == "": return missing return self._handle_invalid_json_error(exc, req) - except UnicodeDecodeError as exc: + except (UnicodeDecodeError, LookupError) as exc: return self._handle_invalid_json_error(exc, req) def load_json_or_form(self, req: Request, schema: ma.Schema) -> typing.Any: diff --git a/src/webargs/flaskparser.py b/src/webargs/flaskparser.py index 278c4fa1b..31fc2732a 100644 --- a/src/webargs/flaskparser.py +++ b/src/webargs/flaskparser.py @@ -78,7 +78,7 @@ def _raw_load_json(self, req: flask.Request) -> typing.Any: def _handle_invalid_json_error( self, - error: json.JSONDecodeError | UnicodeDecodeError, + error: json.JSONDecodeError | UnicodeDecodeError | LookupError, req: flask.Request, *args: typing.Any, **kwargs: typing.Any, diff --git a/src/webargs/pyramidparser.py b/src/webargs/pyramidparser.py index 56c27365a..36bd052fb 100644 --- a/src/webargs/pyramidparser.py +++ b/src/webargs/pyramidparser.py @@ -120,7 +120,7 @@ def handle_error( def _handle_invalid_json_error( self, - error: json.JSONDecodeError | UnicodeDecodeError, + error: json.JSONDecodeError | UnicodeDecodeError | LookupError, req: Request, *args: typing.Any, **kwargs: typing.Any, diff --git a/src/webargs/tornadoparser.py b/src/webargs/tornadoparser.py index 1b0c73d83..67779479d 100644 --- a/src/webargs/tornadoparser.py +++ b/src/webargs/tornadoparser.py @@ -158,7 +158,7 @@ def handle_error( def _handle_invalid_json_error( self, - error: json.JSONDecodeError | UnicodeDecodeError, + error: json.JSONDecodeError | UnicodeDecodeError | LookupError, req: HTTPServerRequest, *args: typing.Any, **kwargs: typing.Any, diff --git a/tests/test_aiohttpparser.py b/tests/test_aiohttpparser.py index 6c7856856..d8689d366 100644 --- a/tests/test_aiohttpparser.py +++ b/tests/test_aiohttpparser.py @@ -63,6 +63,17 @@ def test_schema_as_kwargs_view(self, testapp): "name": "Chandler" } + # https://github.com/marshmallow-code/webargs/issues/1120 + def test_parse_json_with_unknown_charset(self, testapp): + res = testapp.post( + "/echo_json", + '{"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."]} + # https://github.com/marshmallow-code/webargs/pull/297 def test_empty_json_body(self, testapp): environ = {"CONTENT_TYPE": "application/json", "wsgi.input": BytesIO(b"")} diff --git a/tests/test_pyramidparser.py b/tests/test_pyramidparser.py index fb8577cc1..36bff23e8 100644 --- a/tests/test_pyramidparser.py +++ b/tests/test_pyramidparser.py @@ -12,3 +12,14 @@ def test_use_args_with_callable_view(self, testapp): def test_parse_matchdict(self, testapp): assert testapp.get("/echo_matchdict/42").json == {"mymatch": 42} + + # https://github.com/marshmallow-code/webargs/issues/1120 + def test_parse_json_with_unknown_charset(self, testapp): + res = testapp.post( + "/echo_json", + '{"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."]}