Skip to content

A \ud800 escape in a Vue expression renders as "�d800" in the server's HTML #296

Closed

Description

@JuroOravec

What happens

In a component with js, Citry renders Vue directives such as v-text
and :title on the server. When a Vue expression holds a JavaScript
string with a lone surrogate escape (\ud800 to \udfff with no partner),
the server's HTML holds U+FFFD followed by the escape's four hex digits:

from citry import Citry, Component

app = Citry(autodiscover=False)

class Card(Component):
    citry = app
    template = r"""
        <p v-text="'a\ud800b'"></p>
        <p :title="'c\udc00d'">x</p>
        <p v-text="'e\u00e9f'"></p>
    """
    js = """
        $component({data() { return {}; }});
    """

print(Card().render().serialize())

The <p> elements in the output, printed with ascii():

'<p>a\ufffdd800b</p>'
'<p title="c\ufffddc00d">x</p>'
'<p>e\xe9f</p>'

The \u00e9 escape renders as é, as expected. In the browser, Vue
evaluates 'a\ud800b' to a three-character string holding U+D800, so the
text the server wrote does not match what Vue renders. Run on #288's
branch (fix/sandbox-surrogate-literals), which does not change this
crate, so main behaves the same.

Cause

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 string literal's value in its
own encoding and sets StringLiteral::lone_surrogates: each lone
surrogate becomes U+FFFD followed by its hex code unit, and a real U+FFFD
becomes U+FFFD followed by fffd (oxc_ast 0.143.0,
src/ast/literal.rs:106-113). Citry reads literal.value without
checking that flag, so oxc's encoding reaches the output.

On main, literal.value is read in:

  • crates/citry_vue_compiler/src/server_render.rs:784 (read_expr, the
    path in the example), :456 (comment text), :538 (an array of string
    keys) and :915 (computed property names);
  • crates/citry_vue_compiler/src/hydration.rs:581 (comment text), :714
    (computed property names) and :820 (an array of string keys);
  • crates/citry_template_parser/src/browser.rs:1215 and :1767, which
    read the names in a component's props and inject lists.

The page's HTML is UTF-8, which cannot hold a lone surrogate either, and a
browser decodes &#xD800; as U+FFFD. So the server cannot write the
value Vue will render.

Who hits it

Only templates that write a lone surrogate escape in a Vue expression,
which is very rare. The result is wrong text in the server's HTML and a
hydration mismatch in the browser, not an error.

Fix direction

Where Citry reads a StringLiteral from oxc, check lone_surrogates.
When it is set, treat the expression as one the server cannot render: in
server_render.rs return the existing Unsupported result, and in
hydration.rs the existing unsupported error, so the page takes the
path it already takes for render code the server does not read. For the
props and inject names in browser.rs, mark the list unknown, as a
non-literal entry already does. Add one test per file with a \ud800
literal, and one Python test that renders the example and checks that
the server does not write U+FFFD followed by d800.

Found while working on #288 (fixes #280).

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions