diff --git a/playwright/_impl/network.py b/playwright/_impl/network.py index 1b65871a8..5f769895a 100644 --- a/playwright/_impl/network.py +++ b/playwright/_impl/network.py @@ -254,8 +254,11 @@ def post_data_json(self) -> Optional[Any]: entries[k] = v return entries + def reject_json_constant(value: str) -> None: + raise ValueError(f"Invalid JSON constant: {value}") + try: - return json.loads(post_data) + return json.loads(post_data, parse_constant=reject_json_constant) except Exception: raise Error("POST data is not a valid JSON object: " + post_data) diff --git a/tests/async/python/test_network.py b/tests/async/python/test_network.py index f44ae66b1..7b029f1d2 100644 --- a/tests/async/python/test_network.py +++ b/tests/async/python/test_network.py @@ -17,7 +17,9 @@ import asyncio from typing import List -from playwright.async_api import Page, Request, Route +import pytest + +from playwright.async_api import Error, Page, Request, Route from tests.server import Server @@ -104,6 +106,31 @@ async def test_should_parse_the_json_post_data_when_content_type_header_is_missi assert request.post_data_json == {"value": 42} +async def test_should_reject_non_standard_json_constants_in_post_data( + page: Page, server: Server +) -> None: + await page.goto(server.EMPTY_PAGE) + + for value in ["NaN", "Infinity", "-Infinity"]: + async with page.expect_request("**/*") as request_info: + await page.evaluate( + """({url, value}) => { + return fetch(url, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: value, + }); + }""", + {"url": server.PREFIX + "/title.html", "value": value}, + ) + + request = await request_info.value + with pytest.raises( + Error, match="POST data is not a valid JSON object" + ): + request.post_data_json + + async def test_response_security_details_none_without_https( page: Page, server: Server ) -> None: