Skip to content

🐛 fix(linkify): escape text of raw-text roots in string output - #2249

Merged
gaborbernat merged 4 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/linkify-rawtext
Nov 16, 2026
Merged

gaborbernat merged 4 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/linkify-rawtext

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

linkify(node), Linker.linkify(node) and linkify(node, options) return the linked node as an HTML string. When that node is a raw-text element (style, script, xmp, iframe, noembed, noframes or plaintext), the string carried the node's text unescaped, so it did not read back as the text the node held. After this change the text comes back escaped, as it already does for textarea, title, noscript and a plain div. Fixes #2245.

The string form returns the serializer's output for the node itself, from the return line of Linker.linkify.

The root's content model picked how its text was written

The HTML serialization algorithm writes the children of a raw-text element without escaping (serialising HTML fragments). turbohtml follows it in is_rawtext_element and in the raw-text writer. The string form asked for the root's inner_html, so the root's own element decided how its text was written. The returned string has no raw-text context, so a reader of the string sees that text as ordinary content.

The children go under a plain element before serialization

For a raw-text root, the copy's children now move under a fresh div, and the string is that element's inner_html (new lines). This is the route the sanitize string forms take for the same shape. Element children keep their markup, as they do under any element, and the caller's node is untouched because only the copy is moved.

Behavior changes

  • The string from linkify, Linker.linkify and the options form for a raw-text root escapes &, < and > in its text, where it previously wrote them as they were.
  • Links in a raw-text root's text are still created, as they were before, because that text is linked the way a plain element's text is.
  • A raw-text element nested inside the node writes its text as the serializer does for any inner_html, and this change leaves the serializer as it is.
  • linkify_node returns the node and is unchanged, so serializing that node directly still writes a raw-text root's text as written.

linkify and Linker.linkify return the linked node as an HTML string. When
the node was a raw-text element such as style or xmp, the serializer wrote
its text verbatim, so the string could carry markup the node held only as
text. The string is read as plain content, so the root's children now go
under a plain element before they are serialized. The tree form,
linkify_node, still returns the node, and other roots serialize as before.

Fixes tox-dev#2245
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
pre-commit-ci Bot and others added 3 commits November 16, 2026 19:37
A string is parsed under a div, so its root is never a raw-text element.
Returning for a string before the root check keeps the string form's work
unchanged for the common input.

Refs tox-dev#2245
@gaborbernat
gaborbernat merged commit 48d4dde into tox-dev:main Nov 16, 2026
42 of 45 checks passed
@codspeed

codspeed Bot commented Nov 16, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 15.56%

1 warning

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 581 untouched benchmarks
⏩ 89 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[query-closest] 273.8 µs 236.9 µs +15.56%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/linkify-rawtext (26562bf) with main (7e6d0f9)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

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.

linkify returns the text of a raw-text root unescaped

1 participant