fix: sanitize lone Unicode surrogates in YAML serialization#39316
Open
alexmakeev wants to merge 1 commit intomicrosoft:mainfrom
Open
fix: sanitize lone Unicode surrogates in YAML serialization#39316alexmakeev wants to merge 1 commit intomicrosoft:mainfrom
alexmakeev wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Lone surrogates (U+D800–U+DFFF) in DOM textContent are valid in JavaScript strings but produce invalid JSON when serialized. The yamlEscapeValueIfNeeded() function escapes control characters but does not handle lone surrogates, causing 'no low surrogate in string' errors in MCP clients. Apply the same toWellFormed() pattern already used in driver.ts for non-JavaScript language bindings, with a regex fallback for older environments. Fixes microsoft/playwright-mcp#1410
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
yamlEscapeValueIfNeeded()inpackages/injected/src/yaml.tsescapes control characters (\x00-\x1f,\x7f-\x9f) but does not handle lone Unicode surrogates (\uD800-\uDFFF).When a page assigns text containing lone surrogates to DOM nodes (e.g. via
element.textContent = 'text\uD800more'), the aria snapshot captures these verbatim. The resulting YAML string produces invalid JSON per RFC 8259 §8.2, causing"no low surrogate in string"errors in MCP clients.Fix
Apply the same
String.prototype.toWellFormed()pattern already used inpackages/playwright-core/src/cli/driver.tsfor non-JavaScript language bindings, with a regex fallback for environments withouttoWellFormed().Lone surrogates are replaced with U+FFFD (Unicode replacement character). Valid surrogate pairs (emoji, etc.) are preserved.
Changes
packages/injected/src/yaml.ts— sanitize lone surrogates inyamlEscapeValueIfNeeded()before escaping control characterstests/mcp/snapshot-unicode.spec.ts— test coverage for lone high/low surrogates, valid emoji preservation, and mixed contentTest plan
\uD800) → replaced with\uFFFD\uDC00) → replaced with\uFFFD\uD83D\uDE00= 😀) → preservedRef: microsoft/playwright-mcp#1410