Python Markdown2 XSS Vulnerability and Prevention


As we all know, we usually use one or more 3th party application in our Django project and we rarely consider these applications security. In this article I will describe what I found in Markdown python library and how we can use markdown2 without having security issue.

Before go further I would like to tell how I meet with markdown2. One of mine friend started a awesome project and he used Django. One of the project requirements was Markdown2. So I decided to do some test on it.

Once you installed markdown2 with above command. You will be free to execute following commands.

Understand how Markdown2 handles XSS

Here is what I’ve test in first.

This is awkward. In first example it seems markdown2 does output encoding for < and > . But second example shows that markdown2 doesn’t encode < and > when space between < and words.

Let’s try one of the most command payloads.

As you can see markdown encoding tag closer character even if there is space between tag closer and something. We know that we don’t need tag closer characters for XSS. Please look following HTML content. It will execute even if there is no tag closer character.

Which means we have a XSS vulnerability in Markdown2.

Markdown2 Safe Mod

Markdown2 has a feature for XSS prevention. You can set safe_mod to True  for enable this feature. Let’s try to understand what is safe_mode behaviour.

Blacklisting is not good. Let me show you how to bypass this safe_mode without taking your time. Please don’t forget that we don’t need tag closer for successfully XSS exploitation.

Boom! The trick is not rocket science.

  • Double tag openered character ( < )
  • End of payload we need some space ending with random character. I choose use XSSEND for this purpose.

As you can see, we managed to get valid html based XSS payload.


I suggest you to read XSS Security on Django ( )article before go further. Therefor you can easily understand context based XSS payloads.

Markdown2 safe_mode feature is not feasible for making secure your applications against XSS.

This will secure your application html context based XSS payloads. If you gonna use markdown output in different context like JS or Attribute. I suggest you to read XSS Security on Django article, again.

  • .sh

    Nice post bro. Comments you motivated.:)