Repository navigation
Strict JSON parsing lets numbers that overflow to infinity through (1e999), which later 500s in the strict renderer #10094
Replies: 1 comment
|
Reproduced this on 3.15.2. Fixing it in parse_constant = json.strict_constant if self.strict else None
return json.load(decoded_stream, parse_constant=parse_constant)If parse_constant = json.strict_constant if self.strict else None
parse_float = json.strict_float if self.strict else None
return json.load(decoded_stream, parse_constant=parse_constant, parse_float=parse_float)Probably worth a test with Until this lands, a custom parser does the job: import codecs
import math
from django.conf import settings
from rest_framework.exceptions import ParseError
from rest_framework.parsers import JSONParser
from rest_framework.utils import json
def strict_float(value):
number = float(value)
if math.isinf(number):
raise ValueError(f"Out of range float values are not JSON compliant: {value!r}")
return number
class StrictFloatJSONParser(JSONParser):
def parse(self, stream, media_type=None, parser_context=None):
parser_context = parser_context or {}
encoding = parser_context.get("encoding", settings.DEFAULT_CHARSET)
try:
decoded_stream = codecs.getreader(encoding)(stream)
return json.load(
decoded_stream,
parse_constant=json.strict_constant if self.strict else None,
parse_float=strict_float if self.strict else None,
)
except ValueError as exc:
raise ParseError(f"JSON parse error - {exc}")and in settings: REST_FRAMEWORK = {
"DEFAULT_PARSER_CLASSES": [
"path.to.StrictFloatJSONParser",
"rest_framework.parsers.FormParser",
"rest_framework.parsers.MultiPartParser",
],
}
|
Uh oh!
There was an error while loading. Please reload this page.
Hi — posting here first, per the contribution guide, to check whether this behavior change would be welcome as a PR.
Current behavior
Strict JSON parsing rejects the
InfinityandNaNliterals viaparse_constant, but a numeric literal that overflows the float range never reaches that hook: parsing{"x": 1e999}yields{'x': inf}. The value passes parsing and validation, and the strictJSONRendererthen raisesValueErrorwhen it renders the response (allow_nan=False), so the client gets a 500 for what is really malformed input that should have been a 400 at parse time.Proposed change
In
rest_framework/utils/json.py, add astrict_floatand install it asparse_floatin the strictloads/loadwrappers: it converts withfloat()and raises the existing "Out of range float values are not JSON compliant" error when the result is infinite. It is only installed when the caller did not pass a custom decoder class (cls), so custom decoders keep control of their ownparse_float.Verification
I reproduced the gap directly against
rest_framework.utils.json:loads('{"x": 1e999}')returns{'x': inf}today whileInfinityraises; with the change both raise the same error. Intests/test_parsers.py,tests/test_utils.pyandtests/test_fields.py, 5 tests covering this fail without the change and all 385 pass with it.If the direction looks right, I am happy to open a PR with this. If you would rather see it handled at a different layer (parser only, or renderer side), I will adjust.
All reactions