Skip to content

🐛 fix(minify): keep noscript trees when scripts run - #2154

Merged
gaborbernat merged 12 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/wta34pi3
Nov 15, 2026
Merged

gaborbernat merged 12 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/wta34pi3

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

minify parsed the document with scripting off and serialized that tree. 🧱 A browser with scripting on reads <noscript> content as raw text, so the minified markup could build a different tree there, with elements the source kept as text. After this change minify output re-parses to the source's tree under both scripting modes. A generic raw text element with scripting on reads everything up to the first </noscript as text, and the minifier's whitespace folding, tag omission and comment stripping change that text.

Architecture

The parse records each <noscript> with the source location of its tags and content, and with whether the parser state after it matches the state before it. A noscript closed by the first </noscript after its start tag, with that state restored, reads the same under both modes, so the minifier copies it from the source as written.

For the first noscript that does not, the minifier minifies the markup before it only when that markup rebuilds the parser state at the noscript, and copies the source from there to the end. Otherwise it returns the source unchanged. Neither path parses the output again.

Behavior changes

  • minify copies noscript content from the source, so whitespace, comments and attribute quoting inside it stay as written, except that CR and CRLF become LF.
  • A page with a noscript whose content parses differently with scripting on keeps everything from that noscript onward unminified. If the markup before it sits in a table, a template, foreign content, an open form or open formatting elements, the whole document comes back unchanged.
  • The minifying layout keeps <body> before a first child that is base, basefont, bgsound, noframes, noscript or title, because the "after head" insertion mode would otherwise move that child into <head>.
  • With minify_js or minify_css set, a <script> or <style> whose minified text would hold </noscript in any letter case is written verbatim.
  • serialize(Html(layout=Minify())) on a tree has no source to copy, so it still minifies noscript content.

Fixes #1776.

Minifying a script or style inside a <noscript> could write a
</noscript end tag into its output. The JavaScript minifier folds
string concatenation, and the CSS minifier drops line continuations,
so the joined text need not appear in the source. A browser that runs
scripts reads the noscript as raw text up to its first end tag, so the
rest of the element ran as markup.

Emit the content verbatim when the minified text would hold
</noscript in any letter case, the same rule that already covers
</script and <!--. The stylesheet check runs on the bytes it writes.
@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/wta34pi3 (205a623) with main (c0e4c3b)2

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

  2. No successful run was found on main (9fdbc4e) during the generation of this report, so c0e4c3b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

A browser that runs scripts reads <noscript> content as raw text, but
minify() serialized the scripting-off tree, so that content could turn
into live elements there. The document parse now records each noscript's
source span and whether the two readings agree around it. Stable
noscripts get their content copied from the source. Where the readings
part, minify() copies the source up to the first element both place
alike, checks the result against both trees, and falls back to copying
to the end, then to the source unchanged.

The body start tag is also kept before a leading noscript, title, base,
basefont, bgsound or noframes, which the after-head mode would otherwise
move into the head.
Parses and serializations without a noscript record now run their own copy
of the drain loop and the minify walk, so they pay no per-token or per-node
check. The parted path reuses its two located parses for the expected
trees.
The minify walk now checks for a source once per node and writes end tags
under a single quiet flag, and the parted path computes ancestor chains only
for the elements it compares.
@gaborbernat gaborbernat changed the title 🐛 fix(serialize): keep minified content from closing noscript 🐛 fix(minify): keep noscript trees when scripts run Nov 15, 2026
Replace the reparse-and-verify regions with a bounded rule. From the
first noscript whose content parses differently with scripting on, the
rest of the source is copied verbatim when the builder state at its start
tag is one the minified markup before it rebuilds: no active formatting
element, no open form, only HTML ancestors and no table, and no later
html/body attributes. Otherwise the document comes back unchanged. The
minifier no longer reparses its output, so the slow path costs no more
than a plain minify.
@gaborbernat
gaborbernat merged commit 4f11284 into tox-dev:main Nov 15, 2026
30 checks passed
gaborbernat added a commit that referenced this pull request Nov 15, 2026
`minify` is documented as idempotent, but minifying its own output could
change it. Three causes, all in the minifier's serializer walk in
`src/turbohtml/_c/serialize/`.

