Skip to content

Sanitize inside template content - #1371

Open
jeremy wants to merge 3 commits into
mainfrom
sanitize-template-content
Open

jeremy wants to merge 3 commits into
mainfrom
sanitize-template-content

Conversation

@jeremy

@jeremy jeremy commented Oct 9, 2026

Copy link
Copy Markdown
Member

HTMLSanitizer.sanitizeElements() walks the document with document.createTreeWalker, which does not descend into a <template>'s content — that content lives in a separate DocumentFragment rather than as child nodes of the element. Both of the sanitizer's protections ride that single traversal, so inside a <template> neither the forbidden-element list nor the attribute allowlist was applied.

DOMPurify runs after us and does recurse into template content, and its default allowlist already drops script, iframe and noscript. But form is in its default allowlist, so a <form> inside a <template> survived end to end, and disallowed attributes (name, action, method, autocomplete, …) persisted on elements we meant to strip them from.

Recurse sanitizeElements() into node.content when it is a DocumentFragment, mirroring how DOMPurify handles the same structure in _sanitizeShadowDOM. walkTree is untouched — it has many unrelated callers — and the recursive pass accumulates into the same nodesToRemove list, so removal semantics are unchanged. This composes with the <style> forbid from #1369: a <style> inside a template is now stripped too.

The tests are paired to isolate the traversal boundary as the cause rather than a blanket filter failure: a <script> inside the same <template> (independently removed by DOMPurify) and a <form> at top level (already removed) are controls alongside the new <form>-in-template assertion. A final test confirms permitted content — tables and https links — inside a template still survives.

Targeting 2.1.20, alongside #1369 and #1357.

jeremy added 2 commits October 9, 2026 14:01
sanitizeElements() traverses with document.createTreeWalker, which does not
descend into a <template>'s content: that content lives in a separate
DocumentFragment rather than as child nodes of the element. Both of the
sanitizer's protections ride that single traversal, so inside a template neither
the forbidden-element list nor the attribute allowlist applied at all.

DOMPurify runs after us and does recurse into template content, and its default
allowlist excludes script, iframe and noscript. So of the four elements we
forbid, it independently removed three and <form> survived end to end. Attributes
were not covered at all -- inside a template, name/action/method/autocomplete
persisted on elements we meant to strip them from.

Recurse into node.content when it is a DocumentFragment, which is how DOMPurify
handles the same structure in _sanitizeShadowDOM. walkTree is deliberately
untouched: it has 20+ unrelated callers, and nodesToRemove already accumulates
across the whole walk, so a recursive pass composes without changing removal
semantics.

The tests are paired on purpose. A green assertion that <form> is now removed
would, on its own, be satisfied by a sanitizer that had stopped running entirely,
so the script-inside-template and form-at-top-level controls are what isolate the
traversal boundary as the cause. The last test guards the other direction: tables
and https links inside a template must still survive.
action_text-trix/app/assets/javascripts/trix.js is a tracked build artifact
(only /dist is gitignored), so the source fix in the preceding commit does not
reach Action Text consumers until the bundle is regenerated. Rebuilt with
`yarn build-js`; the only change is the recursive template traversal.
@jeremy
jeremy requested review from jorgemanrubia and a balanced review from Copilot October 9, 2026 21:01
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T21:03:22.446420Z b0ee817 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approved

The focused implementation and tests correctly cover nested template sanitization without altering unrelated traversal behavior.

0 open findings

What changed in this PR

Recursively sanitizes <template> document fragments, closing element and attribute filtering gaps in inert template content.

Changes:

  • Adds recursive template-content sanitization.
  • Adds regression and control tests.
  • Updates the bundled Action Text build.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
src/​trix/​models/​html_sanitizer.js Recurses into template fragments.
src/​test/​unit/​html_sanitizer_test.js Tests template sanitization and permitted content.
action_text-trix/​app/​assets/​javascripts/​trix.js Mirrors the sanitizer fix in the bundle.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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