Repository navigation
fix: leave Vue string literals holding a lone surrogate to the browser - #349
Merged
Merged
Conversation
A Vue expression such as `v-text="'a\ud800b'"` made the server write U+FFFD followed by `d800` into the page. A lone surrogate is half of a UTF-16 pair with no partner. A Rust string cannot hold one, so oxc stores such a string literal in its own encoding and sets `StringLiteral::lone_surrogates`, and Citry read that encoding as the value. The page's UTF-8 HTML cannot hold the value either, so the server cannot write what Vue renders in the browser. Every read of an oxc string literal in the Vue compiler crate now goes through `literal_text` or `key_name`, which return nothing for such a literal. The server render leaves such a value to Vue: it declines it as `text-value` for a string and `browser-value` for a key it cannot look up, or writes the element empty for a dynamic `v-text`. A comparison or a condition that reads such a literal is now left to the browser too. The hydration plan treats the render as one it cannot read. The component source analysis marks a `props`, `inject`, `emits` or option section holding such a name as unknown, and leaves such a name out of the scope writes it records. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Fixes #296
What was wrong
In a component with
js, Citry renders Vue directives on the server. AVue expression holding a lone surrogate escape (half of a UTF-16 pair,
with no partner), such as
v-text="'a\ud800b'", put U+FFFD followed bythe escape's hex digits into the server's HTML, as
<p>a\ufffdd800b</p>(U+FFFD, then
d800). In the browser, Vue renders a string holdingU+D800, so the server's text did not match what Vue shows.
The server reads the render code that Vue's compiler generates by parsing
it with oxc, a JavaScript parser written in Rust. A Rust string cannot
hold a lone surrogate, so oxc stores such a literal in its
own encoding and sets
StringLiteral::lone_surrogates. Citry readliteral.valuewithout checking that flag. The page's UTF-8 HTML cannothold the value either, so the server cannot write it.
The fix
Every place that takes a value or a name from an oxc string literal now
checks for such a literal first, mostly through one of two helpers, which
return nothing for it:
literal_textreads a string literal's text;key_namereads the fixed name of an object key, including a string ortemplate literal key.
They live in
crates/citry_vue_compiler/src/lib.rs, andcrates/citry_template_parser/src/browser.rshas a copy of each. When ahelper returns nothing, each part does this:
server_render.rs): a string value is declined astext-value, the code it already uses for other text it cannot write asVue does. A key it cannot hold, as in
obj['\ud800'], is abrowser-only value (
browser-value). A dynamicv-textis writtenempty, as for any value only the browser knows, and hydration sets it.
Tags, comment text, slot names and prop key lists from the render code
give
render-shape; of these, only a slot name, such as<slot :name="'\ud800'">, can hold such a literal from a template.hydration.rs): a tag, comment, component name,slot name or prop key holding such a literal stops the plan, as for
render code it cannot read.
browser.rs): aprops,injectoremitslist, or an option section, that holds such a name is markedunknown, as a non-literal entry already is. Such a name is also left
out of the scope writes it records.
A literal without a lone surrogate reads as before, including an escape
for a complete character such as
'e\u00e9f'and a real U+FFFD.One case that rendered before now goes to the browser: a comparison or a
condition that reads such a literal, as in
v-if="'\ud800' === 'x'". The old code compared the two encodedstrings. That gave the right answer only because both sides used the same
encoding, and it could be wrong:
'\ud800' === '\ufffdd800'came outtrue. Keeping the real UTF-16 value is not worth it for a case this rare.
The
HydrationDeclinedocstring anddocs/design/vue_ssr_selected_tree_plan.mdnow name the case.Tests
server_render.rs,a_lone_surrogate_in_a_string_literal_is_left_to_the_browser:v-text,:title,{{ }}and a member key, each with the HTML and thedeclines, plus
\u00e9, which renders.hydration.rs,a_string_literal_holding_a_lone_surrogate_stops_the_plan:render code with such a tag, comment, component name and prop key.
browser.rs,names_holding_a_lone_surrogate_leave_their_section_unknownand
scope_writes_skip_names_holding_a_lone_surrogate.test_vue_hydration_regions.py,test_a_lone_surrogate_escape_in_a_vue_expression_is_left_to_the_browser:renders the issue's example and checks the HTML and the declines.
Each test fails without the fix.
Changelog: a Fixes line in
CHANGELOG.md.🤖 Generated with Claude Code