DOMPurify

DOM-only, uber-tolerant XSS sanitizer for HTML, MathML, and SVG that runs in the browser and on the server.

Library
npm
v3.4.16
17,413 stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
89 /100 Excellent
Development Activity 96
Maintenance 96
Community 64
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
87 /100 Excellent
Architecture 88
Code Quality 85
Innovation 87
Learning Curve 88

DOMPurify is a battle-tested XSS sanitizer that cleans untrusted HTML, MathML, and SVG markup before it touches the DOM. Built and maintained by the Cure53 security team since 2014, it works directly with the browser’s own DOM APIs rather than a hand-rolled parser, which keeps it fast and keeps its behavior aligned with how browsers actually render markup.

It ships as a single dependency-free function, DOMPurify.sanitize(dirty), that returns clean HTML, with a rich configuration surface for allow-listing tags and attributes, enforcing Trusted Types, and hooking into every stage of the sanitization pipeline. It runs in every modern browser and on the server via jsdom, and is the reference implementation that inspired the browser-native HTML Sanitizer API.

What You Get

  • A single sanitize() function that accepts a dirty HTML/SVG/MathML string (or DOM node) and returns clean output
  • Configurable allow-lists for tags, attributes, and URI schemes via ALLOWED_TAGS, ALLOWED_ATTR, and related config keys
  • Built-in Trusted Types policy support for browsers that enforce them
  • A hook system (beforeSanitizeElements, uponSanitizeAttribute, afterSanitizeAttributes, etc.) for customizing sanitization at every stage
  • Server-side support via jsdom for sanitizing HTML in Node.js

Common Use Cases

  • Sanitizing user-generated HTML (comments, rich-text editor output, markdown-rendered content) before rendering with innerHTML
  • Cleaning HTML pulled from third-party APIs or CMS content before displaying it
  • Sanitizing SVG uploads to strip embedded scripts and event handlers
  • Enforcing Trusted Types policies in CSP-hardened web applications

Under The Hood

Architecture DOMPurify’s core sanitize() function (src/purify.ts, ~2,900 lines) builds a DOM tree from the input using the browser’s own DOMParser (or an injected WindowLike for server-side jsdom use), then walks that tree node-by-node, checking each element and attribute against allow-lists (ALLOWED_TAGS in src/tags.ts, ALLOWED_ATTR in src/attrs.ts) and regex-based URI/attribute validators (src/regexp.ts), removing or neutralizing anything unsafe before serializing the cleaned tree back to a string, or returning the DOM node/fragment directly when RETURN_DOM or RETURN_DOM_FRAGMENT is set. A hook system threaded through every phase (beforeSanitizeElements, uponSanitizeElement, uponSanitizeAttribute, afterSanitizeAttributes) lets callers intercept individual decisions, and a factory function (createDOMPurify) parameterizes the whole module over a WindowLike so the same code path runs against a real browser window or an injected jsdom window.

Tech Stack Written in TypeScript, compiled with Babel and bundled via Rollup into CJS, ESM, and UMD builds (dist/purify.cjs.js, dist/purify.es.mjs, dist/purify.js) plus a terser-minified production build, with zero runtime dependencies and only an optional @types/trusted-types dev dependency for Trusted Types typings. Testing spans QUnit-based unit tests (test/test-suite.js, 300+ assertions) run under both jsdom and happy-dom node runners, Playwright-driven cross-browser tests across Chromium/Firefox/WebKit including legacy engine snapshots, and property-based fuzz testing via fast-check (test/fuzz/sanitize.fast-check.js).

Code Quality Linting is enforced via xo (an ESLint preset) with Prettier formatting and a pre-commit husky hook. The codebase favors small pure helper functions (src/utils.ts) that wrap native Array/String/RegExp methods defensively, guarding against prototype pollution and monkey-patched globals — a pattern typical of security-critical libraries where correctness failures are security failures. A dedicated typescript/verify.js script installs the package fresh and type-checks consumer code against the shipped .d.ts files, catching type-definition regressions before release.

