Skip to content

fix: every evaluator reads a template string literal the same way (surrogate escapes, f-string text) - #288

Merged
JuroOravec merged 3 commits into
mainfrom
fix/sandbox-surrogate-literals
Oct 23, 2026
Merged

JuroOravec merged 3 commits into
mainfrom
fix/sandbox-surrogate-literals

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #280

What was wrong

A template expression such as {{ row['\ud800'] }} read the key U+D800
on some renders and U+FFFD (the replacement character) on others. Citry
runs a template expression through one of three evaluators: the
expression sandbox (the default), the generated leaf evaluator (Python
code Citry builds from Python's own parse for simple paths such as
row['id']), or plain Python (sandbox_expressions=False). The sandbox parses the expression
with ruff's parser in Rust, rewrites it and writes new Python code for
it. A Rust string cannot hold a lone surrogate, so ruff decodes \ud800
to U+FFFD, and the sandbox wrote the literal back from that decoded
value. The other two evaluators keep U+D800.

What the fix changes

crates/python_safe_eval:

  • A string literal whose source holds a lone surrogate escape (\u or
    \U with a code point from D800 to DFFF) is written into the generated
    code from its own source text, and Python decodes the escape when the
    code runs. The new utils/literal_source.rs holds the helpers. The
    code generator writes a name's identifier as it is, so the transformer
    places the source text in a name node. Raw strings and bytes literals
    are left alone, because \u is not an escape in them.
  • F-string and t-string text, and static and dynamic format specs, follow
    the same rule. The sandbox turns an f-string into a str.format
    template; text holding such an escape goes in as a {} argument to the
    sandbox's format() function, so the template itself never holds it.
  • Three related bugs had the same cause, the sandbox rebuilding f-string
    text from decoded values, and are fixed in the same place:
    • f'{{a}}' raised KeyError, because the doubled braces reached
      str.format as one brace. Literal braces now go into the template
      doubled.
    • A plain literal joined to an f-string, 'x' f'{y}', was dropped.
      It is now part of the text.
    • A self-documenting interpolation, f'{x=}', dropped its x= text
      and showed str() instead of repr(). It now writes its source text
      and uses repr() when there is no conversion or format spec, as
      Python does (t-strings too).
  • Text cut from an f-string to be written as its own literal can end in a
    backslash (f'\ud800\{x}'); that backslash is doubled so it cannot
    escape the closing quote. The adversarial review found this case.

packages/py/citry/citry/_vue/native_walk.py (level C2, the step of the
Rust render path that reads every row of a c-for loop in one Rust call)
turned each expression into a path twice, from Python's parse and from
the sandbox's generated code, and refused it unless both agreed, because
of this split. I compared the two readings on 29 path expressions
(number literal forms, implicit concatenation, \N{...}, \x, octal and
surrogate escapes, multi-line strings, comments, NFKC-normalized names),
and they agree on every one now. So the path now comes from Python's
parse alone, after the sandbox compiles the text (an expression the
sandbox refuses still gets no plan, so Python's walk raises the error).
The tests that faked a different generated reading are removed with the
code they tested.

Kept on purpose: level A (the leaf row reader) and C2 still refuse a key
that holds a lone surrogate, because no Rust string can hold it. Their
comments and docs/design/native_render.md now give only that reason.

Related cases checked

  • Other escapes ruff decodes: \x.., \N{...} (including aliases such
    as \N{BYTE ORDER MARK}), octal escapes above \377 in str and
    bytes, line continuations, \U0001F600 and a literal U+FFFD all read
    the same in the sandbox and in Python.
  • Identifiers: ruff normalizes identifiers as Python does (NFKC), so
    fi reads fi in both.
  • Citry's template formatter formats from source text, so it keeps
    '\ud800' as written.
  • Left as is: a t-string interpolation's expression attribute is ''
    in the sandbox and the source text in Python. That is an existing TODO
    in the transformer, unrelated to literals.
  • Left as is, a separate surface: the Vue compiler crate
    (citry_vue_compiler, which uses oxc) also turns a lone surrogate in a
    Vue-syntax string into U+FFFD when it renders on the server
    (server_render.rs, hydration.rs, browser.rs). That code reads Vue
    expressions, not Python ones, and needs its own issue.

Tests

  • python_safe_eval (Rust): the generated code for plain, concatenated,
    f-string, t-string, static-spec and dynamic-spec cases, the cases left
    unchanged ('\\ud800', raw, bytes, raw f-string), the brace,
    concatenation and {x=} fixes, and the trailing backslash, plus unit
    tests for the escape scanner and the f-string body conversion.
  • citry_core: 27 expressions where the sandbox's result must equal
    eval().
  • citry: test_sandbox_setting.py renders text, or '', an attribute,
    c-if, a child component input, two c-for forms and an f-string, in
    an ordinary component (8 templates) and a simple = "vue" component
    (the 4 it supports), with the sandbox on and off, on both render paths
    (render_both). The six sandbox-on cases that read U+FFFD fail when
    citry_core is built from main.

🤖 Generated with Claude Code

JuroOravec and others added 2 commits October 23, 2026 18:11
ruff's parser decodes `\ud800`-style escapes to U+FFFD because a Rust
string cannot hold a lone surrogate, and the expression sandbox wrote
every literal back from the decoded value. So `row['\ud800']` read the key
U+FFFD under the sandbox and U+D800 under the leaf evaluator and with
`sandbox_expressions=False`, and an ordinary component's key depended
on which evaluator its template got.

- python_safe_eval: a string literal holding a lone surrogate escape is
  written into the generated code from its source text (new
  utils/literal_source.rs), and Python decodes it when the code runs. The
  same applies to f-string and t-string text and to static and dynamic
  format specs; f-string text holding such an escape is passed to the
  sandbox's format() function as a `{}` argument.
- Three related bugs had the same cause, the sandbox rebuilding f-string
  text from decoded values, and are fixed here too: literal braces now go
  into the `str.format` template doubled (`f'{{a}}'` raised KeyError), a
  plain literal joined to an f-string (`'x' f'{y}'`) is no longer dropped,
  and `f'{x=}'` writes its `x=` text and uses repr() as Python does.
- native_walk (level C2, the Rust loop walk): the sandbox and Python now
  read a plain path alike, so the path is built from Python's parse alone,
  after the sandbox compiles the text. Level C2 and level A (the Rust leaf
  row reader) still refuse surrogate keys, because no Rust string can hold
  them.
- Tests: generated-code tests in python_safe_eval, a test that the
  sandbox gives what eval() gives in citry_core, and a test rendering
  text, an attribute, c-if, a child input, c-for and an f-string in
  ordinary and simple="vue" components, sandbox on and off, on both
  render paths.

Fixes #280

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y on Python 3.12+

With the sandbox off, Python itself compiles the expression, and it
accepts a reused quote and a backslash inside an f-string's replacement
field only from 3.12. The sandbox case still runs on every version.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit cf4651f into main Oct 23, 2026
29 checks passed
@JuroOravec
JuroOravec deleted the fix/sandbox-surrogate-literals branch October 23, 2026 19:35
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.

A lone surrogate escape such as '\ud800' in a template reads a different key depending on which evaluator runs it

1 participant