DOMPurify
XSS sanitizer for HTML, SVG and MathML that removes dangerous elements and attributes before rendering.
Official Documentation ↗dompurify v3.x / 浏览器;Node 需 jsdom 等 DOM 环境. Check the official docs for the latest API surface.Installation
npm install dompurifypnpm add dompurifyyarn add dompurifyMethods
No methods match this filter.
Overview
DOMPurify cleans untrusted HTML, SVG and MathML by removing dangerous elements, attributes and URL schemes. It is the common defense against stored and reflected XSS when rendering user content.
Core Methods
sanitize takes a dirty string and optional configuration, returning a safe string or trusted type. Configuration controls allowed tags and attributes, data attributes and whether to keep contents of removed elements.
Usage Notes
Always sanitize before inserting into the DOM, tighten the allowlist to the minimum needed, and restrict href and src to safe protocols. Sanitize on both server and client for defense in depth.
Common Pitfalls
Performing sanitization after insertion is too late. Allowing style or event attributes reopens XSS. Treating sanitized HTML as safe in a scripting context still enables injection, so keep contexts separated.
Errors
The following points are quoted verbatim from the official documentation (https://github.com/cure53/DOMPurify). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- Okay, makes sense, let’s move on After sanitizing your markup, you can also have a look at the property DOMPurify.removed and find out, what elements and attributes were thrown out. (https://github.com/cure53/DOMPurify)
- That is circular by definition - sanitizing would call the policy, which sanitizes by calling DOMPurify again - and DOMPurify will throw a descriptive TypeError to prevent the infinite recursion. (https://github.com/cure53/DOMPurify)
- If it is, DOMPurify strips the root’s subtree of every non-allow-listed attribute and then throws a TypeError, so a rejected root is never handed back armed. (https://github.com/cure53/DOMPurify)
Security notes
Quoted from the official documentation (https://github.com/cure53/DOMPurify); this is not a complete threat model.
- DOMPurify is written by security people who have vast background in web attacks and XSS. (https://github.com/cure53/DOMPurify)
- For more details please also read about our Security Goals & Threat Model. (https://github.com/cure53/DOMPurify)
- DOMPurify will strip out everything that contains dangerous HTML and thereby prevent XSS attacks and other nastiness. (https://github.com/cure53/DOMPurify)
FAQ
- Is sanitize enough to prevent XSS?
- Sanitizing untrusted HTML is a key control, but URL attributes and context matter. Restrict allowed tags, validate link protocols, and avoid inserting results into unsafe contexts such as script or style.
- Can DOMPurify run on the server?
- It needs a DOM implementation, so on Node it is used with jsdom or a similar environment. Sanitize server-side for defense in depth and again client-side where content is rendered.
- What are Trusted Types?
- A browser feature that only allows TrustedHTML assignment to dangerous sinks. Returning trusted types from DOMPurify integrates with a Trusted Types policy so unsanitized strings are rejected at the sink.
Report a problem with this API docOpens a pre-filled issue on GitHub.