{"schemaVersion":4,"id":"GHSA-CG2G-XH64-V4Q7","aliases":[],"status":"published","duplicateOf":null,"title":null,"description":"DOMPurify SAFE_FOR_TEMPLATES bypass: split template expressions survive in attached shadow root content on DOM output modes\n\n## Summary\n\nWhen DOMPurify is configured with `SAFE_FOR_TEMPLATES: true` together with a DOM-returning mode (`RETURN_DOM: true`, `RETURN_DOM_FRAGMENT: true`, or `IN_PLACE: true`), template expressions such as `{{...}}`, `${...}`, and `<%...%>` survive sanitization inside the text content of an attached shadow root. The post-walk safety net `_scrubTemplateExpressions` (`src/purify.ts:1717`) was extended to recurse into `<template>.content`, but it never recurses into attached shadow roots. An attacker who can supply a DOM node carrying a shadow root (the `IN_PLACE` and shadow-aware `RETURN_DOM` surfaces) can split an expression across text nodes separated by disallowed elements; after those elements are removed the fragments become adjacent and serialize into a complete expression that a downstream template engine will evaluate.\n\n## Root Cause\n\n`SAFE_FOR_TEMPLATES` neutralizes template expressions through two mechanisms: a per-text-node strip during the main walk (`_stripTemplateExpressions`, applied in `_sanitizeElements` at `src/purify.ts:2246`), and a final post-walk pass (`_scrubTemplateExpressions`, `src/purify.ts:1717`) that normalizes adjacent text nodes and re-strips expressions that only form after merging. The per-node strip cannot catch an expression that is split into individual fragments, because each fragment fails the delimiter regexes. The post-walk pass is the only mechanism that would catch the merged form, but it is structurally unable to reach shadow root content.\n\n```text\n_scrubTemplateExpressions (src/purify.ts:1717-1751):\n  - node.normalize() at line 1718 does not merge text nodes inside shadow roots (DOM spec).\n  - NodeIterator at lines 1723-1731 does not descend into shadow trees (DOM spec).\n  - Explicit recursion at lines 1743-1750 enters <template>.content only, via\n    querySelectorAll('template'); there is no equivalent recursion into attached\n    shadow roots.\nShadow roots are sanitized earlier by _sanitizeShadowDOM (src/purify.ts:2710), which\napplies the per-node strip but never performs the post-walk normalize-and-rescrub.\n```\n\nShadow root text nodes are therefore sanitized only per-node and never re-examined as a merged unit.\n\n## Impact\n\nAn attacker who controls DOM-node input containing an attached shadow root can inject template expressions that survive sanitization and are later evaluated by the application's template engine. In client-side contexts this yields cross-site scripting; in a server-side template context it can escalate to template injection. Exploitation requires non-default application configuration: the application must enable `SAFE_FOR_TEMPLATES` and a DOM-returning mode, pass a DOM node that already has an attached shadow root, and feed the sanitized shadow root content into a template evaluator. The shadow root does not have to be built programmatically with `attachShadow`: in engines that support declarative shadow DOM, attacker-supplied markup containing `<template shadowrootmode=\"open\">` parsed through `setHTMLUnsafe` or `Document.parseHTMLUnsafe` produces the same attached shadow root, so a plain HTML string reaches the gap once the application is configured this way. Demonstrated capability is injection of a complete, attacker-chosen expression into post-sanitization shadow root content; the resulting script execution and any confidentiality or integrity consequence depend on the downstream engine's evaluation context.\n\n## Proof of Concept\n\n```text\nPreconditions:\n  - DOMPurify built from the affected source (dist/purify.js or an ESM/CJS bundle).\n  - A browser (or DOM implementation) with shadow DOM support.\n  - Application constructs a DOM node with an attached shadow root before sanitizing,\n    and later evaluates the shadow root's text content as a template.\n```\n\nBuild a host element whose shadow root holds a template expression split across text nodes separated by disallowed elements, then sanitize in place with `SAFE_FOR_TEMPLATES`:\n\n```javascript\nconst host = document.createElement('div');\nconst root = host.attachShadow({ mode: 'open' });\n\n// Split \"${alert(document.domain)}\" so no single fragment matches a regex.\n// <object> is not in the default ALLOWED_TAGS and is removed during sanitization,\n// leaving the surrounding text-node siblings adjacent.\nroot.appendChild(document.createTextNode('$'));\nroot.appendChild(document.createElement('object'));\nroot.appendChild(document.createTextNode('{alert(document.domain)}'));\n\nDOMPurify.sanitize(host, { IN_PLACE: true, SAFE_FOR_TEMPLATES: true });\n\n// After the disallowed element is removed, the fragments are adjacent and survive.\nhost.shadowRoot.normalize();\nconsole.log(host.shadowRoot.textContent);\n```\n\n```text\n${alert(document.domain)}\n```\n\nA downstream template evaluator reading `shadowRoot.textContent` (for example `new Function('return `' + content + '`')()` or any `${...}`/`{{...}}` engine) then evaluates the fully formed expression.\n\n## Attack Chain\n\n1. **Exposure:** The application calls `DOMPurify.sanitize(node, cfg)` (`src/purify.ts:2854`) with `SAFE_FOR_TEMPLATES: true` and `IN_PLACE: true` (or a shadow-aware `RETURN_DOM`), passing a DOM node that already carries an attached shadow root.\n2. **Control:** The attacker controls the shadow root's text nodes and the disallowed separator elements between them; each fragment (`$`, `{alert(document.domain)}`) is a valid, individually benign text node.\n3. **Path:** `_sanitizeAttachedShadowRoots` (`src/purify.ts:2977`) feeds the shadow root into `_sanitizeShadowDOM` (`src/purify.ts:2710`), which strips expressions per text node (`src/purify.ts:2246`) and removes the disallowed separator elements, leaving the fragments adjacent.\n4. **Guard:** The post-walk safety net `_scrubTemplateExpressions` (`src/purify.ts:3131-3132`) is the only pass that would catch the merged expression, but it never recurses into shadow roots (`src/purify.ts:1717-1751` recurses into `<template>.content` only).\n5. **Primitive:** The adjacent fragments serialize as a single concatenated string on `shadowRoot.textContent` / `shadowRoot.innerHTML`, reconstituting the complete expression.\n6. **Result:** A downstream template engine evaluates the attacker-chosen expression, achieving script execution (or template injection) in the engine's context.\n\n## Bypass Evidence\n\nThe relevant protection is `SAFE_FOR_TEMPLATES`, specifically the post-walk pass that defends against expressions that only form after adjacent text nodes merge. That defense was previously extended to `<template>.content` under CVE-2026-65900 (GHSA-gvmj-g25r-r7wr) by adding `querySelectorAll('template')` recursion at `src/purify.ts:1743-1750`. The strongest challenge is whether any code path performs the equivalent merge-and-rescrub inside shadow roots. Enumerating every call site of `_scrubTemplateExpressions` (`src/purify.ts:1717`, `1747`, `3132`, `3141`) shows none receive a shadow root, and `_sanitizeShadowDOM` (`src/purify.ts:2710`) never calls `_scrubTemplateExpressions` or `normalize()`. The per-node strip cannot compensate: `MUSTACHE_EXPR` (`src/regexp.ts:3`) requires `{{` or `}}`, `TMPLIT_EXPR` (`src/regexp.ts:5`) requires `${`, and `ERB_EXPR` (`src/regexp.ts:4`) requires `<%` or `%>`, none of which appear in a single-character fragment. The CVE-2026-65900 regression tests exercise `<template>.content` only; no test covers shadow root content under `SAFE_FOR_TEMPLATES`. The gap is therefore an unfixed case on a distinct remediation boundary (shadow roots) rather than a bypass of the delivered template-content fix.\n\n## Affected Versions\n\n- **Ecosystem:** `npm`\n- **Package:** `dompurify`\n- **Confirmed affected range:** `>= 3.4.4, <= 3.4.16`\n- **Latest release checked:** `3.4.16` ([npm registry](https://www.npmjs.com/package/dompurify/v/3.4.16))\n- **Fix status:** not fixed\n\nThe lower bound 3.4.4 is the release that introduced both the attached-shadow-root sanitization pass and the `_scrubTemplateExpressions` safety net; releases 3.4.4 through 3.4.7 carry the safety net with no content-container recursion, and 3.4.8 onward add `<template>.content` recursion but still omit shadow root recursion. No published release and no current development revision contains a shadow-root fix.\n\n## Suggested Fix\n\nEnforce the same post-walk normalize-and-rescrub invariant on attached shadow roots that already applies to `<template>.content`. After `_sanitizeShadowDOM` finishes walking a shadow root, run `_scrubTemplateExpressions` over that shadow root so adjacent text nodes created by element removal are merged and re-stripped; alternatively, have `_scrubTemplateExpressions` discover and recurse into attached shadow roots as it does for template content. The fix must cover nested shadow roots and shadow roots inside `<template>.content`, and it must handle all three expression dialects, since any disallowed element can serve as the separator that produces adjacent fragments.\n\n---\n\nReported by zx (GitHub: @manus-pi).","dates":{"published":"2026-10-22T16:25:31.000Z","modified":"2026-10-22T16:45:03.731Z","reserved":null,"kevAdded":[],"kevDue":[]},"cvss":{"headline":{"score":9.2,"severity":"critical","version":"4.0","versionLabel":"v4.0","source":"OSV · Npm","vector":"CVSS:4.0/AV:N/AC:H/AT:P/PR:N/UI:N/VC:H/VI:H/VA:L/SC:N/SI:N/SA:N","computed":true,"selection":"version-preference"},"metrics":[{"version":"4.0","versionLabel":"v4.0","source":"OSV · Npm","score":9.2,"severity":"critical","vector":"CVSS:4.0/AV:N/AC:H/AT:P/PR:N/UI:N/VC:H/VI:H/VA:L/SC:N/SI:N/SA:N","exploitability":null,"impact":null,"computed":true}]},"epss":{"probability":null,"percentile":null,"severity":"unknown","asOf":null,"change":{"pp":null,"baselineDate":null,"baselineScore":null},"history":[],"last30":[]},"kev":{"state":"not-listed","entries":[],"listedCount":0,"totalCatalogs":3,"catalogs":[{"source":"cisa","label":"CISA","listed":false,"entry":null},{"source":"enisa","label":"ENISA","listed":false,"entry":null},{"source":"circl","label":"CIRCL","listed":false,"entry":null}]},"ransomware":{"state":"none-recorded","sources":[]},"exploits":[],"exploitCount":0,"weaknesses":[],"affected":[{"vendor":"Npm","product":"dompurify","name":"dompurify","label":"dompurify","ecosystem":"Npm","cpePart":null,"purl":"pkg:npm/dompurify","purlType":"npm","versions":[{"text":"≥ 3.4.4, < 3.4.17","noFixRecorded":false,"fixedIn":null,"bounds":{"kind":"range","start":"3.4.4","startInclusive":true,"end":"3.4.17","endInclusive":false}}],"versionsText":"≥ 3.4.4, < 3.4.17","eol":null,"hubHref":null}],"affectedCount":1,"eol":{"hasEol":false,"count":0},"references":[{"url":"https://github.com/cure53/DOMPurify/security/advisories/GHSA-cg2g-xh64-v4q7","label":"github.com/cure53/DOMPurify/security/advisories/GHSA-cg2g-xh64-v4q7","hostname":"github.com","tags":["WEB"]},{"url":"https://github.com/cure53/DOMPurify/pull/1672","label":"github.com/cure53/DOMPurify/pull/1672","hostname":"github.com","tags":["WEB"]},{"url":"https://github.com/cure53/DOMPurify/commit/a77a3e78b59f72fa4d1c83f22e121ad9cc7d3575","label":"github.com/cure53/DOMPurify/commit/a77a3e78b59f72fa4d1c83f22e121ad9cc7d3575","hostname":"github.com","tags":["WEB"]},{"url":"https://github.com/cure53/DOMPurify","label":"github.com/cure53/DOMPurify","hostname":"github.com","tags":["PACKAGE"]},{"url":"https://github.com/cure53/DOMPurify/releases/tag/3.4.17","label":"github.com/cure53/DOMPurify/releases/tag/3.4.17","hostname":"github.com","tags":["WEB"]}],"lineage":{"upstream":[],"downstream":["CGA-22J7-CRG7-G892","CGA-23FX-RJ84-WRRV","CGA-23P6-6545-H2CR","CGA-24Q3-F8XG-PVW5","CGA-2CWV-3JXF-J7Q6","CGA-2FQ6-38H2-H2C8","CGA-2FXQ-GCW8-V74M","CGA-2QHJ-4MFC-VQ8X","CGA-2WXM-QPV4-M5FF","CGA-3839-F6W7-GJQ8","CGA-3R52-935J-G6CF","CGA-4767-F744-J4MM","CGA-488G-8WRV-GR78","CGA-4C9P-5HJJ-24V7","CGA-4F9X-RF8V-59M2","CGA-4HC4-6682-MHQF","CGA-4M24-M2HP-J365","CGA-4MQG-6VH5-F57C","CGA-4MV5-F39W-JVJG","CGA-4V3C-GHV5-VC6M","CGA-4XCW-RVX4-Q7X9","CGA-56W4-3J26-68WJ","CGA-5JJM-8F84-WCX8","CGA-5PQ6-M7J5-PP88","CGA-5RV3-5J6X-P4PR","CGA-5RW5-5R3H-VQGW","CGA-5V5P-2XPF-99C6","CGA-5VWR-HFQX-9G6F","CGA-5WPW-W7WX-38JC","CGA-5X2X-RH24-5GXC","CGA-5X3V-P3RC-HJR3","CGA-6668-578W-HC9W","CGA-6CXV-PRWP-2FMM","CGA-6H3C-VV55-R957","CGA-6HCW-F5R6-Q876","CGA-6HXF-GP9V-CF76","CGA-6JCH-3CFP-39QW","CGA-6JMF-VGJF-VQWH","CGA-6JWP-HQ8J-8W6P","CGA-6VMP-CGQC-QVPG","CGA-7873-56RM-4WPF","CGA-7GX4-G8JX-22RF","CGA-7M4M-8R8X-VQWQ","CGA-7V22-5HGJ-8G6F","CGA-7VR2-77CM-HHC8","CGA-7WR8-8VPF-MF73","CGA-85RJ-2VG2-M7CJ","CGA-8HHJ-H4G5-MGMG","CGA-8MRJ-7RM3-CJQW","CGA-8R8W-QWM9-MXCG","CGA-8V9F-J7GC-36J3","CGA-9CPG-G7G4-QMVH","CGA-9F39-PV44-V38G","CGA-9F68-FV8V-X6GW","CGA-9HH8-W486-CCRG","CGA-9W2Q-RW9H-W547","CGA-9X87-5XCX-8392","CGA-C5JH-VG5H-3M4W","CGA-C69C-FFCH-G49Q","CGA-C96V-XCJ7-R5X9","CGA-C996-9JW2-84PQ","CGA-C9HW-9MWP-8QW7","CGA-CJ25-6GGV-V8W6","CGA-CP6J-QM6G-H4HR","CGA-CP9G-CF6F-FQ3G","CGA-CPWP-VJM8-22G9","CGA-CQ66-QR3J-5H3F","CGA-CQX9-HGMQ-FW75","CGA-CR6W-J9XQ-WX58","CGA-CVXH-XW9J-V9W3","CGA-F5M6-RG94-X2FQ","CGA-F762-M4PX-JPWJ","CGA-F87V-9RVM-7CM4","CGA-FH47-CPFX-6WMM","CGA-FH7X-HF2R-4H8P","CGA-FWW7-9C65-5CH3","CGA-FX73-H2FQ-FHX7","CGA-G67R-F23P-2447","CGA-G946-PJ2V-P8G4","CGA-GM68-98P4-P7WW","CGA-GWWX-MM24-QGRP","CGA-H2R8-69QW-Q3C4","CGA-H982-7FFV-X5FV","CGA-HC4G-2563-V8JQ","CGA-HJ6V-VP7F-F467","CGA-HM8Q-8V69-53QJ","CGA-HMF5-VCFP-M3RP","CGA-HPCG-CFRV-3237","CGA-J9J2-QQ3M-8QQ2","CGA-JF9R-H7CG-6FG4","CGA-JG97-GGRC-33J8","CGA-JGR8-73GF-QWX3","CGA-JQR3-72J5-G2M8","CGA-JR7X-JXFX-W8VW","CGA-JV2X-JRVV-V5P3","CGA-JX6F-PCMC-2MG6","CGA-JXW7-FMPR-P3MW","CGA-M8CW-M7WM-GXGW","CGA-MF24-QH4Q-HGGP","CGA-MJ28-HRX7-XJ6V","CGA-MM26-HHJ9-4QGH","CGA-MVVP-4HFX-8FH6","CGA-P878-FG9G-2W22","CGA-P9HC-92VF-XMPF","CGA-P9XW-XH5F-647H","CGA-PF7H-CX93-2JV8","CGA-PV4J-VV7X-987V","CGA-PW5C-X4FF-VX64","CGA-Q7WJ-J9W7-7M5J","CGA-QFR7-3HRC-GVQW","CGA-QH9R-23PP-28C8","CGA-QHC4-MH57-9P9Q","CGA-QMF8-68PG-VFQ7","CGA-QXX3-PXFM-2254","CGA-R99V-GM6X-46H3","CGA-RQQ2-M4X2-C77P","CGA-RV35-PHPQ-P53G","CGA-V5W9-9MCM-8PFJ","CGA-V76M-VP5Q-9RV9","CGA-V794-248Q-V24J","CGA-V8H9-263Q-7GGX","CGA-VCX5-JCGX-9Q65","CGA-VMCV-VQ26-433G","CGA-VQ9C-7RXF-23PV","CGA-VQQF-84WM-PG63","CGA-VR5G-P93W-CM2X","CGA-W52W-GC2W-F25J","CGA-W5PR-9XCC-CV4M","CGA-WCJ2-G78R-M2QV","CGA-WHJR-Q5GR-87VR","CGA-WJ7Q-26H8-JXC8","CGA-WJXG-VFQ3-77RH","CGA-WP3F-GQRG-XXCC","CGA-WPHW-9H4R-W524","CGA-WRPV-2F7Q-G8X5","CGA-WX47-PWMC-2G4V","CGA-X2WR-8V9H-P2W3","CGA-X5WR-QH7G-26XC","CGA-X7WP-3W38-4262","CGA-X9Q4-V985-X42H","CGA-XFRC-XFH2-F2VV","CGA-XJMW-GPH8-VGQ9","CGA-XMMP-8G8P-PVV6","CGA-XQ9V-6JV5-7HWH","CGA-XWHM-X46W-G6H2"],"duplicates":[],"related":[]},"timeline":[{"kind":"published","date":"2026-10-22T16:25:31.000Z","label":"Published","detail":"Vulnerability first disclosed.","source":null},{"kind":"modified","date":"2026-10-22T16:45:03.731Z","label":"Last modified","detail":"Vulnerability information updated.","source":null}],"priority":{"label":"prioritise","tag":"severe","reason":"Severe, CVSS 9.2 (OSV · Npm); no exploitation reported by the 5 checked sources; EPSS not yet published","nextStep":"Check applicability and the vendor advisory: no fix release is recorded here","rank":[0,0,0,0,0,92],"checkedSources":["cisa","enisa","circl","shadowserver","cisa-adp"],"evidence":[{"type":"cvss","source":"osv_npm","date":"2026-10-22T16:45:03.731Z","status":"valid","statusDate":null,"statusSource":null,"disputes":[],"score":9.2,"vector":"CVSS:4.0/AV:N/AC:H/AT:P/PR:N/UI:N/VC:H/VI:H/VA:L/SC:N/SI:N/SA:N","version":"4.0","headline":true}],"epss":{"value":null,"date":null,"stale":false,"promoted":null},"sources":[{"id":"cisa","lastSuccessfulSync":"2026-12-05T13:01:25.582Z","state":"fresh"},{"id":"enisa","lastSuccessfulSync":"2026-12-05T13:01:26.078Z","state":"fresh"},{"id":"circl","lastSuccessfulSync":"2026-12-05T12:01:28.919Z","state":"stale"},{"id":"shadowserver","lastSuccessfulSync":null,"state":"unavailable"},{"id":"cisa-adp","lastSuccessfulSync":"2026-12-05T13:01:07.092Z","state":"fresh"},{"id":"epss","lastSuccessfulSync":"2026-12-05T13:01:23.911Z","state":"fresh"},{"id":"osv","lastSuccessfulSync":"2026-12-05T13:01:22.884Z","state":"fresh"}],"feedEvent":null},"meta":{"dataAsOf":"2026-12-05T13:19:14.238Z","stale":false,"partial":false}}