Skip to content

fix(core): strip file marks inside templates and refuse markup a browser reads differently - #8845

Merged
miguel-heygen merged 2 commits into
mainfrom
fix/template-composition-file-marks
Nov 25, 2026
Merged

miguel-heygen merged 2 commits into
mainfrom
fix/template-composition-file-marks

Conversation

@miguel-heygen

Copy link
Copy Markdown
Collaborator

What

A data-composition-file that a composition file writes itself can no longer reach Studio's preview in two places that #8812 missed:

  • inside a <template>, when the server builds a page;
  • inside markup that the build parser and a scripting browser read differently. Examples are a </noscript> inside a comment in a <noscript>, and a comment that holds --!>.

Why

Studio writes an element's edits to the file its nearest data-composition-file names. #8812 strips the marks a file authored before the build stamps its own. That covered elements the build parser can see, but not these two cases:

  • Templates: linkedom keeps a parsed template's children in the template itself. querySelectorAll skips them, and template.content is a copy linkedom makes on first read. The strip ran on that copy, so the serialized page kept the marks, and the runtime later mounted them from the template.
  • Parser differences: the build parser (linkedom on htmlparser2) reads a <noscript>, <iframe>, <noembed> or <noframes> as markup. A browser with scripting on reads it as text up to the first matching end tag, even one inside a comment or an attribute value. A browser also ends a comment at --!>, which htmlparser2 reads past. In both cases a mark hidden in a comment is never an element at build time, so the strip can't see it, yet the browser turns it into a real element.

The runtime had the same gap of its own. When a fetched composition has no template or root, the runtime mounted its parsed body by setting innerHTML from a string. The page then re-read that string with scripting on.

How

  • dropAuthoredCompositionFiles (packages/core/src/compositionSource.ts) walks the tree in document order, so it reaches a linkedom template's real children. It reads content only on a template with no children, which is how a browser's template looks. On linkedom the strip therefore no longer creates a content copy that later edits to the template would miss.

  • parseHTML and DOMParser in packages/parsers/src/linkedom.ts already refuse HTML that a browser would read as a different tree (raw-text elements inside svg and math). They now also refuse:

    • an end tag for a noscript, iframe, noembed or noframes inside that element's own comments or attribute values;
    • a comment that holds --!>.

    Every server path parses through them: the bundler, inlined sub-compositions, the render compile, studio-server's single-composition page and the saved page served when the build fails. All of them now refuse such a file the way they already refuse the svg cases.

  • The runtime's fallback mount imports the parsed nodes the strip already cleaned, into a detached fragment that is stripped of extracted assets before it is attached. That is what its template and root branches already do. Nothing in a fetched body is read a second time.

I checked which constructs reveal a mark, using linkedom to parse, strip and write, and parse5 with scripting on to read the result back:

Construct Result
Comments or attribute values in noscript (including in <head>, inside templates and in any case), iframe, noembed, noframes Revealed the mark
A comment holding --!> Revealed the mark
<!-->, <!--->, CDATA, processing instructions, xmp, style, script, title, textarea, select, plaintext, and /, tab or form feed between attributes Matched the browser
svg and math raw text Already refused

The refusals cover every case that revealed a mark. Plain <noscript> fallbacks, <iframe>s and comments still parse and bundle as before.

No visible change

Previews and renders of ordinary compositions are unchanged. A file that hides an end tag in one of those elements, or a --!> in a comment, now gets the same "can't be read the way a browser reads them" error as the svg cases.

Studio already refused to paste a copied element that hides </noscript> in an attribute. The parser now refuses it first, so the toast shows the parser's message, as it already does for the svg case. useClipboard.sealed.test.ts expects that message for this one case. Nothing is written either way.

Test plan

  • Unit tests added/updated
  • Manual testing performed
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

fails without the fix:

  • packages/core/src/compositionSource.test.ts: drops a mark inside a template at any depth and leaves a template's content to be made from its children when it is first read.
  • packages/core/src/compiler/htmlBundler.test.ts: drops a mark authored inside a template, in the entry page or an inlined sub-composition.
  • packages/core/src/compiler/htmlBundler.foreignText.test.ts: the four refuses ... in the root composition and refuses ... in a sub-composition cases.
  • packages/core/src/runtime/compositionLoader.noscript.test.ts: mounts a fetched body without a mark hidden in a noscript comment. This test runs jsdom with scripting on, as the preview does.
  • packages/parsers/src/linkedomForeignText.test.ts: the nine new refuses %s, which a browser reads differently cases.
  • packages/studio-server/src/helpers/subComposition.test.ts: drops a mark written inside a template, at any depth and refuses a mark hidden where a scripting browser reads past a comment.
  • packages/studio-server/src/routes/preview.unbuiltFileMarks.test.ts: the four existing cases, whose fixture now also has a marked element inside a template.

Every one of these fails on main and passes with the fix, three runs in a row.

