逃避XSS漏洞维护Markdown语法? [英] Escape from XSS vulnerability maintaining Markdown syntax?

查看:163
本文介绍了逃避XSS漏洞维护Markdown语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算在我的网页中使用 Markdown语法。我将在数据库中保留用户输入(原始,不转义或任何内容),然后像往常一样,使用 htmlspecialchars()



这是它的外观:



echo markdown(htmlspecialchars($ content));



防止XSS漏洞和Markdown工作。或者,至少有点工作。



问题是,让我们说,> 语法(还有其他简单来说,引用你这样做:



>这是我的报价。



在转义和解析到Markdown之后,我得到:



&安培; GT;这是我的报价。



自然而然,Markdown解析器识别& gt ; 为报价符号,而且工作! :(



我来到这里要求解决这个问题,一个想法是:



解析到Markdown,然后使用 HTML Purifier 删除坏零件。



你有什么想法吗?它实际上可以工作吗?



我确信有人有同样的情况,也可以帮助我。 :)

解决方案

是的,某个网站有完全相同的情况。当我写这篇文章的时候,你在这个网站上有1664个声望:)



在堆栈溢出中,我们完全按照你所描述的(除了我们不在飞行中呈现)。用户输入的Markdown源将转换为纯HTML,然后使用白名单方法对结果进行清理( JavaScript版本,C#版本第1部分第2部分)。



与HTML Purifier所使用的方法相同(从未使用过,我无法说明细节)。


I'm planning to use Markdown syntax in my web page. I will keep users input (raw, no escaping or whatever) in the database and then, as usual, print out and escape on-the-fly with htmlspecialchars().

This is how it could look:

echo markdown(htmlspecialchars($content));

By doing that I'm protected from XSS vulnerabilities and Markdown works. Or, at least, kinda work.

The problem is, lets say, > syntax (there are other cases too, I think).

In short, to quote you do something like this:

> This is my quote.

After escaping and parsing to Markdown I get this:

> This is my quote.

Naturally, Markdown parser do not recognize > as "quote's symbol" and it does not work! :(

I came here to ask for solutions to this problem. One idea was to:

First, parse to Markdown, — then with HTML Purifier remove "bad parts".

What do you think about it? Would it actually work?

I'm sure that someone had have the same situation and the one can help me too. :)

解决方案

Yes, a certain website has that exact same situation. At the time I'm writing this, you have 1664 reputation on that website :)

On Stack Overflow, we do exactly what you describe (except that we don't render on the fly). The user-entered Markdown source is converted to plain HTML, and the result is then sanitized using a whitelist approach (JavaScript version, C# version part 1, part 2).

That's the same approach that HTML Purifier takes (having never used it, I can't speak for details though).

这篇关于逃避XSS漏洞维护Markdown语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