Skip to content

🐛 fix(rewrite): keep output parsing as the input after edits - #2146

Merged
gaborbernat merged 8 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/49xnpt7d
Nov 15, 2026
Merged

gaborbernat merged 8 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/49xnpt7d

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

rewrite() copies every token a handler leaves alone exactly as the source has it. 📝 That copy reads the same in a browser only while the markup before it leaves the browser's tree builder in the same state as the source did.

Inside <svg>, <math>, <template> and <frameset>, a handler that drops, unwraps or replaces a tag can change that state, so copied raw text parses as markup, or the other way round. A page passed through rewrite() with a handler that removes <script> could come out with a script element the handler never saw. After this change the rewriter follows how a browser parses its own output after such an edit.

Architecture

Whether <title>, <style> and the other raw-text tags switch the tokenizer to text depends on the namespace of the element the tree builder inserts. The rewriter's tree builder reads the source, so it decides for the source's tree. An edit can give the output a different tree, for example by removing an HTML integration point or by adding color to a <font>, which makes it break out of foreign content.

After an edit that can change the output's tree, the rewriter runs a second tree builder over its output, the mirror. The mirror starts at the edit when the edit is inside <svg> or <math>, or else at the next <svg>, <math>, <template> or <frameset> start tag, because only there can a different tree change how a later token is tokenized. While it runs, each token is written so the output reads it as the source did.

Edits to comment text, to raw-text content, and to attributes other than an <input>'s type and a foreign <font> or <annotation-xml>'s attributes cannot change the tree, so they leave the rewriter as it was.

Source text ending in <

A text run can end in a < that the source reads as text only because of what follows it. If a handler removes or replaces what follows, the bytes that now follow can join that < into a tag, a comment or an end tag. The rewriter checks the last character of each copied run, and writes a < as &lt; when the next thing written would join it. A < followed by what followed it in the source never joins, so an unedited rewrite is byte-identical.

Behavior changes

  • After such an edit, followed by SVG or MathML content, a template or a frameset, the output can contain end tags the source did not have, escaped text where the source had raw text, and escaped text where the source had a CDATA section.
  • Text written into a raw-text element with prepend, append, set_content or set_text is escaped when the output reads that element as markup.
  • Markup inserted with html=True is written as given. Tokens after it are written so they read as in the source, which can close raw text that the markup opened.
  • A rewrite without such an edit, or with one and no SVG or MathML content, template or frameset after it, writes the same output as before.

Fixes #1701.

rewrite() copies every token it does not edit, which keeps the output
parsing as the input did only while the markup before each token is the
same. A handler that drops, unwraps or replaces a tag, writes text around
an element, or changes a <font> or <annotation-xml> inside SVG/MathML can
change what a browser has open at the next token: an <svg> stays open
after the tag that closed it is gone, a formatting element is rebuilt
elsewhere, or an integration point disappears. The raw text after such a
point, which the input read as text, then becomes markup in the output,
and the other way round.

After such an edit the rewriter now runs a second tree builder over its
own output, starting at the edit inside SVG/MathML or at the next <svg>,
<math>, <template> or <frameset>, since only there can the difference
change how a token is read. Before each token it ends raw text the
output opened but the input did not, escapes raw text the output reads
as markup, writes a CDATA section as text or a bogus "<![CDATA[" comment
as a comment where the two disagree, and after an end tag writes end
tags for the SVG/MathML elements the input closed but the output left
open. Unescaped text a handler wrote into a raw-text element is escaped
when the output reads that element as markup. A rewrite without such an
edit never starts the second tree builder and writes the same bytes as
before.

Fixes tox-dev#1701
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 2.12%

2 warnings

⚠️ 3 benchmarks with a significant change ran on different hardware

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

Learn more about Macro Runners

See affected benchmarks

⚠️ 3 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 1 improved benchmark
❌ 2 regressed benchmarks
✅ 579 untouched benchmarks
⏩ 89 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_feature[query-closest] 237.5 µs 274.7 µs -13.55%
❌ test_feature[select-nth] 437.8 µs 463 µs -5.42%
⚡ test_mutating_feature[path-xpath-one-cold] 143 µs 124.7 µs +14.69%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/49xnpt7d (c29c877) with main (f5b7d14)

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. ↩

Source text that ends in < (or, in a title or textarea, in an end tag
cut short) read as text because of what followed it in the input. When
a handler drops or changes what follows, the < is escaped if the next
bytes written would turn it into markup; an unedited rewrite stays
byte-identical.
@gaborbernat
gaborbernat merged commit 3817d41 into tox-dev:main Nov 15, 2026
29 of 30 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.

rewrite() leaves an open svg or math after dropping the tag that closed it

1 participant