Registry / serialization / nh3
library0.3.7pypypi✓ verified 25d ago

nh3 is a Python binding to the Ammonia HTML sanitizer Rust crate, providing fast and configurable whitelist-based HTML sanitization. It is notably faster than pure-Python alternatives like `Bleach`, which it largely replaces since `html5lib` became unmaintained. The library is actively maintained, with its current version 0.3.4, and receives regular updates to both the Python bindings and its underlying Rust components.

pip install nh3
INSTALL
IMPORT
SIG · NH3
N
nh3
serializationpythonv0.3.7
Install
1.6s avg
Import
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 20.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 21MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

clean
from nh3 import clean
Cleaner
from nh3 import Cleaner

Sanitize an HTML fragment using the default configuration, then customize allowed tags, and finally demonstrate creating a reusable `Cleaner` instance with specific rules for tags, attributes, and URL schemes.

import nh3 # Basic sanitization unclean_html = "<b><img src='' onerror='alert(\'hax\')'>XSS?</b><script>alert('malicious')</script><p>Hello</p>" cleaned_html = nh3.clean(unclean_html) print(f"Cleaned HTML: {cleaned_html}") # Customizing allowed tags custom_cleaned = nh3.clean(unclean_html, tags={"b", "p"}) print(f"Custom cleaned (b, p only): {custom_cleaned}") # Using Cleaner for reusable configuration my_cleaner = nh3.Cleaner( tags={"b", "i", "p"}, attributes={ "a": {"href"}, "img": {"src"} }, url_schemes={"http", "https"} ) reusable_cleaned = my_cleaner.clean("<a href='javascript:alert(\"xss\")'>Click</a><b>Bold</b><i>Italic</i><script>evil</script>") print(f"Reusable cleaner output: {reusable_cleaned}")
Debug
Known issues
breakingnh3 is a modern replacement for the deprecated `Bleach` library. Users migrating from `Bleach` should note that while APIs are similar, configuration and underlying behavior may differ, requiring careful review of sanitization rules.
fix
Review `nh3` documentation, especially for `tags`, `attributes`, and `url_schemes` parameters, to match your desired sanitization policy. Consider `nh3.Cleaner` for explicit, reusable configurations.
affects: All versions (when migrating from Bleach)
gotchaThe default `nh3.clean()` function allows a relatively broad set of HTML tags (around 75) and attributes. This might be too permissive for many applications and could inadvertently allow potentially unsafe content if not explicitly restricted.
fix
Always explicitly specify the `tags` and `attributes` you want to allow using the `nh3.clean()` function parameters or by configuring an `nh3.Cleaner` instance. For example, `nh3.clean(html, tags={'p', 'b', 'i'}, attributes={'a': {'href'}})` to define a strict whitelist.
affects: All versions
breakingAs `nh3` is a binding to the Rust `ammonia` crate, security vulnerabilities in the underlying Rust library can affect it. Regular updates are necessary to incorporate critical fixes. For example, recent updates addressed `RUSTSEC-2025-0071`.
fix
Keep `nh3` up-to-date with the latest releases. Monitor the `nh3` GitHub repository and PyPI for security announcements and new versions.
affects: All versions before 0.3.x (and any future versions with unpatched underlying Rust crate vulnerabilities)
gotchaWhen integrating with web frameworks like Django, simply calling `nh3.clean()` in templates or view logic may not be sufficient. Unsanitized data might still be saved to the database if sanitization isn't applied at the input (e.g., form field or model field) level.
fix
Implement sanitization in Django form fields (e.g., in `to_python`) or consider using a dedicated package like `django-nh3` (if mature) that provides a sanitized model field. Always sanitize user-provided input *before* saving it to the database.
affects: All versions (when used in web frameworks without proper integration)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'nh3'
The 'nh3' library is not installed in your current Python environment, or there is a typo in the import statement.
fix
Install the library using pip: `pip install nh3`
TypeError: 'module' object is not callable
You are attempting to call the 'nh3' module directly as a function (e.g., `nh3(html_string)`), instead of calling its `clean` function.
fix
Use the `clean` function within the module to sanitize HTML: `nh3.clean(html_string)`
TypeError: Argument 'tags' must be set, not list
A parameter in `nh3.clean()`, such as `tags`, `clean_content_tags`, or `url_schemes`, was provided with an incorrect data type (e.g., a list instead of a set).
fix
Ensure arguments like `tags`, `clean_content_tags`, and `url_schemes` are passed as Python `set` objects, and `attributes` as a dictionary of `set` objects, as specified in the documentation. For example, `nh3.clean(html_string, tags={'b', 'i'})`
Upgrade
Version history
0.3.7latest on PyPI · released Aug 23, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
56 hits · last 30 days
node
46
Amazon
2
OpenAI (training)
1
Resources
nh3 — pip install nh3 · libregistry