Low · 2.3 Node.js & npm GHSA-cg2g-xh64-v4q7
DOMPurify: SAFE_FOR_TEMPLATES bypass via split template expressions in shadow root content
DOMPurify's SAFE_FOR_TEMPLATES protection can be bypassed when sanitizing DOM nodes that contain an attached shadow root, allowing split template expressions to recombine and survive sanitization. Fixed in 3.4.17.
By PatchBriefing ·
Produced automatically from official sources with the help of AI. How we work →
AI summary
A vulnerability has been identified in DOMPurify, a widely used JavaScript library for sanitizing HTML to prevent cross-site scripting (XSS). The issue affects applications that use DOMPurify's SAFE_FOR_TEMPLATES option together with DOM-returning sanitization modes, and that process DOM nodes carrying an attached shadow root. Under these specific, non-default conditions, an attacker-controlled template expression split across text nodes can recombine after sanitization and survive, potentially leading to script execution or template injection in a downstream template engine.
What happened
A vulnerability identified as GHSA-cg2g-xh64-v4q7 was published for the dompurify npm package. It describes a bypass of the SAFE_FOR_TEMPLATES sanitization feature that occurs specifically when content inside an attached shadow root is sanitized using a DOM-returning mode such as IN_PLACE or a shadow-aware RETURN_DOM. Template expression fragments (for example parts of a ${...} expression) that are separated by a disallowed element can become adjacent after that element is removed during sanitization, reconstituting a complete expression that was never re-checked as a merged unit.
Technical cause
DOMPurify protects against template-expression injection in two steps: a per-text-node strip applied during the main sanitization walk, and a post-walk safety net that normalizes adjacent text nodes and re-strips any expression that only becomes complete after merging. According to the advisory, this post-walk safety net was previously extended to recurse into the content of <template> elements (addressed under CVE-2026-65900 / GHSA-gvmj-g25r-r7wr), but it was never extended to recurse into attached shadow roots. Shadow root content is sanitized earlier in the process using only the per-text-node strip, which cannot detect an expression that has been deliberately split into fragments too small to match the expression-detection patterns. As a result, fragments separated by a disallowed element can end up adjacent after that element is stripped, and the merged expression is never re-examined.
Why it matters
If an application enables SAFE_FOR_TEMPLATES together with a DOM-returning sanitize mode, accepts a DOM node that already has an attached shadow root (including via declarative shadow DOM parsed from HTML strings), and later evaluates the sanitized shadow root content with a template engine, an attacker-chosen expression can survive sanitization intact. Depending on how the application evaluates that content, this can lead to cross-site scripting in a client-side context or template injection in a server-side context. Exploitation depends on this specific, non-default configuration; it does not affect default DOMPurify usage.
Who is affected
Applications using the dompurify npm package with SAFE_FOR_TEMPLATES enabled in combination with a DOM-returning sanitize mode (IN_PLACE or a shadow-aware RETURN_DOM), where the sanitized DOM node carries an attached shadow root and the resulting content is later passed to a template evaluator.
Affected versions
The advisory lists dompurify versions from 3.4.4 up to and including 3.4.16 as affected. Versions 3.4.4 through 3.4.7 contain the post-walk safety net without any content-container recursion; versions 3.4.8 onward add recursion into <template>.content but still do not recurse into shadow roots. The fix is included in version 3.4.17.
Fixes and mitigation
The issue is fixed in dompurify version 3.4.17. Applications should update to this version or later.
Recommended action
Update dompurify to version 3.4.17 or later. If an immediate update is not possible, review whether your application uses SAFE_FOR_TEMPLATES together with a DOM-returning sanitize mode (IN_PLACE or RETURN_DOM) on DOM nodes that may carry an attached shadow root, and whether sanitized output is subsequently evaluated by a template engine; this specific combination is required for exploitation.
PatchBriefing score
2.3 / 10 · Low
Official CVSS: 2.3
CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:P/VC:N/VI:N/VA:N/SC:L/SI:L/SA:N
Why this score
The PatchBriefing score equals the official CVSS score; when this article covers several vulnerabilities, the highest one. Signals such as known exploitation and EPSS are listed separately and do not change the score.
Affected versions
- dompurify >= 3.4.4, <= 3.4.16
- vulnerable
- dompurify ≥ 3.4.4 < 3.4.17
- vulnerable
- ≥ 3.4.17
- patched
Reported fixes
The issue is fixed in dompurify version 3.4.17. Applications should update to this version or later.
How this was built
2 source records were collected, matched and used to prepare the report above.
-
GitHub Advisory Database database
-
OSV.dev database
Revision history
- Generated
- Published
- Generated