Skip to content

fix(python): reject non-standard JSON constants in request post data - #3376

Closed
himanshug189-sketch wants to merge 1 commit into
microsoft:mainfrom
himanshug189-sketch:fix-request-post-data-json-parsing
Closed

himanshug189-sketch wants to merge 1 commit into
microsoft:mainfrom
himanshug189-sketch:fix-request-post-data-json-parsing

Conversation

@himanshug189-sketch

Copy link
Copy Markdown

Summary

This PR fixes an issue in Request.post_data_json where Python's JSON parser accepts non-standard JSON constants (NaN, Infinity, and -Infinity) instead of rejecting them.

The behavior is inconsistent with JavaScript's JSON.parse(), which rejects these values as invalid JSON.

Problem

Python's built-in json.loads() accepts the following non-standard JSON constants by default:

  • NaN
  • Infinity
  • -Infinity

As a result, accessing Request.post_data_json could return Python floating-point values (nan, inf, and -inf) instead of raising an error for invalid JSON input.

Changes Made

  • Added a custom parse_constant handler to reject non-standard JSON constants.
  • Ensured invalid JSON continues to raise Playwright's Error with the existing error message.
  • Added regression test coverage for NaN, Infinity, and -Infinity.

Test Coverage

Added a regression test covering all three non-standard JSON constants.

Validation performed:

  • tests/async/python/test_network.py — 18 tests passed.
  • Verified across Chromium, Firefox, and WebKit.
  • Python compilation check passed.
  • git diff --check passed.

Expected Behavior

Request.post_data_json should reject non-standard JSON constants and raise a Playwright Error, consistent with JavaScript JSON parsing behavior.

Related Issue

Related to #3375.

Note: Submitted as a Draft PR for maintainer feedback and review.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants