Skip to content
Draft
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
14 changes: 12 additions & 2 deletions playwright/_impl/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,8 +1383,18 @@ def headers_array(self) -> HeadersArray:


def _json_stringify(value: Any) -> str:
# JSON.stringify(value): no spaces, and no escapes for non-ASCII characters.
return json.dumps(value, allow_nan=False, ensure_ascii=False, separators=(",", ":"))
# Python: JSON.stringify escapes lone surrogates, but preserves valid pairs.
def escape_surrogate(match: "re.Match[str]") -> str:
text = match[0]
if len(text) == 2:
return text.encode("utf-16-le", "surrogatepass").decode("utf-16-le")
return f"\\u{ord(text):04x}"

return re.sub(
r"[\ud800-\udbff][\udc00-\udfff]|[\ud800-\udfff]",
escape_surrogate,
json.dumps(value, allow_nan=False, ensure_ascii=False, separators=(",", ":")),
)


def _js_typeof(value: Any) -> str:
Expand Down
15 changes: 15 additions & 0 deletions tests/async/python/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ async def test_response_headers_should_work(page: Page, server: Server) -> None:
async def test_should_parse_the_json_post_data_when_content_type_header_is_missing(
page: Page, server: Server
) -> None:
# Regression test for https://github.com/microsoft/playwright-python/issues/3374.
await page.goto(server.EMPTY_PAGE)
async with page.expect_request("**/*") as request_info:
await page.evaluate(
Expand All @@ -103,6 +104,20 @@ async def test_should_parse_the_json_post_data_when_content_type_header_is_missi
assert "content-type" not in request.headers
assert request.post_data_json == {"value": 42}

route_future: "asyncio.Future[Route]" = asyncio.Future()
await page.route("**/title.html", lambda route: route_future.set_result(route))
await page.evaluate("() => { fetch('/title.html', {method: 'POST'}); }")
route = await route_future
_, server_request = await asyncio.gather(
route.continue_(
post_data={"text": "\ud800|\udfff|\ud83d\ude00|\u00e9|\U0001f600"}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to provide bytes instead of text here to achieve the same post_body result?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Pre-serialized UTF-8 bytes produce the same post_body on Chromium and Firefox, even without the fix. I left the dictionary input because bytes bypass JSON serialization and would not test this regression.

Revised on Linux by the Playwright bot - agent run

),
server.wait_for_request("/title.html"),
)
assert server_request.post_body == (
'{"text":"\\ud800|\\udfff|\U0001f600|\u00e9|\U0001f600"}'.encode("utf-8")
)


async def test_response_security_details_none_without_https(
page: Page, server: Server
Expand Down
Loading