mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
Raise exception if invalid JSON numbers are encountered.
This commit is contained in:
parent
8eb9dab0ca
commit
e9a18b4a2a
@ -70,6 +70,11 @@ class JSONParser(BaseParser):
|
|||||||
def parse(self, request):
|
def parse(self, request):
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
|
|
||||||
|
def strict_constant(o):
|
||||||
|
raise ValueError(
|
||||||
|
"Out of range float values are not JSON compliant: " + repr(o)
|
||||||
|
)
|
||||||
|
|
||||||
if isinstance(request, HttpRequest):
|
if isinstance(request, HttpRequest):
|
||||||
request = request.body
|
request = request.body
|
||||||
return json.loads(request), MultiValueDict()
|
return json.loads(request, parse_constant=strict_constant), MultiValueDict()
|
||||||
|
@ -60,3 +60,13 @@ class TestParsers(SimpleTestCase):
|
|||||||
msg = "You cannot change parsers after processing the request's content."
|
msg = "You cannot change parsers after processing the request's content."
|
||||||
with self.assertRaisesMessage(AttributeError, msg):
|
with self.assertRaisesMessage(AttributeError, msg):
|
||||||
request.parsers = []
|
request.parsers = []
|
||||||
|
|
||||||
|
def test_json_strict(self):
|
||||||
|
parser = JSONParser()
|
||||||
|
|
||||||
|
msg_base = "Out of range float values are not JSON compliant: '%s'"
|
||||||
|
for value in ["Infinity", "-Infinity", "NaN"]:
|
||||||
|
with self.subTest(value=value):
|
||||||
|
msg = msg_base % value
|
||||||
|
with self.assertRaisesMessage(ValueError, msg):
|
||||||
|
parser.parse(bytes(value.encode()))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user