Repository navigation
fix: every evaluator reads a template string literal the same way (surrogate escapes, f-string text) - #288
Merged
Conversation
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]>
This was referenced Oct 23, 2026
Closed
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 #280
What was wrong
A template expression such as
{{ row['\ud800'] }}read the key U+D800on 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 expressionwith 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
\ud800to 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:\uor\Uwith a code point from D800 to DFFF) is written into the generatedcode from its own source text, and Python decodes the escape when the
code runs. The new
utils/literal_source.rsholds the helpers. Thecode 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
\uis not an escape in them.the same rule. The sandbox turns an f-string into a
str.formattemplate; text holding such an escape goes in as a
{}argument to thesandbox's
format()function, so the template itself never holds it.text from decoded values, and are fixed in the same place:
f'{{a}}'raisedKeyError, because the doubled braces reachedstr.formatas one brace. Literal braces now go into the templatedoubled.
'x' f'{y}', was dropped.It is now part of the text.
f'{x=}', dropped itsx=textand showed
str()instead ofrepr(). It now writes its source textand uses
repr()when there is no conversion or format spec, asPython does (t-strings too).
backslash (
f'\ud800\{x}'); that backslash is doubled so it cannotescape the closing quote. The adversarial review found this case.
packages/py/citry/citry/_vue/native_walk.py(level C2, the step of theRust render path that reads every row of a
c-forloop 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 andsurrogate 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.mdnow give only that reason.Related cases checked
\x..,\N{...}(including aliases suchas
\N{BYTE ORDER MARK}), octal escapes above\377instrandbytes, line continuations,\U0001F600and a literal U+FFFD all readthe same in the sandbox and in Python.
fireadsfiin both.'\ud800'as written.expressionattribute is''in the sandbox and the source text in Python. That is an existing TODO
in the transformer, unrelated to literals.
(
citry_vue_compiler, which uses oxc) also turns a lone surrogate in aVue-syntax string into U+FFFD when it renders on the server
(
server_render.rs,hydration.rs,browser.rs). That code reads Vueexpressions, 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 unittests for the escape scanner and the f-string body conversion.
citry_core: 27 expressions where the sandbox's result must equaleval().citry:test_sandbox_setting.pyrenders text,or '', an attribute,c-if, a child component input, twoc-forforms and an f-string, inan 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 whencitry_coreis built frommain.🤖 Generated with Claude Code