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.17.7
4,599stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
85/100Excellent
Development Activity100
Maintenance52
Community88
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
81/100Excellent
Architecture82
Code Quality85
Innovation78
Learning Curve80

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
49%
Other

Airbyte

Developer Tools · Data Engineering

21,916

Open-source ELT platform with 600+ connectors for moving data from any source to warehouses, lakes, and AI agents.

View details
95
Repo Health
80
Technical
67
Dependency
Built with
Python49%
Kotlin42%
Updated today
TypeScript
100%
MIT

Botpress

AI Assistants · AI Development · Customer Support

14,872

The open-source hub for building and deploying LLM-powered AI agents with TypeScript-first tooling, 40+ integrations, and a revolutionary code-execution agent framework.

View details
95
Repo Health
82
Technical
66
Dependency
Built with
TypeScript100%
Updated yesterday
TypeScript
69%
Other

Budibase

Low Code Platforms · No Code Platforms

28,220

Build AI agents, automations, and internal apps on a single open-source platform with full self-hosting control.

View details
92
Repo Health
81
Technical
63
Dependency
Built with
TypeScript69%
Svelte26%
Updated today
Java
73%
Other

ByteChef

Automation · AI Agents

990

Unified open-source platform for AI agent orchestration and workflow automation with 180+ connectors, MCP support, and durable execution.

View details
81
Repo Health
82
Technical
73
Dependency
Built with
Java73%
TypeScript26%
Updated 2 days ago
TypeScript
96%
Other

Cal.diy

Scheduling

47,796

The 100% MIT-licensed, community-driven scheduling platform — self-host your own booking infrastructure with no enterprise strings attached.

View details
93
Repo Health
86
Technical
66
Dependency
Built with
TypeScript96%
Updated 1 weeks ago
TypeScript
53%
Other

Chaskiq

CRM · Customer Support

3,560

Self-hosted live chat, video calls, help center, and marketing automation — a full-stack Intercom alternative you run on your own infrastructure.

View details
60
Repo Health
66
Technical
68
Dependency
Built with
TypeScript53%
Ruby36%
Updated 1 months ago
Go
76%
AGPL 3.0

Coder

Devops · Developer Tools · Code Editors

14,199

Self-hosted cloud development environments and AI coding agents — defined in Terraform, connected via WireGuard, automatically shut down when idle.

View details
93
Repo Health
90
Technical
71
Dependency
Built with
Go76%
TypeScript22%
Updated today
TypeScript
80%
Other

Directus

CMS · Low Code Platforms

37,462

Connect any SQL database and get instant REST and GraphQL APIs, a visual management Studio, and a native MCP server for AI agents — free for most organizations.

View details
93
Repo Health
82
Technical
78
Dependency
Built with
TypeScript80%
Vue18%
Updated yesterday
TypeScript
100%
Other

Dub

Marketing · Analytics

24,521

The open-source link attribution platform for short links, conversion tracking, and affiliate programs — powering 100M+ clicks monthly.

View details
81
Repo Health
78
Technical
65
Dependency
Built with
TypeScript100%
Updated today

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

Search