Skip to content

🐛 fix(serialize): close scripts left in a double escape - #2173

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/ulcu3x8n
Nov 15, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/ulcu3x8n

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

A <script> whose text ends in the double escaped state does not survive serialization. A parse produces this only when the input ends inside the script. DOM edits to script text can produce it anywhere.

🔁 Serializers wrote one </script>, which only ends the double escape on a re-parse, so the script stayed open and swallowed the markup after it up to the next </script>. Code that serializes turbohtml output and hands it on, such as a sanitizer, could receive a different tree than the one it checked. Every serializer now writes a second end tag for such a script, so the output re-parses to the tree that was serialized.

Architecture

Serializers replay the script states over the text they just wrote, and add the end tag when the replay ends in the double escaped state. The compact, minified and lossless writers all call this check. To skip repeat scans, the tree records a flag once a script's text is known to end outside the double escape. The parser sets the flag when an end tag closes the script, and the serializer sets it after one clean scan. Edits to the text clear it.

Behavior changes

  • A script whose text ends in the double escaped state gets two </script> end tags in serialize(), serialize_iter(), encode(), the Indent and Minify layouts, minify() and to_source().
  • Every other script serializes as before.

Fixes #1773

A script whose text ends in the script data double escaped state was
written with one end tag. In that state the first end tag only leaves
the double escape, so a re-parse kept the element open and ran on into
the next end tag in the output, which could sit inside a comment or a
style element. serialize(), to_source(), the Minify layout and minify()
now write a second end tag in that case. A script nested in another
raw-text element through the DOM is left alone: its end tag is text
there, and the outer element's end tag decides.

The check replays the tokenizer's script states over the written text,
and ordinary input skips it. The tree builder marks the text of a script
its end tag closed, since the tokenizer only reads that end tag outside
the double escape, and the serializer marks a script text it scanned
once. Every write to character data clears the mark. The lossless walk
keeps its state in one field that the end-tag writer already tests.
@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 not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:sec-fix/ulcu3x8n (0542774) with main (7a2a98c)

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

@gaborbernat
gaborbernat merged commit 39d96be into tox-dev:main Nov 15, 2026
31 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.

Serialized output reparses to a different tree when a script ends double escaped

1 participant