Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URLs are improperly escaped #57

Closed
JakobKallin opened this issue May 23, 2013 · 4 comments
Closed

URLs are improperly escaped #57

JakobKallin opened this issue May 23, 2013 · 4 comments
Assignees

Comments

@JakobKallin
Copy link

Converting the following Markdown:

http://example.com/'onmouseover='alert(document.cookie)'/

yields the following HTML:

<p><a href='http://example.com/'onmouseover='alert(document.cookie)'/'>http://example.com/'onmouseover='alert(document.cookie)'/</a></p>

which in turn is parsed by the browser as:

<p><a href="http://example.com/" onmouseover="alert(document.cookie)" '="">http://example.com/'onmouseover='alert(document.cookie)'/</a></p>

The reason is that URLs are improperly escaped before being inserted into href attributes.

I discovered this by accident while writing a tutorial on XSS. It's a defect in the Showdown library that would allow for unintended script injection if Markdown weren't designed to allow inline HTML to begin with.

@tivie
Copy link
Member

tivie commented May 28, 2015

This is a necro issue, but I got the time to play around with showdown and XSS.

In short, my conclusion is that you can't really prevent XSS attacks with markdown and claiming to do so will probably give some sense of false security, which is worse.

The 2 rules of thumb are:

  • filter for XSS after Showdown has processed any input, not before
  • preferably server side, not client side

This excellent post by Michael Fortin provices more in-depth explanation

@tivie tivie closed this as completed May 28, 2015
@JakobKallin
Copy link
Author

Correct. As I wrote in my original comment, Markdown already allows markup injection by design, so this doesn't introduce any (unintended) security issues. It's still a defect, though, causing invalid markup to be generated in some cases. To avoid it, URLs should be HTML-escaped before being inserted between attribute quotes.

@tivie tivie added enhancement and removed bug labels May 29, 2015
@tivie
Copy link
Member

tivie commented May 29, 2015

@JakobKallin I agree. The problem is assuring that by URL encoding, well, URLs, we won't break the parser. I will look into it.

@JakobKallin
Copy link
Author

Also keep in mind the difference between URL encoding and HTML escaping (neither of which has a standardized name as far as I know): URL encoding replaces characters that are not allowed in URLs, while HTML escaping replaces characters with special meaning in HTML. While I think both would work fine here in practice, HTML escaping is the correct one in this context.

@tivie tivie mentioned this issue Nov 4, 2015
@tivie tivie mentioned this issue Nov 1, 2018
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants