Repository navigation
JSONParser and JSONField answer JSON nested past the recursion limit with a 500 instead of a 400 #10110
|
Posting here first, as the contributing guide asks, to check whether a fix would be welcome as a pull request. Current behavior
In a project shell: import io
from rest_framework.parsers import JSONParser
JSONParser().parse(io.BytesIO(b"[" * 200_000 + b"]" * 200_000))
# RecursionError: maximum recursion depth exceeded while decoding a JSON array from a unicode stringfrom django.http import QueryDict
from rest_framework import serializers
class ConfigSerializer(serializers.Serializer):
config = serializers.JSONField()
data = QueryDict(mutable=True)
data["config"] = "[" * 200_000 + "]" * 200_000
ConfigSerializer(data=data).is_valid()
# RecursionError: maximum recursion depth exceeded while decoding a JSON array from a unicode stringhttps://github.com/miniware9/django-rest-framework-json-recursion sends both through Django's request handler: with DRF 3.18.3, Django 6.1.3 and Python 3.13.5, malformed JSON gets a 400 and both nested requests a 500. Proposed changeCatch The change and its tests are ready, and the full suite passes with them. Happy to open the pull request if this looks right. |
Replies: 1 comment 2 replies
|
This reproduces on Python 3.13.18 with DRF 3.18.3 and Django 6.1.3 ( Until a fix is decided, a project can close both off with two small subclasses that answer the deep document the way DRF answers malformed JSON. # myproject/parsers.py
from rest_framework import serializers
from rest_framework.exceptions import ParseError
from rest_framework.parsers import JSONParser
class SafeJSONParser(JSONParser):
def parse(self, stream, media_type=None, parser_context=None):
try:
return super().parse(stream, media_type, parser_context)
except RecursionError:
raise ParseError("JSON parse error - nesting too deep")
class SafeJSONField(serializers.JSONField):
def to_internal_value(self, data):
try:
return super().to_internal_value(data)
except RecursionError:
self.fail("invalid")REST_FRAMEWORK = {
"DEFAULT_PARSER_CLASSES": [
"myproject.parsers.SafeJSONParser",
"rest_framework.parsers.FormParser",
"rest_framework.parsers.MultiPartParser",
],
}The script posts A view that sets its own |
This reproduces on Python 3.13.18 with DRF 3.18.3 and Django 6.1.3 (
python:3.13-slim, no network). Through Django's testClientwithDEBUG=False, a malformed body gets a 400 and a 200,000-level array gets a 500, with theRecursionErrorcoming out ofjson.loadinJSONParser.parse. The form-data case raises the same error out ofis_valid()fromJSONField.to_internal_value.Until a fix is decided, a project can close both off with two small subclasses that answer the deep document the way DRF answers malformed JSON.