Repository navigation
🐛 fix(dom): keep lone-surrogate attribute names whole - #2265
Merged
Merged
Conversation
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
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.
An element whose attribute name holds a lone surrogate parses fine, but reading it back raised
UnicodeDecodeErrorin several places.attrs.keys()andattrs.items()failed on the Python attribute map, and so didsanitize(),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
strused the strict codec, so the first such name raised. The decode now usessurrogatepass, 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()andlocal-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 samesurrogatepassrule.Observer handle read outside its lock
On free-threaded builds,
take_records()anddeliver()readobserver->handlebefore taking the tree lock, anddisconnect()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 asattrs.get(...)or an observer filter name, still raisesUnicodeEncodeError, as it does for every string argument.