Repository navigation
Strict JSON parsing lets numbers that overflow to infinity through (1e999), which later 500s in the strict renderer #10094
Unanswered
b84330808
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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