Skip to content

Commit 051b3cb

Browse files
committed
fix(fetch): JSON-stringify NaN and Infinity string data
Fixes: microsoft/playwright#43329
1 parent 9d8c50f commit 051b3cb

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎playwright/_impl/_fetch.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,12 @@ def is_json_parsable(value: Any) -> bool:
645645
if not isinstance(value, str):
646646
return False
647647
try:
648-
json.loads(value)
648+
# NaN and Infinity are not valid JSON, unlike what json.loads() accepts by default.
649+
json.loads(value, parse_constant=_reject_json_constant)
649650
return True
650-
except json.JSONDecodeError:
651+
except ValueError:
651652
return False
653+
654+
655+
def _reject_json_constant(value: str) -> Any:
656+
raise ValueError(f"{value} is not valid JSON")

‎tests/async/test_fetch_global.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,17 @@ async def test_should_json_stringify_string_body_when_content_type_is_applicatio
391391
),
392392
)
393393
assert req.post_body == b'"foo"'
394+
# https://github.com/microsoft/playwright/issues/43329
395+
# NaN is not valid JSON, even though json.loads() accepts it.
396+
[req, _] = await asyncio.gather(
397+
server.wait_for_request("/empty.html"),
398+
request.post(
399+
server.EMPTY_PAGE,
400+
headers={"content-type": "application/json"},
401+
data="NaN",
402+
),
403+
)
404+
assert req.post_body == b'"NaN"'
394405
await request.dispose()
395406

396407

0 commit comments

Comments
 (0)