Skip to content

fix(engine): accept lone surrogate escapes in JSON.parse - #5578

Merged
HalidOdat merged 1 commit into
boa-dev:mainfrom
cristianchiriac:fix-json-parse-lone-surrogate
Oct 12, 2026
Merged

HalidOdat merged 1 commit into
boa-dev:mainfrom
cristianchiriac:fix-json-parse-lone-surrogate

Conversation

@cristianchiriac

Copy link
Copy Markdown
Contributor

This Pull Request fixes/closes #5576.

It changes the following:

  • JSON.parse checked that its input is valid JSON by deserializing it into a serde_json::Value. That builds Rust Strings, so lone surrogate escapes such as "\ud83d" were rejected (unexpected end of hex escape), although they are valid JSON (ECMA-404) and JSON.parse must return a one-code-unit string for them.
  • The check now uses serde_json::from_str::<serde::de::IgnoredAny>, which only validates the syntax without decoding strings into Rust Strings. Boa's own JSON-mode parser, which already ran afterwards, builds the actual UTF-16 strings, so lone surrogates come through correctly. Invalid JSON still throws a SyntaxError with serde's message.
  • Added json_parse_lone_surrogate_escapes, which checks a lone high surrogate, a lone low surrogate, one inside an array, and a valid surrogate pair. Also added json_parse_invalid_text_still_throws_syntax_error for an incomplete \u escape.

Testing: cargo test -p boa_engine --lib json → 44 passed. Before the change, the new test fails with Uncaught SyntaxError: unexpected end of hex escape at line 1 column 8. cargo fmt --check is clean.

The repo's pre-push hook (cargo make run-ci: fmt check + clippy with all features and with no features) passes.

JSON.parse validated its input by deserializing it into a
serde_json::Value, which builds Rust strings and so rejects lone
surrogate escapes such as "\ud83d", although they are valid JSON.
Only check the syntax, with serde::de::IgnoredAny; Boa's own JSON parser
then builds the UTF-16 strings.

Fixes #5576
@cristianchiriac
cristianchiriac requested a review from a team as a code owner October 11, 2026 23:33
@github-actions github-actions Bot added the Waiting On Review Waiting on reviews from the maintainers label Oct 11, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Oct 11, 2026
@github-actions github-actions Bot added C-Tests Issues and PRs related to the tests. C-Builtins PRs and Issues related to builtins/intrinsics labels Oct 11, 2026
@github-actions

Copy link
Copy Markdown

Test262 conformance changes

Test result main count PR count difference
Total 53,578 53,578 0
Passed 51,447 51,448 +1
Ignored 1,648 1,648 0
Failed 483 482 -1
Panics 0 0 0
Conformance 96.02% 96.02% +0.00%
Fixed tests (1):
test/staging/sm/JSON/parse-with-source.js (previously Failed)

Tested main commit: 64be2e138a44672de3f01f67065e9645a16d3ccb
Tested PR commit: 57d3efaf0776a550f324ba8a457b9f33b4f30945
Compare commits: 64be2e1...57d3efa

@codecov

codecov Bot commented Oct 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.00%. Comparing base (6ddc2b4) to head (57d3efa).
⚠️ Report is 1070 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #5578       +/-   ##
===========================================
+ Coverage   47.24%   63.00%   +15.76%     
===========================================
  Files         476      536       +60     
  Lines       46892    60315    +13423     
===========================================
+ Hits        22154    38003    +15849     
+ Misses      24738    22312     -2426     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@HalidOdat HalidOdat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks perfect to me! Thank you for the contribution!😄

@HalidOdat
HalidOdat added this pull request to the merge queue Oct 12, 2026
Merged via the queue into boa-dev:main with commit 63a67e6 Oct 12, 2026
22 checks passed
@github-actions github-actions Bot removed the Waiting On Review Waiting on reviews from the maintainers label Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Builtins PRs and Issues related to builtins/intrinsics C-Tests Issues and PRs related to the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON.parse rejects a valid lone UTF-16 surrogate escape

2 participants