API Design The public surface is deliberately minimal: DOMPurify.sanitize(dirty, config?) is the only call most consumers need, returning a string by default or a DOM node/fragment when configured. Configuration keys (ALLOWED_TAGS, ALLOWED_ATTR, ADD_TAGS, FORBID_TAGS, SAFE_FOR_TEMPLATES, etc.) are consistently named and documented inline with TSDoc comments in src/config.ts, and the hook API exposes a fixed vocabulary of named hook points rather than free-form events. Getting started requires no setup for browser use (script tag or import DOMPurify from 'dompurify' plus one function call); server-side use requires only injecting a jsdom window, documented in the README with copy-pasteable CJS and ESM examples.

Used by 135 apps in this directory

Go
73%
GPL 3.0

1Panel

Devops · Hosting Control Panel · Monitoring

37,044

The only open-source VPS control panel with native AI agent runtime — deploy websites, Docker stacks, and local LLMs from one web interface.

View details
91
Repo Health
76
Technical
67
Dependency
Built with
Go 73%
Vue 27%
Updated 4 days ago
TypeScript
82%
Other

AFFiNE

Collaboration · Note Taking · Productivity

73,040

Write, draw, and plan in one infinite canvas — the open-source alternative to Notion and Miro that keeps your data yours.

View details
91
Repo Health
87
Technical
67
Dependency
Built with
TypeScript 82%
Rust 11%
Updated 5 days ago
Go
59%
Apache 2.0

agent-orchestrator

AI Agents · AI Code Assistants · Developer Tools

12,440

A local desktop workspace that gives every coding task its own agent, Git branch, and worktree, then tracks tasks, pull requests, CI, and reviews for 27 coding agents on one live Kanban board.

View details
86
Repo Health
83
Technical
68
Dependency
Built with
Go 59%
TypeScript 38%
Updated 4 days ago
TypeScript
56%
Other

agenta

AI Development · Developer Tools · Devops

4,788

The open-source LLMOps platform unifying prompt engineering, evaluation, and observability for teams building reliable LLM applications.

View details
91
Repo Health
77
Technical
67
Dependency
Built with
TypeScript 56%
Python 39%
Updated 5 days ago
TypeScript
100%
Apache 2.0

agentic-inbox

AI Agents · Mcp · Productivity

8,118

A self-hosted email client with an AI agent that reads your inbox, drafts replies automatically, and exposes full email operations over MCP — running entirely on Cloudflare Workers.

View details
31
Repo Health
74
Technical
77
Dependency
Built with
TypeScript 100%
Updated 5 months ago
TypeScript
72%
AGPL 3.0

APITable

Databases · Low Code Platforms

15,623

API-first collaborative spreadsheet-database platform that auto-generates REST APIs and lets teams build internal tools, CRMs, and dashboards without code.

View details
71
Repo Health
77
Technical
60
Dependency
Built with
TypeScript 72%
Java 22%
Updated 3 weeks ago
JavaScript
95%
MIT

AnythingLLM

AI Assistants · Automation · Developer Tools

66,533

The all-in-one AI platform for private document chat, no-code agents, and local LLMs with zero setup friction.

View details
90
Repo Health
78
Technical
64
Dependency
Built with
JavaScript 95%
Updated 6 days ago
TypeScript
89%
Other

anytype-ts

Collaboration · Knowledge Management · Note Taking

8,861

A local-first, end-to-end encrypted knowledge OS that lets you build notes, tasks, wikis, and entire apps — with your data stored offline and synced peer-to-peer.

View details
88
Repo Health
78
Technical
67
Dependency
Built with
TypeScript 89%
Updated 1 weeks ago
Python
59%
Apache 2.0

argilla

AI Development · Data Engineering

5,125

Collaborate on high-quality AI training data with a self-hosted annotation platform built for LLMs, NLP, and multimodal models.

View details
65
Repo Health
81
Technical
61
Dependency
Built with
Python 59%
Jupyter Notebook 21%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers