sanitize-html

Allowlist-based HTML sanitizer that strips XSS vectors from untrusted markup while preserving the tags and attributes you choose to keep.

Library
npm
v2.18.1
4,652 stars
MIT License

Repository Health

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

Technical Analysis

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

sanitize-html cleans up user-submitted or otherwise untrusted HTML by keeping only an allowlist of tags and per-tag attributes and discarding everything else. Built on the htmlparser2 streaming parser, it is tolerant of messy input, making it well suited to the fragments produced by rich-text editors like CKEditor and TinyMCE or pasted from Word.

Beyond simple tag filtering, it validates href and src URL schemes, filters iframe hostnames, sanitizes inline styles with PostCSS, escapes all text content, and lets you transform tags on the fly. The result is a small, dependency-light module that gives you fine-grained, server-side control over which HTML is safe to store and render.

What You Get

  • A single sanitizeHtml(dirty, options) function that returns cleaned HTML
  • A configurable per-tag allowlist for tags, attributes, classes, and URL schemes
  • Inline CSS sanitization via PostCSS and iframe hostname filtering
  • transformTags, exclusiveFilter, and textFilter hooks for custom rewriting
  • A sensible default option set covering common rich-text markup

Common Use Cases

  • Sanitizing rich-text editor output before persisting it to a database
  • Cleaning pasted-from-Word markup by stripping unwanted inline styles and tags
  • Rendering user-generated comments or posts without exposing an XSS surface
  • Whitelisting a narrow set of formatting tags for constrained content fields

Under The Hood

Architecture - The entire library is a single ~1,000-line index.js exporting one function, sanitizeHtml. It drives htmlparser2 with onopentag, ontext, onclosetag, and oncomment handlers, maintaining a stack of open tags so it can decide whether each node and its text are kept, escaped, or discarded (tracking depth for discard modes). URL attributes flow through naughtyHref checks, srcset through parse-srcset, and inline styles through a PostCSS AST pass before being reserialized.

Tech Stack - Pure CommonJS JavaScript targeting Node 22+, with a small dependency set: htmlparser2 for parsing, postcss for style sanitization, deepmerge and is-plain-object for option merging, escape-string-regexp, parse-srcset, and launder for href laundering. No build step or transpilation is involved; the published package is just index.js.

Code Quality - The code favors small pure helpers (each, has, filter, isEmptyObject) and defensive guards against prototype-pollution vectors like proto. It is backed by an extensive Mocha suite of roughly 240 test cases in a 2,400-line test file exercising allowlists, URL validation, style filtering, and known XSS payloads, plus ESLint via eslint-config-apostrophe.

API Design - The public surface is deliberately minimal: sanitizeHtml(dirty, options). A well-documented default option set gets developers productive immediately, while allowedTags, allowedAttributes, allowedSchemes, transformTags, exclusiveFilter, and textFilter provide progressively deeper control. The README is thorough with copy-paste examples, keeping the learning curve gentle for the common case.

Used by 42 apps in this directory

Python
45%
GPL 3.0

MaxKB

AI Development · Knowledge Management

23,022

Build enterprise-grade AI agents with RAG, workflows & multi-modal support

View details
92
Repo Health
68
Technical
66
Dependency
Built with
Python 45%
Vue 37%
TypeScript 17%
Updated 6 days ago
Python
58%
Apache 2.0

MLflow

AI Development · Monitoring

28,512

The open source AI engineering platform for debugging, evaluating, monitoring, and optimizing production LLMs and agents at scale.

View details
96
Repo Health
86
Technical
67
Dependency
Built with
Python 58%
TypeScript 35%
Updated 5 days ago
TypeScript
92%
Other

n8n

Automation · No Code Platforms

208,437

Code when you need it, UI when you don't — the workflow automation platform built for technical teams who refuse to choose.

View details
95
Repo Health
87
Technical
62
Dependency
Built with
TypeScript 92%
Updated 6 days ago
TypeScript
99%
Other

NocoBase

Low Code Platforms · No Code Platforms

24,686

Open-source AI + no-code platform that lets coding agents and people collaborate to build business systems fast on proven infrastructure.

View details
94
Repo Health
81
Technical
63
Dependency
Built with
TypeScript 99%
Updated 5 days ago
JavaScript
88%
GPL 3.0

NodeBB

Community

15,242

Modern Node.js forum software with real-time WebSockets, multi-database support, and a plugin ecosystem — the community platform built for the open web and the Fediverse.

View details
97
Repo Health
78
Technical
70
Dependency
Built with
JavaScript 88%
Go Template 11%
Updated 5 days ago
TypeScript
98%
Other

Novu

Developer Tools

40,195

Open-source communication infrastructure that connects your products and AI agents to every channel your users live on — Inbox, Email, SMS, Push, Chat, and more.

View details
93
Repo Health
80
Technical
64
Dependency
Built with
TypeScript 98%
Updated 5 days ago
TypeScript
26%
AGPL 3.0

Omnivore

Bookmarks Archiving · Knowledge Management · Note Taking

16,356

Self-hosted read-it-later platform with highlights, newsletters, PDFs, and seamless Obsidian and Logseq integration.

View details
90
Repo Health
74
Technical
65
Dependency
Built with
TypeScript 26%
JavaScript 24%
HTML 20%
Updated 6 days ago
TypeScript
82%
AGPL 3.0

OpenStatus

Devops · Monitoring

9,221

Open-source status pages and uptime monitoring with global synthetic checks, monitoring-as-code, and AI agent integration via MCP.

View details
77
Repo Health
82
Technical
69
Dependency
Built with
TypeScript 82%
MDX 14%
Updated 6 days ago
Go
36%

OpenPanel

Devops · Hosting Control Panel

764

Docker-powered web hosting control panel that gives every user a fully isolated environment with dedicated web server, database, and networking — VPS-grade security on shared hardware.

View details
87
Repo Health
75
Technical
63
Dependency
Built with
Go 36%
HTML 30%
TypeScript 24%
Updated 6 days 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