cure53 /DOMPurify
DOMPurify - a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG. DOMPurify works with a secure default, but offers a lot of configurability and hooks. Demo:
★ 17,531⑂ 907👁 146⑃ 983● 723▤ 152
+0
Daily
17,531
stars
907
forks
146
watchers
983
prs
723
issues
152
releases
README
DOMPurify
!npm package minimized gzipped size (select exports)
DOMPurify is a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG.
It's also very simple to use and get started with. DOMPurify was started in February 2014 and, meanwhile, has reached version v3.4.18.
DOMPurify runs as JavaScript and works in all modern browsers (Safari (10+), Opera (15+), Edge, Firefox and Chrome - as well as almost anything else using Blink, Gecko or WebKit). It doesn't break on MSIE or other legacy browsers. It simply does nothing.
Note that DOMPurify v2.5.9 is the latest version supporting MSIE. For important security updates compatible with MSIE, please use the 2.x branch.
Our automated tests cover 9 browser/OS combinations on the current engines (Chromium, Firefox, and WebKit across Ubuntu, macOS, and Windows) on every push, and a separate matrix re-runs the suite on older engine snapshots (back to roughly Chromium 110, Firefox 108 and WebKit 16.4, around three years old) so regressions on outdated browsers get caught too. We also run Node.js v20, v22, v24, v25 and v26 with DOMPurify on jsdom. Older Node versions are known to work as well, but hey... no guarantees.
DOMPurify is written by security people who have vast background in web attacks and XSS. Fear not. For more details please also read about our Security Goals & Threat Model. Please, read it. Like, really. And if you enjoy the gory details, the Attack Classes & Bypass History page catalogs the parser-mutation, namespace, clobbering, and template tricks DOMPurify defends against.
The DOMPurify project inspired the creation of the HTML Sanitizer API, which is already shipping in many browsers. The same capability is now being standardized directly in the WHATWG HTML specification.
Table of Contents
- What does it do?
- How do I use it?
- Is there a demo?
- What if I find a _security_ bug?
- Some purification samples please?
- What is supported?
- What about legacy browsers like Internet Explorer?
- What about DOMPurify and Trusted Types?
- Can I configure DOMPurify?
- Persistent Configuration
- Hooks
- Removed Configuration
- Continuous Integration
- Security Mailing List
- Who contributed?
What does it do?
DOMPurify sanitizes HTML and prevents XSS attacks. You can feed DOMPurify with e.g. a string full of dirty HTML and it will return a string (unless configured otherwise) with clean HTML. DOMPurify will strip out everything that contains dangerous HTML and thereby prevent XSS attacks and other nastiness. It's also damn bloody fast. We use the technologies the browser provides and turn them into an XSS filter. The faster your browser, the faster DOMPurify will be.
How do I use it?
It's easy. Just include DOMPurify on your website.
Using the unminified version (source-map available)
html
Using the minified and tested production version (source-map available)
html
Afterwards you can sanitize strings by executing the following code:
js
const Latest release
DOMPurify 3.4.18 3.4.18 2026-11-14- Fixed a SAFE_FOR_TEMPLATES bypass where the final scrub cut through tags and let a template expression form on reparse, thanks @e1abrador - Fixed a direct property read in the clobbering check, thanks @trace37labs - Removed several entries from the SVG tag allow-list that are attributes and not elements, thanks @hackvertor - Added a console warning when USE_PROFILES is combined with ALLOWED_TAGS or ALLOWED_ATTR, thanks @rugk - Refactored the code base: removed dead code and redundant helpers, shortened comments - Fixed a hook-widened allow-list carrying over when the config object recei