Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ Features:
``__version_info__`` constants from ``__init__.py`` file. Users should use
``importlib.metadata`` instead (:pr:`1024`).

Bug fixes:

* `PyramidParser <webargs.pyramidparser.PyramidParser>` and
`AIOHTTPParser <webargs.aiohttpparser.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:


Expand Down
10 changes: 9 additions & 1 deletion src/webargs/aiohttpparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."]}
Expand Down
8 changes: 8 additions & 0 deletions src/webargs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
10 changes: 10 additions & 0 deletions tests/test_aiohttpparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_pyramidparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."]}