Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion playwright/_impl/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 28 additions & 1 deletion tests/async/python/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down