Two further assertions in compositionLoader.test.ts cover the browser branch: a template inside a fetched file is stripped through its content. They pass on main too. Removing the content branch fails them.

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 273 (base branch 271), smooth 68 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Newly passing (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Quarantined, measured but not gated (0)

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes at 6b4f5a5: one blocker, a required check that this PR turns red.

Blocker: packages/cli/src/media-use/lib/svg-sanitize.mjs is stale. Test (support) fails, so the required Test job fails. The failing case is media-use svg-sanitize.mjs matches a fresh regeneration (scripts/check-media-use-svg-sanitize-generated.test.mjs), with the message svg-sanitize.mjs is stale — run: node scripts/generate-media-use-svg-sanitize.mjs. That file bundles sanitizeSvg.ts together with @hyperframes/parsers/linkedom, which this PR edits. The committed bundle does not contain the new --!> check. #8760 regenerated the bundle in its own commit. This is the PR's own red, not infra: the other Test run on main's head 5dd0ee9e failed in a different way, and its log has no is stale line. Fix: run node scripts/generate-media-use-svg-sanitize.mjs and commit the result. It matters beyond CI, because media-use's zero-install lane would otherwise keep the old, narrower refusal.

The rest checks out, from reading the diff (I ran nothing locally):

  • dropAuthoredCompositionFiles now walks in document order. It reaches linkedom's real template children, nested templates and mixed-case attribute names. It reads content only for a template with no children, which is how a browser's template looks.
  • The noscript, iframe, noembed and noframes check uses /</name[\t\n\f\r />]/i on innerHTML. That covers upper case, </NOSCRIPT > and </noscript/, and it ignores </noscripts>. The --!> check looks only at comment nodes, so --!> inside a <script> string is not refused. Plain noscript fallbacks and comments still parse, and a test pins that.
  • The runtime fallback no longer re-parses a string. It imports the already-stripped nodes into a fragment and strips extracted assets before attaching, as the template and root branches do.
  • Every case in the "fails without the fix" list is present, and each is a real regression test. The one that needs scripting on, compositionLoader.noscript.test.ts, runs jsdom with runScripts set, as the preview does.

Notes for Miguel, not blocking. These are posture questions, not mine to answer:

  1. hyperframes lint on refused files. Lint rules call parseHTML unguarded (rules/media.ts, rules/composition.ts, project.ts). A file that the parser now refuses therefore makes the whole lint command fail with runtime_error, instead of reporting a finding on that file. That is what #8760 did for the svg cases. This PR widens it to a --!> comment, which is valid HTML, and to end tags inside noscript and iframe comments. I read this from the code and did not run it. Should lint report a finding for these files? That could be a follow-up.
  2. False positive. A <noscript> inside <svg> is read as markup by a browser, but the check still refuses an end tag inside it. That input is very rare and fails safe.

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Blocker fixed at 0799adb7: I regenerated packages/cli/src/media-use/lib/svg-sanitize.mjs with node scripts/generate-media-use-svg-sanitize.mjs and committed it on its own. The bundle now has the --!> check and the wider end-tag refusal.

I ran these on the regenerated tree:

  • check-media-use-svg-sanitize-generated.test.mjs, check-svg-sanitize-parity.test.mjs and comment-ratchet.test.mjs under node --import tsx --test: 72 passed, 0 failed.
  • All of bun run test:scripts: 1,027 passed, 1 failed. The failure is catalog-detail.test.ts, "a link's text reaches the copy buttons only once…". It fails only on my machine with a postMessage origin null error. CI passed it on the previous head, and this PR doesn't touch that page.

Both notes on lint posture and the <svg><noscript> false positive are left to the maintainers.

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving at 0799adb. The one blocker is fixed.

  • The delta from 6b4f5a5 is one file, packages/cli/src/media-use/lib/svg-sanitize.mjs (+6/-6), regenerated. parsers/src/linkedom.ts is byte-identical between 6b4f5a5 and 0799adb, so the old bundle was simply stale against its own source.
  • The new bundle now carries the --!> constant and the comment-walk check, and its foreign-text logic (title/style/script/textarea/xmp inside svg/math, the noscript/iframe/noembed/noframes end-tag regex, the document-order walk, and the DOMParser text/html refusal) matches linkedom.ts line for line. The sanitizer tail is identical to the previous bundle apart from identifier renames, and no new code paths were added.
  • Required checks are green on this head, including Test, Test (support), regression, Studio: edit accuracy gate, Tests on windows-latest and Build. Heavy films: Safari vs Chrome was still running and is not a required check.

Two earlier non-blocking notes stand: lint rules call parseHTML unguarded, so a refused file turns into a runtime_error for the whole hyperframes lint run (posture call, follow-up is fine, same as #8760), and a rare noscript-inside-svg false positive that fails safe.

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 25, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 25, 2026
@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 81869fa Nov 25, 2026
108 checks passed
@miguel-heygen
miguel-heygen deleted the fix/template-composition-file-marks branch November 25, 2026 15:10
@miguel-heygen miguel-heygen mentioned this pull request Nov 25, 2026
3 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants