Executive brief
justhtml is a Python library for safely sanitizing and parsing HTML. The default sanitizer fails to remove event handlers from SVG and MathML elements that are cloned during selectedcontent projections, allowing attackers to inject malicious code that executes in users' browsers. Any application using the library's documented safe default mode is vulnerable to stored or reflected XSS attacks.
Technical details
A cross-site scripting (CWE-79) vulnerability exists in the HTML parser's selectedcontent projection mechanism. When processing <selectedcontent> elements, the parser clones child nodes and reinserts them without passing them through sanitization passes. Specifically, SVG and MathML elements with event handler attributes (onload, onclick, animate attributeName, etc.) bypass the deferred sanitization that normally removes such handlers. The vulnerability is in the default safe engine configuration (use_compiled_safe_engine), where _project_selectedcontent() executes before _drop_recorded_nodes() and _unwrap_recorded_nodes(), causing cloned nodes to evade recorded-node removal lists. An attacker can inject malicious payloads like <svg onload=X> into untrusted HTML and trigger XSS when JustHTML(input).to_html() is called. The issue was fixed in version 3.11.0 by reordering the projection pass to occur after sanitization.
Affected products
- Emil Stenstrom justhtml before 3.11.0
Timeline
- 2026-08-23: disclosed
- 2026-08-23: patched: Fixed in version 3.11.0