Scriptless UI Redressing via Allowed class Attributes
An HTML sanitizer that allows the class attribute with any value lets an attacker style their content with the site's own stylesheet. No JavaScript is needed: with a utility-first framework like Tailwind, the classes to cover the whole page are already shipped.
<div class="fixed inset-0 z-50 bg-white">
Session expired. Please log in again at https://attacker.example/login
</div>
Rendered inside a staff page, this becomes a full-screen overlay that looks like part of the application, a phishing prompt the sanitizer considered harmless. The same trick can hide legitimate content or fake buttons.
When class must be allowed
Syntax highlighting is the usual reason. Python-Markdown's codehilite extension uses Pygments, which emits <div class="codehilite"><pre><code> and wraps every token in <span class="k">, <span class="nf">, <span class="s2"> and so on. The fix is to allow only those class names.
Allowlisting class names
Exact set (preferred). Pygments publishes its short class names in pygments.token.STANDARD_TYPES:
from pygments.token import STANDARD_TYPES
PYGMENTS_CLASSES = {c for c in STANDARD_TYPES.values() if c} | {"codehilite", "hll"}
Pass it with nh3.clean(..., allowed_classes={"div": PYGMENTS_CLASSES, "span": PYGMENTS_CLASSES, ...}). nh3 requires that class then not appear in attributes. Alternatively keep class in attributes and drop unknown names in an attribute_filter(tag, attr, value) callback, returning None when nothing is left.
Pattern (weaker). A regex such as ^(codehilite|[a-z]{1,3}\d?)$ matches the Pygments names but also any short class the site defines (btn, row, nav). It is only safe if you know the site's CSS has no 1–3 letter classes.
References
- nh3 documentation -
allowed_classesandattribute_filter; "the class attribute itself should not be whitelisted if this parameter is used" (last checked 2026-10-24). - Pygments: Builtin Tokens - Token types and the short CSS class names
HtmlFormatteremits for them. - Python-Markdown: CodeHilite - Default
codehilitewrapper class and output without Pygments. - Sanitizing Markdown Output with an HTML Allowlist - The sanitization step this allowlist plugs into.
- Django Markdown Rendering XSS Fix – nh3 Sanitization - Lab that shipped the weaker regex as a first fix, with the exact Pygments set left as a follow-up.
- Web Application and Django Security MOC - Hub for Django and web application security notes.
Review cards
Flashcard How can an allowed `class` attribute be abused without any JavaScript?
The attacker reuses the site's own CSS: with Tailwind, fixed inset-0 z-50 bg-white turns their message into a full-page overlay, such as a fake "session expired" login prompt.
Flashcard Why does a Markdown sanitizer usually have to allow `class` at all?
For syntax highlighting: codehilite uses Pygments, which wraps every token in a short class such as <span class="k">.
Quiz Which way of allowing `class` for Pygments highlighting is safest?
- An exact set of names from
pygments.token.STANDARD_TYPES - A regex such as
^(codehilite|[a-z]{1,3}\d?)$ - Any value, as long as
stylestays forbidden - Any value, but only on
span
The regex also matches any short class the site defines (btn, row, nav), and allowing any value lets attackers reuse the site's CSS.
Flashcard What must you do with `class` in nh3's `attributes` when you pass `allowed_classes`?
Leave it out: nh3 requires that class not be in attributes when allowed_classes is used.
Flashcard What's the risk of allowing `class` values with a regex like `^(codehilite|[a-z]{1,3}\d?)$`?
It also matches any short class the site's own CSS defines (btn, row, nav), which attackers can then reuse. Prefer the exact Pygments set.