mirror of
https://github.com/django/django.git
synced 2025-03-31 11:37:06 +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):
|
||||
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):
|
||||
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."
|
||||
with self.assertRaisesMessage(AttributeError, msg):
|
||||
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