- A stripped comment reset the walk's trailing-formatting state. An
ancestor's end tag was omitted even though, once the comment is gone, a
formatting element is the last content of that ancestor. The reparse
then needs the end tag, and the second pass writes it back. The walk now
steps over stripped comments when it looks for the next sibling.
- The body start tag was omitted before `base`, `basefont`, `bgsound`,
`noframes`, `noscript` and `title`. The parser files those elements
under `head` when no body tag precedes them, so the tree changed. The
keep-list for the body start tag covers them now; `meta`, `link`,
`script`, `style` and `template` were already in it.
- A `pre` or `listing` whose content starts with a stripped comment and
then a newline lost the newline the reparse drops. The leading-newline
check now skips stripped comments before it reads the first text.

### Behavior changes

- Output keeps an ancestor's end tag when a stripped comment follows the
formatting content that ends it. The second pass previously restored the
tag.
- Output keeps the body start tag before the head elements listed above.
- Output keeps one extra newline at the start of a `pre` or `listing`
whose first text starts with a newline after a stripped comment.

Output differs from before only in those three cases.

### Tests

- `test_body_start_kept_before_head_element` covers the head elements
and checks that the reparse builds the source tree.
- `test_minify_keeps_end_tag_guarded_by_trailing_formatting` lists the
comment cases with their expected output, and checks that minifying the
output again returns it unchanged.
- `test_minify_keeps_leading_newline_after_stripped_comment` covers
`pre` and `listing`.
- `test_minify_is_idempotent_over_seeded_documents` minifies 2,000
documents from a fixed seed and asserts that each output is a fixed
point. Documents whose plain serialization does not round-trip are
skipped, because the serializer itself is not a fixed point for them.

On a release build of current `upstream/main`, 13 of the new cases fail.
The body-start cases already pass there, since the keep-list landed in
#2154. On this branch all pass.

### Performance

Base: `upstream/main` at `4f112841e`. The minify sources this PR touches
are unchanged in later upstream commits, so the table keeps that base.
Release LTO builds, run under callgrind with `PYTHONHASHSEED=0` and
`setarch -R`. Each row is instructions per call from the 220- and
20-repetition difference. Two builds of `upstream/main` at equal path
length give the noise floor, which is exactly 0 on every row.

| Row | main, run 1 | main, run 2 | this branch | change |
| --------------------------- | ----------: | ----------: | ----------:
| ---------------: |
| minify: daring fireball | 668,184 | 668,184 | 669,048 | +864 (+0.13%)
|
| minify: ars technica | 3,129,560 | 3,129,560 | 3,133,244 | +3,684
(+0.12%) |
| minify: mozilla blog | 6,703,490 | 6,703,490 | 6,710,350 | +6,860
(+0.10%) |
| minify: whatwg spec | 20,177,012 | 20,177,012 | 20,239,859 | +62,847
(+0.31%) |
| serialize `Minify()` (tree) | 1,574,017 | 1,574,017 | 1,580,983 |
+6,966 (+0.44%) |
| control: parse | 4,898,997 | 4,898,997 | 4,898,997 | 0 |

One optimization round was used. The first version of the newline check
ran for every element before its tag test, which put the rows at +0.4%
to +0.8% (whatwg spec +0.78%, serialize tree +1.77%). Moving the comment
skip behind the `pre`/`textarea`/`listing` test brought them to the
table above. The remaining cost is that per-element tag test in
`serialize_minify`; a second round did not seem worth it, and no row is
above +2%.

Fixes #2182
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.

Minified JavaScript in a noscript element can gain a </noscript> end tag

1 participant