Repository navigation
Return a 400 for JSON nested past the recursion limit - #10114
kasparovabi wants to merge 1 commit into
Conversation
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
left a comment
There was a problem hiding this comment.
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.")
Description
refs #10110
JSONParser.parseturns aValueErrorfromjsoninto aParseError, andJSONField.to_internal_valueturnsTypeErrorandValueErrorinto itsinvaliderror. A document nested past the recursion limit makesjsonraiseRecursionErrorinstead, so a JSON body like"[" * 200000 + "]" * 200000ended in a 500, and the same string in a form field ended in an uncaught exception fromis_valid().This adds
RecursionErrorto both except clauses. The parser now answers withJSON parse error - maximum recursion depth exceeded while decoding a JSON array from a unicode string(on 3.14 the text after the prefix readsStack overflow ...), and the field reportsValue 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.