Skip to content

fix: very long decimal character references no longer crash rendering or citry check - #304

Merged
JuroOravec merged 1 commit into
mainfrom
fix/long-character-references
Oct 23, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/long-character-references

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #291

What was wrong

Python refuses to turn a string of more than 4,300 decimal digits into an
int, and Python's html.unescape does exactly that for every &#...;
reference. A browser has no such limit: it skips leading zeros and reads
any value past U+10FFFF as U+FFFD. So a reference such as &# followed
by 5,000 zeros and 106; (which a browser reads as j) raised
ValueError wherever Citry decoded it:

  • citry check (static or with --app) on a template attribute holding
    it;
  • an interactive component printing it in a Markup value;
  • a Markup value bound with c-href, c-title or another c-
    attribute, which fails while the component renders;
  • every HTMLParser that reads rendered output (below).

The fix

A new helper, citry.util.html.decode_character_references, decodes
exactly as html.unescape does, except for numeric references too long
for Python's int(): it drops leading zeros before converting, and it
reads a value with more than seven significant decimal digits (more than
six hex digits) as U+FFFD without converting it. That is the rule #289 uses in Rust for the output checks
(parse_code_point in crates/citry_html_transform/src/output_policy.rs).

Every decoder in packages/py/citry now uses it:

  • decode_attribute_entities (numeric branch), used by citry check,
    the template lint and escape_attribute_value;
  • _vue/direct_capture.py (Markup text, attribute text, root markers);
  • _vue/capture.py, citry_render.py, ext/i18n/bindings.py.

grep -rn "unescape(" packages/py/citry/citry now finds only the helper
itself.

Decoders inside HTMLParser

Python's HTMLParser calls html.unescape on every attribute value it
reads (and on text with convert_charrefs=True), so the same crash came
from every parser Citry and citry-ui run over rendered output, for
example an authored <textarea title="&#000…0106;"> in a page that
serializes without JavaScript (serialize.py), or the reference in a
<c-CDisclosure> title fill.

