Skip to content

fix: serialize integers beyond safe range as BigInt (#3384) - #3390

Closed
Akash Sarangi (Roxen-AS) wants to merge 1 commit into
microsoft:mainfrom
Roxen-AS:fix/serialize-large-ints-bigint
Closed

Akash Sarangi (Roxen-AS) wants to merge 1 commit into
microsoft:mainfrom
Roxen-AS:fix/serialize-large-ints-bigint

Conversation

@Roxen-AS

Copy link
Copy Markdown

Fixes #3384

Problem

When passing large integers (such as integers beyond JavaScript's safe integer range ±(2**53 - 1), 64-bit nanosecond timestamps, database IDs, or Snowflake IDs) to page.evaluate(), _inner_serialize_value previously serialized all integers as standard Numbers {"n": value}.
When serialized to JSON and handled by the JavaScript runtime, values outside the safe integer range are silently rounded to the nearest IEEE 754 double float, causing silent precision loss and data corruption.

Solution

  • In playwright/_impl/protocol/serializers.py, serialize integers strictly outside ±(2**53 - 1) (abs(value) > MAX_SAFE_INTEGER) as {"bi": str(value)}.
  • Integers within the safe integer range continue to serialize as standard Numbers {"n": value}, preserving standard JavaScript arithmetic behavior.
  • The Playwright protocol already supports {"bi": ...} (deserializing to a JavaScript BigInt on the page and serializing back as {"bi": ...}), allowing large integers to round-trip with exact precision.
  • Added regression tests in tests/async/python/test_page_evaluate.py.

Verification

  • Tested serialization of safe and unsafe integers, booleans, and large ints (2**53 + 1, 2**64, -2**63, 2**70).
  • Ran pytest tests/async/python/test_page_evaluate.py -k test_serializers_large_integers across Chromium, Firefox, and WebKit (3 passed).
  • Verified flake8 playwright/_impl/protocol/serializers.py tests/async/python/test_page_evaluate.py (0 warnings).
  • Verified python scripts/check_test_registry.py (0 issues).
  • Signed-off commit with GPG.

Integers outside JavaScript safe integer range ±(2**53 - 1) were
previously sent as standard Numbers, causing silent rounding and precision loss
for large integers such as 64-bit timestamps and Snowflake IDs.

Serialize integers outside this range as {"bi": str(value)}, allowing exact
round-trips as BigInt without precision loss.

Fixes microsoft#3384

Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
@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.

[Bug]: Python ints beyond 2**53 are silently corrupted when passed to page.evaluate()

2 participants