html-sanitizer
HTML sanitizer
What it is and what it does
HTML sanitizer is an allowlist-based HTML cleaner that removes unsafe content and normalizes structure. It takes HTML fragments (from rich-text editors, copy-paste, or untrusted sources) and produces valid, safe output by enforcing a configurable set of allowed tags and attributes, stripping inline styles and scripts, and applying structural transforms—for example, converting `<b>` and `<i>` tags to `<strong>` and `<em>`, or merging adjacent identical tags.
It builds on lxml's HTML cleaner and goes further than simpler alternatives by not only blocking unsafe content but also normalizing whitespace, removing empty elements, and applying element preprocessors and postprocessors. Configuration is highly opinionated by default (allowing only semantic tags like headings, lists, links, and emphasis) but can be customized. It includes optional Django integration for managing multiple sanitizer configurations via settings.
Use it for:
- Sanitize user-submitted HTML in comment systems or forum posts before storage or display
- Clean up messy HTML from copy-pasted content in rich-text editors to enforce consistent styling
- Remove XSS attack vectors from untrusted HTML sources before rendering in web pages
- Normalize and validate HTML fragments in content management systems or static site generators
- Convert non-semantic formatting tags (bold, italic) into semantic equivalents for accessibility
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Sanitizes HTML fragments by enforcing an allowlist of safe tags and attributes, removing inline styles and scripts, and normalizing structure to produce valid, consistent output.
Yes, with conditions. Install if you need strict, opinionated HTML sanitization with structural normalization and you are comfortable with an aging but stable codebase. The low install friction, permissive license, zero known vulnerabilities, and active repository make it safe to adopt. However, the 410-day release cycle suggests limited active development; verify that its feature set and dependency versions align with your current stack before committing to a long-term dependency.
Install
html-sanitizer on PyPI
pip
pip install html-sanitizeruv
uv add html-sanitizerpoetry
poetry add html-sanitizerInstalling html-sanitizer
Before you install
Low install friction with a pure-Python wheel and only three runtime dependencies (beautifulsoup4, lxml-html-clean, lxml). Maintenance status is aging—last release was 410 days ago—but the repository remains active with a recent commit on 2025-06-30 and no archived status.
License in practice
BSD-3-Clause is a permissive license; you may use, modify, and distribute this package freely in both open-source and commercial projects with minimal restrictions.
Quickstart
from html_sanitizer import Sanitizer
sanitizer = Sanitizer()
result = sanitizer.sanitize('<p>some text</p>')
Verify before relying
- Whether the 410-day release cycle reflects active maintenance or stable-but-dormant status
- Performance characteristics on large HTML documents or high-throughput sanitization workloads
- Compatibility with recent versions of beautifulsoup4 and lxml beyond the stated Python version support
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 3 — beautifulsoup4, lxml-html-clean, lxml |
| Maintenance | aging — 410 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 916,770/month — #4,729 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: html_sanitizer-2.6.0-py3-none-any.whl
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
django-bleachIntegrates bleach HTML sanitization into Django…
permissive · top 15,000 on PyPI
textileConverts Textile markup (a human-friendly text…
permissive · top 15,000 on PyPI
bleach-allowlistProvides curated allowlists of HTML tags,…
permissive · top 5,000 on PyPI
html-void-elementsProvides a list of HTML void element tag names…
copyleft · top 15,000 on PyPI
py_svg_hushSanitizes SVG files by removing malicious…
unclear · top 15,000 on PyPI
lxml-html-cleanCleans and sanitizes HTML by removing unwanted…
permissive · top 5,000 on PyPI
bleachBleach sanitizes untrusted HTML by escaping or…
permissive · top 1,000 on PyPI
sanitize-filenameRemoves or replaces problematic characters from…
permissive · top 15,000 on PyPI
html-textExtracts plain text from HTML while filtering…
permissive · top 5,000 on PyPI
django-markdownifyA Django template filter that converts Markdown…
permissive · top 15,000 on PyPI