citry.util.html.LongReferenceSafeParser is an HTMLParser whose
feed rewrites each decimal reference too long for int() as text of the
same length that html.unescape decodes alike, then hands the text to
HTMLParser.feed. The rewrite is the short reference (&#106;, or
&#1114112; for a value past U+10FFFF) padded with &#1; references,
which html.unescape decodes to nothing. Positions, line and column
numbers and start-tag lengths stay valid, which matters because
_PreloadHeadLocator uses offsets. A chunk that ends in &# and digits
without ; keeps that tail back until the next feed or close,
because more digits may follow and HTMLParser would otherwise decode it
too early. Input without such a reference reaches HTMLParser.feed
unchanged (apart from that held tail), so CPython's own buffering of
small chunks still applies; the overhead measured under 2% on a 1 MB
page. On Python 3.10.0 to 3.10.6, which have no digit limit and no
sys.get_int_max_str_digits, nothing is rewritten.

Parsers moved onto it: in citry _PreparedMarkupTracker
(serialize.py), _IdScanner (_page_ids.py), _PreloadHeadLocator
and _HostValidator (_vue/serialization.py); in citry-ui
caccordion, ccommand_palette, cdisclosure (both parsers), cimage,
clistbox, cmenu and ctree. Not moved: the docs site and
scripts/vue_render_parity parse the project's own pages, and the test
helpers parse fixed test output.

Text the parser does not decode (<script> content, comments,
get_starttag_text()) shows the padded form of such a reference, and with
convert_charrefs=False handle_charref receives its pieces. None of the
twelve parsers reads those, and it only happens for a reference of more
than 4,300 digits.

Other digit strings Python cannot convert

  • An Events timing such as .debounce.<5,000 digits>ms or
    @c-poll.<5,000 digits>s raised the digit-limit ValueError. A number
    with more than 16 significant digits is past the JavaScript safe-integer
    limit anyway, so it now gets the existing "timing exceeds the JavaScript
    safe-integer limit" error (ext/events/bindings.py).
  • The timing pattern used \d, which also matches digits from other
    writing systems (.debounce.٥٠ms read as 50 ms). It now takes ASCII
    0-9 only, here and in the editor's matching pattern
    (citry_lsp/engine.py).
  • The x-citry-vue-revision request header was checked with isdigit(),
    which also accepts digits from other writing systems such as
    Arabic-Indic ٣, and then converted with int(). A header that is not
    a JavaScript safe integer in ASCII digits is now refused with the same
    error as a missing header (_vue/events.py).

Sweep of int( on user or template text in packages/py: the Events
schema (ext/events/schemas.py), the JSON protocol reader
(_protocol/events/issues.py, via json.loads), the carrier
sendSequence (_protocol/events/carriers.py) and WSGI's
CONTENT_LENGTH (contrib/wsgi.py) already catch the ValueError and
report or fall back. Five citry-ui input coercions (ctextarea,
ctags_input, cgrid, cdisclosure and caccordion heading_level)
convert an authored decimal string with int() and would raise Python's
ValueError instead of the component's own error for a value of more
than 4,300 digits. They are left as they are: the input is absurd and the
error still names the problem.

Relation to #289

#289 (merged) moved the strict-CSP checks to Rust, whose
parse_code_point already follows this rule, so this branch, rebased on
it, no longer touches _csp_validation.py. #289's test reference decoder
(tests/_output_policy_reference.py, _bounded_decimal_reference) keeps
its own copy of the rule as an independent check of the Rust code.

Tests

tests/test_long_character_references.py:

  • the helper and decode_attribute_entities on long references (leading
    zeros, no ;, hex, values past U+10FFFF), and the helper against
    html.unescape on short references;

  • citry check --static on a template with the reference;

  • an interactive component printing it in Markup, on both render paths;

  • an authored href and a Markup c-href holding
    &#000...0106;avascript:alert(1), which the strict CSP check refuses as
    a javascript: URL (the authored one already passes on main since
    perf: run the strict CSP and JavaScript-policy checks in Rust (560 ms to 99 ms on a large page) #289; the Markup one crashed while rendering).

  • LongReferenceSafeParser on attributes and text, split across chunks,
    with positions compared to Python's parser on the short reference, and
    each citry and citry-ui output parser checked to use it;

  • a static <textarea title> with the reference, and the reference in a
    CDisclosure title;

  • a .debounce/@c-poll timing with 5,000 digits, and bad revision
    headers (5,000 digits, 17 digits, past 2**53 - 1, Arabic-Indic digits,
    negative, empty).

The behavior tests crash or fail on main, except the negative and empty
revision headers, which main already refused.

Release note

citry-ui's components now import LongReferenceSafeParser from citry, so
citry-ui does not work with citry 0.6.0. This PR keeps citry-ui's
citry>=0.6.0 floor (packages/py/citry_ui/pyproject.toml) as it is,
because the release PR owns version values: the release must raise that
floor to the citry version that ships decode_character_references and
LongReferenceSafeParser.

🤖 Generated with Claude Code

Python refuses to convert more than 4,300 decimal digits to an int, and
html.unescape converts every decimal reference that way, so a reference
such as `&#` + 5,000 zeros + `106;` raised ValueError in `citry check`, in
an interactive component printing it in Markup, when a Markup value was
bound to a `c-` attribute (escape_attribute_value), and in every
HTMLParser Citry and citry-ui run over output (HTMLParser calls
html.unescape itself). The strict CSP check already follows this rule in
Rust since #289.

- New citry.util.html.decode_character_references: decodes as
  html.unescape does, but drops a numeric reference's leading zeros first
  and reads a value with more than seven significant decimal digits (more
  than six hex digits) as U+FFFD, the browser's reading of a value past
  U+10FFFF. This is the rule of #289's Rust output checks
  (parse_code_point). decode_attribute_entities' numeric branch and every
  html.unescape call in packages/py/citry use it.
- New LongReferenceSafeParser: an HTMLParser that holds back a reference
  that may continue in the next chunk and rewrites each decimal reference
  too long for int() as text of the same length that html.unescape
  decodes alike (the short reference padded with `&#1;`, which it decodes
  to nothing), then hands the text to HTMLParser.feed. Positions stay
  valid, and input without such a reference parses as before. The four
  citry output parsers and the eight citry-ui ones use it.
- Siblings with the same int() limit: an Events timing such as
  `.debounce.<5,000 digits>ms` now reports the JavaScript safe-integer
  limit (and the timing pattern, here and in citry-lsp, takes ASCII digits
  only), and an `x-citry-vue-revision` header that is not a JavaScript
  safe integer in ASCII digits is refused as a missing header.
- Tests for each, failing on main.

Fixes #291

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit f2d4a0b into main Oct 23, 2026
26 of 27 checks passed
@JuroOravec
JuroOravec deleted the fix/long-character-references branch October 23, 2026 20:48
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 decimal character reference with more than 4,300 digits crashes citry check and interactive renders

1 participant