Summary
justhtml before 1.15.0 contains multiple input-validation weaknesses in URL sanitization, HTML serialization, Markdown passthrough, and custom sanitization policies. Depending on configuration and attacker control over processed content or programmatically constructed DOM data, these issues may allow active HTML or JavaScript to bypass sanitization and execute in a user's browser.
Affected context
The issues occur in justhtml deployments that process attacker-influenced URLs, markup, Markdown, or programmatically generated element and attribute values. Risk is configuration-dependent: the default sanitize=True behavior may avoid many custom-policy cases, while helper APIs, html_passthrough=True, custom policies, and transformation pipelines can expose additional bypass paths.
Exploit preconditions
- Attack surface: HTML, Markdown, URL, or DOM-construction inputs processed by justhtml
- Required access: Ability to submit or influence content handled by the application
- Trigger point: URL cleaning, HTML serialization, Markdown passthrough, or custom sanitization-policy processing
- Why exploitation is possible: Certain encoded URLs, backslash-based URL forms, markup-sensitive names or comments, raw HTML fragments, and permitted policy elements may not be normalized or constrained consistently before output
Why this happens
The underlying issue is incomplete or inconsistent validation of security-sensitive input across multiple parsing and serialization paths. URL sanitizers may fail to recognize dangerous schemes or browser-specific URL interpretation, while serializers and passthrough features may preserve attacker-controlled syntax that changes document structure or behavior. This maps to CWE-20: Improper Input Validation, because input is accepted without applying a sufficiently complete, context-aware validation policy before it reaches HTML or browser execution contexts.
Potential impact
- An attacker may inject active HTML into rendered pages
- Arbitrary JavaScript may execute in a victim's browser under the application's origin
- Session data or other accessible page information could be exposed
- Users may be redirected to attacker-controlled destinations
- Content security and trust boundaries could be weakened in applications using custom sanitization pipelines
General mitigation guidance
Mitigation generally focuses on applying one consistent, context-aware sanitization policy across every parsing, transformation, and serialization path.
Common defensive patterns include:
- Prefer safe-by-default sanitization and avoid raw HTML passthrough for untrusted content
- Validate and normalize URLs against an explicit scheme and destination policy
- Encode output according to its final context, including HTML text, attributes, URLs, CSS, and script-adjacent contexts
- Restrict programmatic element and attribute names to validated, non-markup-sensitive identifiers
- Use narrowly defined custom policies that deny active elements and navigation mechanisms unless explicitly required
- Keep untrusted content separate from privileged templates and minimize the privileges of affected application sessions
How teams detect issues like this
Organizations can detect related weaknesses through:
- Static analysis rules for unsafe HTML passthrough, custom sanitizer overrides, and inconsistent URL validation
- Code review checks covering
html_passthrough=True, custom policies, serializer inputs, and DOM construction from untrusted values - Security tests using encoded schemes, alternate slash and backslash forms, malformed markup, comment delimiters, and context-breaking characters
- Policy enforcement that requires approved sanitization functions and blocks direct insertion of untrusted HTML
- Browser-based integration tests verifying that rendered output cannot create executable elements, navigation redirects, or script-capable attributes
Automated review tools may help identify risky parser and serializer patterns, but application-specific policy testing remains important.

