Sanitizing Markdown Output with an HTML Allowlist
To render untrusted Markdown safely, convert it to HTML, run that HTML through an allowlist-based sanitizer, and only then call mark_safe(). The order matters: sanitizing the Markdown source is useless, because Markdown can produce dangerous HTML (a javascript: link) that doesn't exist as HTML in the input. This fixes Rendering Untrusted Markdown with mark_safe Causes XSS.
html = markdown.Markdown(extensions=["fenced_code", "codehilite"]).convert(value)
return mark_safe(nh3.clean(html))
Why a parser-based allowlist
nh3 is a Python binding to the Rust crate ammonia, which parses HTML with the spec-compliant html5ever parser, keeps only allowlisted tags, attributes and URL schemes, and serializes the cleaned tree back to HTML. Regex filters fail on the many ways browsers parse malformed markup; a real parser sees the same tree the browser will.
bleach, the previous standard Python sanitizer, was deprecated by its maintainer on 2023-01-23. The announcement named no successor; nh3 is the replacement the ecosystem has largely adopted.
What nh3.clean() does with its defaults
| Input | Result |
|---|---|
Allowlisted tags (p, h1–h6, strong, em, ul, ol, li, a, code, pre, blockquote, img…) |
Kept |
<script>, <style> |
Removed with their content |
Other tags not on the list (<iframe>, <form>, <input>, <svg>…) |
Tag removed, inner text kept |
Attributes not on the list (onerror, onclick, style…) |
Removed |
href/src with a scheme not on the list (javascript:, data:, vbscript:) |
The attribute is removed; the element and its text stay |
<a> elements |
Get rel="noopener noreferrer" |
Allowed schemes include http, https, mailto, tel and relative URLs. The defaults are exposed as nh3.ALLOWED_TAGS, nh3.ALLOWED_ATTRIBUTES and nh3.ALLOWED_URL_SCHEMES, so extend a copy rather than writing the lists from scratch.
What an allowlist still lets through
- Images from any
https:URL: a message can embed a tracking pixel that tells the attacker when, and from which IP, a staff member opened it. - Same-origin requests: a relative
src(<img src="/some/action/">) makes the viewer's browser send a GET with their cookies. That only matters if an endpoint changes state on GET, which it shouldn't. - Ordinary links to phishing pages, with normal-looking text.
Drop img from the allowed tags (or proxy images) if the first two matter.
References
- nh3 documentation -
clean()parameters, default allowlists,attribute_filter,allowed_classesandlink_rel(last checked 2026-10-24). - ammonia crate documentation - The Rust sanitizer nh3 wraps, and its default policy.
- mozilla/bleach #698: Bleach is deprecated - Deprecation announcement of 2023-01-23; recommends no specific replacement (last checked 2026-10-24).
- Python-Markdown Change Log - 3.0 entry: untrusted content should be passed through an HTML sanitizer after conversion.
- OWASP Cross Site Scripting Prevention Cheat Sheet - HTML sanitization as the control when users may author HTML.
- Django Markdown Rendering XSS Fix – nh3 Sanitization - Lab applying this to a Django template filter, with tests.
- Web Application and Django Security MOC - Hub for Django and web application security notes.
Review cards
Flashcard Why sanitize the HTML after conversion rather than the Markdown source?
Markdown itself generates dangerous HTML, such as a javascript: link from [text](url), that isn't there as HTML in the source. Only the converted output shows what the browser will get.
Quiz What does `nh3.clean()` with its defaults do to `<a href="javascript:alert(1)">click</a>`?
- Removes the whole link, text included
- Drops the
href, keeps the link and its text - Escapes the tag so it shows as text
- Rewrites the scheme to
https:
A disallowed URL scheme only costs the attribute. Only <script> and <style> are removed together with their content.
Flashcard Why use a parser-based sanitizer like nh3 rather than regex filters?
It parses HTML the way a browser does (html5ever), so malformed markup that slips past a regex is seen as the same tree the browser will build.
Flashcard What can an attacker still do through nh3's default allowlist, without any script?
Embed an https: image as a tracking pixel that tells them when, and from which IP, staff opened the message.