Skip to content

Return a 400 for JSON nested past the recursion limit - #10114

Open
kasparovabi wants to merge 1 commit into
encode:mainfrom
kasparovabi:json-recursion-error
Open

kasparovabi wants to merge 1 commit into
encode:mainfrom
kasparovabi:json-recursion-error

Conversation

@kasparovabi

Copy link
Copy Markdown

Description

refs #10110

JSONParser.parse turns a ValueError from json into a ParseError, and JSONField.to_internal_value turns TypeError and ValueError into its invalid error. A document nested past the recursion limit makes json raise RecursionError instead, so a JSON body like "[" * 200000 + "]" * 200000 ended in a 500, and the same string in a form field ended in an uncaught exception from is_valid().

This adds RecursionError to both except clauses. The parser now answers with JSON parse error - maximum recursion depth exceeded while decoding a JSON array from a unicode string (on 3.14 the text after the prefix reads Stack overflow ...), and the field reports Value must be valid JSON.

The two new tests fail without the change on Python 3.10, 3.13 and 3.14. With it, the full suite passes on 3.10 with Django 5.2 and on 3.13 and 3.14 with Django 6.1, and flake8 and isort are clean.

JSONParser and JSONField caught ValueError from json, but a document nested
past the interpreter's recursion limit raises RecursionError, so the request
ended in a 500. Treat it the same way as malformed JSON.

Refs encode#10110
@auvipy
auvipy self-requested a review November 19, 2026 13:56
@auvipy auvipy added this to the 3.18 milestone Nov 19, 2026

@auvipy auvipy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the failures please

=================================== FAILURES ===================================
___________ ActionDecoratorTestCase.test_method_mapping_http_method ____________
tests/test_decorators.py:399: in test_method_mapping_http_method
method_names = [getattr(HTTPMethod, name.upper()) for name in APIView.http_method_names]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E AttributeError: type object 'HTTPMethod' has no attribute 'QUERY'
___________ ActionDecoratorTestCase.test_method_mapping_http_methods ___________
tests/test_decorators.py:420: in test_method_mapping_http_methods
getattr(test_action.mapping, name)(method)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E AttributeError: 'MethodMapper' object has no attribute 'query'
____________________ TestJSONParser.test_deeply_nested_json ____________________
tests/test_parsers.py:151: in test_deeply_nested_json
with pytest.raises(ParseError) as excinfo:
^^^^^^^^^^^^^^^^^^^^^^^^^
E Failed: DID NOT RAISE ParseError
________________ TestJSONField.test_html_input_nested_too_deep _________________
tests/test_fields.py:2802: in test_html_input_nested_too_deep
assert not serializer.is_valid()
E AssertionError: assert not True
E + where True = is_valid()
E + where is_valid = TestSerializer(data=<QueryDict: {'config': ['[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[...]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]']}>):\n config = JSONField().is_valid
=============================== warnings summary ===============================
.tox/venvs/py315-djangomain/lib/python3.15/site-packages/coverage/inorout.py:515
/home/runner/work/django-rest-framework/django-rest-framework/.tox/venvs/py315-djangomain/lib/python3.15/site-packages/coverage/inorout.py:515: CoverageWarning: --include is ignored because --source is set (include-ignored); see https://coverage.readthedocs.io/en/7.17.3/messages.html#warning-include-ignored
self.warn("--include is ignored because --source is set", slug="include-ignored")

rest_framework/fields.py:1025
/home/runner/work/django-rest-framework/django-rest-framework/rest_framework/fields.py:1025: UserWarning: max_value should be an integer or Decimal instance.
warnings.warn("max_value should be an integer or Decimal instance.")

@auvipy auvipy added the Bug label Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants