talira

DOMPurify

A DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG

open sourceMPL-2.0 OR Apache-2.0self-hostablefree

DOMPurify takes a string (or DOM node) of untrusted HTML and returns clean HTML with anything that could run script removed, using the browser's own parser so it is fast and matches what the browser will actually do. It supports HTML5, SVG and MathML, the shadow DOM and Trusted Types, and is configurable with allow-lists, profiles and hooks. On the server it runs on Node.js with jsdom. It is written by the security firm Cure53, whose work on it inspired the browser HTML Sanitizer API.

Problems it solves

  • users can paste HTML into my app and I need to stop them injecting scripts
  • safely show HTML from an API or a rich text field without opening an XSS hole
  • strip onclick handlers, javascript: links and script tags from user content
  • render customer-written HTML inside forms embedded on other people's sites
  • sanitize SVG uploads before showing them in the page
  • my regex-based HTML cleaner keeps getting bypassed
  • allow only bold, italics and links in comments and drop everything else
  • sanitize HTML on the server in Node before storing it
  • make innerHTML assignments safe under a Trusted Types content security policy
  • a sanitizer written by people who actually do XSS research

Features

  • HTML5, SVG and MathML
  • browser-parser-based sanitizing
  • configurable allow-lists and profiles
  • hooks
  • Trusted Types support
  • shadow DOM and template sanitizing
  • returns string, DOM or DocumentFragment
  • server-side use with jsdom
  • SAFE_FOR_TEMPLATES mode