Summary
Multiple sanitization bypass conditions in justhtml before 1.16.0 may allow dangerous HTML, SVG, MathML, script, or style content to survive filtering. The risk is greatest when applications mutate or reuse policy objects, sanitize programmatically constructed DOM trees, or enable custom foreign-content policies, potentially resulting in cross-site scripting.
Affected context
The issue can occur in advanced justhtml usage rather than the ordinary JustHTML(..., sanitize=True) path for typical parsed HTML. Relevant conditions include reusable or mutable sanitization policies, programmatic DOM input, mixed-case element names, crafted doctype values, and custom policies that preserve SVG or MathML content. Applications that render sanitized output in a browser-facing context may be exposed when active content is not removed.
Exploit preconditions
- Attack surface: Application code that uses justhtml sanitization or DOM serialization.
- Required access: The ability to supply or influence HTML, DOM nodes, policy objects, or related document metadata processed by the application.
- Trigger point: Sanitization or serialization of crafted programmatic input, reused policy state, foreign-content elements, or namespace metadata.
- Why exploitation is possible: Validation and foreign-content checks may rely on incomplete normalization or mutable policy state, allowing dangerous content to remain in output that is later interpreted by a browser.
Why this happens
The underlying bug class is improper input validation, mapped to CWE-20. Sanitization decisions can be bypassed when element names are not normalized consistently, when policy objects retain unsafe mutations across calls, or when namespace, doctype, SVG, and MathML handling does not accurately represent the resulting markup. These patterns weaken the boundary between inert document data and browser-interpreted active content.
Potential impact
- A browser may execute attacker-controlled script content.
- An attacker could access data available to the affected web origin.
- User sessions or application actions could be exposed to unauthorized manipulation.
- Malicious style, animation, or external resource references could affect rendered content.
- Applications could experience content spoofing or persistent client-side compromise.
General mitigation guidance
Mitigation generally focuses on making sanitization deterministic, strictly normalized,
and resistant to policy-state changes.
Common defensive patterns include:
- Using a maintained sanitization implementation with safe-by-default behavior.
- Treating sanitization policies as immutable, isolated configuration rather than shared mutable state.
- Normalizing tag names, attributes, namespaces, and document metadata before applying security decisions.
- Applying conservative allowlists for SVG, MathML, animation elements, style attributes, and external references.
- Validating programmatic DOM objects as strictly as parsed source HTML.
- Encoding or otherwise safely handling sanitized output according to its browser rendering context.
- Limiting the privileges and origin capabilities of applications that process untrusted markup.
How teams detect issues like this
Organizations can look for this class of issue through:
- Static analysis rules identifying shared or mutable sanitizer policy objects.
- Code review checks for separate handling of parsed HTML and programmatic DOM input.
- Tests using mixed-case element names, crafted doctypes, namespace mismatches, SVG and MathML animation elements, and external url(...) references.
- Regression tests that verify dangerous content is absent after repeated sanitization calls.
- Policy enforcement that requires explicit, reviewed allowlists for foreign-content namespaces and active attributes.
- Automated security review tools that flag browser-facing HTML sanitization and serialization paths.

