A JavaScript sanitizer that strips dangerous markup from HTML, SVG, and MathML strings before they reach the DOM. It runs in modern browsers and can be used with Node.js when a DOM implementation is present.
Overview
DOMPurify is a client-side JavaScript library focused on preventing cross-site scripting when untrusted HTML must be shown or inserted into a page. It accepts dirty markup as a string or DOM node and returns cleaned HTML by removing elements and attributes that could execute scripts or otherwise enable attacks.
It supports HTML5, SVG, and MathML by default, with configuration for tighter allow lists, hooks, persistent settings, and Trusted Types integration. The library uses browser parsing APIs for speed and can sanitize open shadow DOM trees; closed shadow roots and certain mixed parsing contexts require careful handling as described in its security guidance.
An interactive demo lets developers paste sample payloads, choose presets such as HTML-only or strict text formatting, and inspect timing, removed nodes, and sanitized output. Server-side use typically pairs DOMPurify with a maintained jsdom release; edge runtimes may rely on separate compatibility projects.
Security documentation covers threat models, attack classes, bypass history, and safe patterns when combining sanitization with other libraries or post-processing steps.
Key features
- HTML, SVG, and MathML purification
- Configurable tags and attributes
- Hook and policy customization
- Trusted Types API support
- DOM node and string inputs
- Shadow DOM template walking
- Node.js use with jsdom
- Interactive sanitization demo
Use cases
- Rendering user-generated rich text
- Cleaning HTML before innerHTML insertion
- Angular and jQuery HTML sinks
- Server-side sanitization in Node
- Testing XSS payloads against rules