Skip to content

Read numeric references in attribute values as the browser does - #610

Merged
JuroOravec merged 1 commit into
mainfrom
hardening/attribute-numeric-references
Nov 14, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
hardening/attribute-numeric-references

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

The HTML parsers that Citry uses to read its own output decoded attribute values with Python's html.unescape. It drops some code points that a browser keeps, such as U+0001 and U+FDD0. With this change, those parsers (the repeated-id warning, the Vue host check, and the citry_ui output parsers) read the attribute values the browser reads.

Changes

  • LongReferenceSafeParser (citry/util/html.py) now gives its start tag handlers the attribute values a browser reads, decoded with decode_attribute_entities from each tag's original text. It keeps the original text only when one of its start tag handlers receives these values, and drops the part before the tag it is reading, so a page fed in small chunks costs time and memory in step with its length.
  • When it cannot line a tag up with its original text, the handler gets the values HTMLParser gives. The same holds for a handler added after the class is created, set on an instance, or defined by a mixin that comes before the parser class; the class docstring says so.
  • Parsers that never read attribute values skip the second decoding: _PreparedMarkupTracker and _PreloadHeadLocator in citry, _DirectMenuParser and _DirectItemParser in citry_ui.
  • The repeated-id warning writes a control character in an id as an escape, because an id read this way can now hold one, such as ESC.
  • decode_attribute_entities already decodes numeric references this way on main, so its behavior does not change.
  • CHANGELOG.md has one entry under ## Unreleased, for the repeated-id warning.

What the browser reads

The tests hold the table below. Chromium 156 and Firefox 157 each loaded a page with title="a&#xN;b" for every row and returned the same value.

Reference Browser and new parser output Parser output before
 to , ,  to ,  the character dropped
, , , , ,  the character dropped
€, Ÿ €, Ÿ same
,  , 	 the character same
�, �, � U+FFFD same

One difference is outside this change: 
 decodes to a carriage return, which Citry writes as is and the browser's HTML parser turns into a line feed. Plain string values behave the same way, so that belongs in a separate change.

Tests

  • tests/test_attribute_numeric_references.py: the table above for the decoder and the parser, class and style pieces, static and interactive pages on both render paths, and the parser cases (quoted and unquoted values, long references including one in an attribute name, input split into many small feeds and a bound on the text the parser keeps, subclasses, handlers called by hand and through super(), the opt-outs, the two cases above where a handler keeps HTMLParser's values, and the Vue host check).
  • tests/test_page_ids.py: two ids that differ by a reference are different ids, and the warning spells out a control character.
  • citry_ui/tests/test_output_parsers.py: the two opt-outs, and a popover value read as the browser reads it.
  • Checked after rebasing onto main: ruff, and mypy (including --platform linux), are clean, and the focused citry and citry_ui suites pass. Three tests in tests/test_check.py (reserved names in v-for and v-slot) need a citry_core build newer than the one I used, and fail the same way on main with it. About 6,000 generated start tags, split in many ways, read the same as main's parser (names) and as a known answer (values) on CPython 3.11.15, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1. Deleting any one of five guards in the parser (the alignment check, the name check, the chunk dropping, the ancestry check and the wrapper marker) makes a new test fail.

🤖 Generated with Claude Code

… does

The HTML parser Citry uses to read its own output decodes attribute
values with `html.unescape`, which drops some code points (U+0001 to
U+0008, U+000B, U+000E to U+001F, U+007F, noncharacters such as U+FDD0)
that the browser keeps. The page id check and the other output parsers
then read values that differ from the page the browser builds.

`LongReferenceSafeParser` now hands its subclasses the values a browser
reads, decoded with `decode_attribute_entities` from each tag's original
text. The rewrite of long references keeps the text the same length, so
the parser splits the rewritten tag as `HTMLParser` did and reads each
value from the same span of the original. When the original text cannot
be lined up with the tag, the handler gets the values `HTMLParser` gives.
The parser keeps the original text only when one of its start tag
handlers receives the browser's values, drops the part before the tag
being read, and joins chunks only for a tag that spans more than one.
Parsers that never read attribute values opt out of decoding them a
second time.

The warning for a repeated `id` writes control characters in an id as
escapes, because an id can now hold one, such as ESC, that the old
reading dropped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the hardening/attribute-numeric-references branch from b71b6e0 to 130bbb8 Compare November 14, 2026 15:29
@JuroOravec
JuroOravec marked this pull request as ready for review November 14, 2026 15:29
@JuroOravec
JuroOravec merged commit 3adf79d into main Nov 14, 2026
27 of 33 checks passed
@JuroOravec
JuroOravec deleted the hardening/attribute-numeric-references branch November 14, 2026 16:24
JuroOravec added a commit that referenced this pull request Nov 14, 2026
… checks (#674)

A browser keeps the first of two attributes with the same name on one
tag. The citry_ui checks of rendered fills read `dict(attrs)`, which
keeps the last, so `<span tabindex="0" tabindex="-1">` passed the
Listbox option check while the browser makes the span focusable.

- `LongReferenceSafeParser` hands the start tag handlers of subclasses
  that read attributes each repeated name once, with its first value.
  The repeated `id` warning now counts only the first `id` of an
  element, and the Vue mount host check reads a repeated `id` the same
  way, so `<div id="app" ID="x">` is the host with one attribute.
- The citry_ui checks read their tags through their own
  `first_attribute_occurrences`, because Citry UI also runs on Citry
  releases whose parser keeps every occurrence.
- `LongReferenceSafeParser` holds back a lone `&` at the end of a chunk
  until the next chunk arrives, so a long decimal reference split right
  after the `&` no longer raises `ValueError`. That error made Citry
  skip its preload links for such a page.
- The citry_ui check messages write control characters in a tag name as
  escapes such as `\x1b`.

The citry_ui changelog also gets the entry for #610's stricter reading
of numeric references in attribute values.

Co-authored-by: Claude Opus 5.5 <[email protected]>
JuroOravec added a commit that referenced this pull request Nov 14, 2026
…ight merged changes (#682)

Add one record under docs/security/reviews/ for each of the eight published
0.8.0 advisories (GHSA-4hj5, -8xvf, -7qpw, -g658, -x6rm, -h524, -xmx5,
-xg8c) and for each of the pull requests that Mechanism 7, step 7 owes one:
#610, #640, #651, #652, #653, #658, #674 and #675. Each record says only what
the public pull request, the published advisory, the merged code and its tests
show, and lists what was not checked.

The reviews README describes how a record for a published advisory differs
from a pull request record. The Mechanism 7 list in CLAUDE.md gains the two
places that GHSA-7qpw added: parser rules browsers differ on, and the page's
declared encoding.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant