Skip to content

🐛 fix(dom): keep lone-surrogate attribute names whole - #2265

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/attr-name-surrogates
Nov 16, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/attr-name-surrogates

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

An element whose attribute name holds a lone surrogate parses fine, but reading it back raised UnicodeDecodeError in several places. attrs.keys() and attrs.items() failed on the Python attribute map, and so did sanitize(), linkify(), source locations, tree builders and mutation records. Once this merges, those names come back as the parser stored them. Fixes #2263.

Names were decoded as strict UTF-8

Attribute names are stored as UTF-8 bytes, and a lone surrogate has no strict UTF-8 form. Every place that turns a stored name into a Python str used the strict codec, so the first such name raised. The decode now uses surrogatepass, which reads back the bytes the parser wrote for that code point. Valid names decode exactly as before.

XPath name() copied bytes as code points

name() and local-name() on an attribute converted each UTF-8 byte of the name into one character, so the name's bytes were copied as code points. A name such as é came back as é. The name is now decoded from UTF-8 before it becomes XPath text, with the same surrogatepass rule.

Observer handle read outside its lock

On free-threaded builds, take_records() and deliver() read observer->handle before taking the tree lock, and disconnect() cleared it without any lock. A disconnect on another thread could clear the handle between the check and the drain. The field is now read and written under the observer's own critical section.

Behavior changes

Attribute names with a lone surrogate are returned as strings, and sanitized, linked and observed output keeps them. An XML fragment whose context has a declaration with such a name now raises the XML parser's error instead of UnicodeDecodeError, since XML cannot carry that name. Passing a lone surrogate as an argument, such as attrs.get(...) or an observer filter name, still raises UnicodeEncodeError, as it does for every string argument.

An attribute name can hold a lone surrogate, because the parser keeps
it and a DOM edit can carry it. The names were decoded as strict UTF-8
wherever they reached Python, so attrs, sanitize, linkify, source
locations, tree builders and mutation records raised UnicodeDecodeError
for such an element. Those paths now decode with surrogatepass, which
reads the same bytes back as the original code point.

XPath name() and local-name() copied each name byte into one code
point, so a non-ASCII attribute name came back as mojibake. They now
decode the UTF-8 name.

Observer handle reads in take_records(), deliver() and disconnect()
now happen under the observer's own critical section, so a concurrent
disconnect() on a free-threaded build cannot clear the handle between
the check and the drain.

Fixes tox-dev#2263
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
@gaborbernat
gaborbernat merged commit ffb7897 into tox-dev:main Nov 16, 2026
3 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attribute names with a lone surrogate raise UnicodeDecodeError

1 participant