Repository navigation
fix: very long decimal character references no longer crash rendering or citry check - #304
Merged
Merged
Conversation
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 ``, 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]>
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 #291
What was wrong
Python refuses to turn a string of more than 4,300 decimal digits into an
int, and Python'shtml.unescapedoes 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
&#followedby 5,000 zeros and
106;(which a browser reads asj) raisedValueErrorwherever Citry decoded it:citry check(static or with--app) on a template attribute holdingit;
Markupvalue;Markupvalue bound withc-href,c-titleor anotherc-attribute, which fails while the component renders;
HTMLParserthat reads rendered output (below).The fix
A new helper,
citry.util.html.decode_character_references, decodesexactly as
html.unescapedoes, except for numeric references too longfor Python's
int(): it drops leading zeros before converting, and itreads 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_pointincrates/citry_html_transform/src/output_policy.rs).Every decoder in
packages/py/citrynow uses it:decode_attribute_entities(numeric branch), used bycitry 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/citrynow finds only the helperitself.
Decoders inside
HTMLParserPython's
HTMLParsercallshtml.unescapeon every attribute value itreads (and on text with
convert_charrefs=True), so the same crash camefrom every parser Citry and citry-ui run over rendered output, for
example an authored
<textarea title="�…0106;">in a page thatserializes without JavaScript (
serialize.py), or the reference in a<c-CDisclosure>title fill.citry.util.html.LongReferenceSafeParseris anHTMLParserwhosefeedrewrites each decimal reference too long forint()as text of thesame length that
html.unescapedecodes alike, then hands the text toHTMLParser.feed. The rewrite is the short reference (j, or�for a value past U+10FFFF) padded withreferences,which
html.unescapedecodes to nothing. Positions, line and columnnumbers and start-tag lengths stay valid, which matters because
_PreloadHeadLocatoruses offsets. A chunk that ends in&#and digitswithout
;keeps that tail back until the nextfeedorclose,because more digits may follow and
HTMLParserwould otherwise decode ittoo early. Input without such a reference reaches
HTMLParser.feedunchanged (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),_PreloadHeadLocatorand
_HostValidator(_vue/serialization.py); in citry-uicaccordion,ccommand_palette,cdisclosure(both parsers),cimage,clistbox,cmenuandctree. Not moved: the docs site andscripts/vue_render_parityparse the project's own pages, and the testhelpers 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 withconvert_charrefs=Falsehandle_charrefreceives its pieces. None of thetwelve parsers reads those, and it only happens for a reference of more
than 4,300 digits.
Other digit strings Python cannot convert
.debounce.<5,000 digits>msor@c-poll.<5,000 digits>sraised the digit-limitValueError. A numberwith 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).\d, which also matches digits from otherwriting systems (
.debounce.٥٠msread as 50 ms). It now takes ASCII0-9only, here and in the editor's matching pattern(
citry_lsp/engine.py).x-citry-vue-revisionrequest header was checked withisdigit(),which also accepts digits from other writing systems such as
Arabic-Indic
٣, and then converted withint(). A header that is nota 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 inpackages/py: the Eventsschema (
ext/events/schemas.py), the JSON protocol reader(
_protocol/events/issues.py, viajson.loads), the carriersendSequence(_protocol/events/carriers.py) and WSGI'sCONTENT_LENGTH(contrib/wsgi.py) already catch theValueErrorandreport or fall back. Five citry-ui input coercions (
ctextarea,ctags_input,cgrid,cdisclosureandcaccordionheading_level)convert an authored decimal string with
int()and would raise Python'sValueErrorinstead of the component's own error for a value of morethan 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_pointalready follows this rule, so this branch, rebased onit, no longer touches
_csp_validation.py. #289's test reference decoder(
tests/_output_policy_reference.py,_bounded_decimal_reference) keepsits 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_entitieson long references (leadingzeros, no
;, hex, values past U+10FFFF), and the helper againsthtml.unescapeon short references;citry check --staticon a template with the reference;an interactive component printing it in
Markup, on both render paths;an authored
hrefand aMarkupc-hrefholding�...0106;avascript:alert(1), which the strict CSP check refuses asa
javascript:URL (the authored one already passes onmainsinceperf: run the strict CSP and JavaScript-policy checks in Rust (560 ms to 99 ms on a large page) #289; the
Markupone crashed while rendering).LongReferenceSafeParseron 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 aCDisclosuretitle;a
.debounce/@c-polltiming with 5,000 digits, and bad revisionheaders (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 emptyrevision headers, which
mainalready refused.Release note
citry-ui's components now import
LongReferenceSafeParserfrom citry, socitry-ui does not work with citry 0.6.0. This PR keeps citry-ui's
citry>=0.6.0floor (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_referencesandLongReferenceSafeParser.🤖 Generated with Claude Code