VAITP Dataset

← Back to the dataset

CVE-2026-44896

Mistune's image directive allows XSS via unescaped attribute injection.

  • CVSS 5.3
  • CWE-79
  • Input Validation and Sanitization
  • Remote

Mistune is a Python Markdown parser with renderers and plugins. In 3.2.0 and earlier, in src/mistune/directives/image.py, the render_figure() function concatenates figclass and figwidth options directly into HTML attributes without escaping. This allows attribute injection and XSS even when HTMLRenderer(escape=True) is used, because these values bypass the inline renderer. Version 3.2.1 contains a patch.

CVSS base score
5.3
Published
2026-05-26
OWASP
A03 Injection
Orthogonal defect classification
Checking
Code defect classification
Missing Check
Category
Input Validation and Sanitization
Subcategory
Cross-Site Scripting (XSS)
Accessibility scope
Remote
Impact
Arbitrary Code Execution
Affected component
Mistune
Fixed by upgrading
Yes

Solution

Upgrade Mistune to version 3.2.1 or later.

Vulnerable code sample

import mistune
from mistune.directives import Directive, Plugin

# This example re-creates the vulnerable logic found in mistune < 3.2.1.
# It uses a custom directive plugin to process a Markdown block.

class VulnerableImageDirective(Directive):
    def __call__(self, md):
        self.register_directive(md, 'figure')

    def parse(self, block, m, state):
        options = dict(self.parse_options(m))
        src = options.get("src", "")
        alt = options.get("alt", "")
        figclass = options.get("figclass", "")
        figwidth = options.get("figwidth", "")
        
        # The vulnerability lies in the render method, which is called later.
        # Here we just pass the raw, unescaped options.
        return {
            'type': 'figure',
            'attrs': {
                'src': src,
                'alt': alt,
                'figclass': figclass,
                'figwidth': figwidth,
            }
        }

    # This method mimics the vulnerable code in src/mistune/directives/image.py
    def render_html_figure(self, attrs):
        src = attrs["src"]
        alt = attrs["alt"]
        figclass = attrs["figclass"]
        figwidth = attrs["figwidth"]

        html = f'<img src="{src}" alt="{alt}">'

        # VULNERABLE CONCATENATION FOR FIGCLASS
        klass = ''
        if figclass:
            klass = ' class="' + figclass + '"'

        # VULNERABLE CONCATENATION FOR FIGWIDTH
        style = ''
        if figwidth:
            style = ' style="width: ' + figwidth + '"'

        return f'<figure{klass}{style}>{html}</figure>'

# Malicious Markdown payload
# The figclass attribute is crafted to close the class="..." attribute
# and inject an onmouseover event handler.
malicious_markdown = """
.. figure::
   :src: image.png
   :alt: picture
   :figclass: "><script>alert('XSS')</script>
"""

# Setup mistune with the vulnerable plugin.
# Note that escape=True has no effect on this vulnerability.
markdown_parser = mistune.create_markdown(
    escape=True,
    plugins=[VulnerableImageDirective()]
)

# Render the malicious markdown
injected_html = markdown_parser(malicious_markdown)

# The output demonstrates the successful XSS injection.
print(injected_html)

Patched code sample

import html

def patched_render_figure(src, alt="", title="", figclass="", figwidth=""):
    """
    A simplified function representing the patched code from mistune v3.2.1.
    In the actual library, this is a method of a class.
    """
    # Dummy representation of an internal image rendering call
    inner_image_html = f'<img src="{html.escape(src, quote=True)}" alt="{html.escape(alt, quote=True)}">'

    html_output = '<figure'
    if figclass:
        # FIX: The 'figclass' value is properly escaped to prevent attribute injection.
        escaped_figclass = html.escape(figclass, quote=True)
        html_output += ' class="' + escaped_figclass + '"'

    if figwidth:
        # FIX: The 'figwidth' value is properly escaped to prevent style attribute injection.
        escaped_figwidth = html.escape(figwidth, quote=True)
        html_output += ' style="width: ' + escaped_figwidth + '"'

    html_output += '>\n'
    html_output += inner_image_html
    if title:
        html_output += '\n<figcaption>' + html.escape(title) + '</figcaption>'
    html_output += '\n</figure>'
    return html_output

# Example of vulnerable input
malicious_class = '" onload="alert(\'xss\')'
malicious_width = '100px;" onload="alert(\'xss\')'

# The patched function correctly escapes the malicious input, neutralizing the attack.
# Vulnerable output would have been: <figure class="" onload="alert('xss')">...
# Patched output is safe: <figure class="" onload="alert('xss')">...
safe_html_output = patched_render_figure("image.jpg", alt="test", figclass=malicious_class, figwidth=malicious_width)

# print(safe_html_output)

Payload

.. figure:: image.png
   :figclass: a" onmouseover="alert('XSS')

   This is a caption.

Cite this entry

@misc{vaitp:cve202644896,
  title        = {{Mistune's image directive allows XSS via unescaped attribute injection.}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2026},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2026-44896},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-44896/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.

The supreme art of war is to subdue the enemy without fighting.

Sun Tzu – “The Art of War”

:: Shaping the future through research and ingenuity ::