这个preg_match能成功阻止XSS吗?

发布于 2024-12-02 04:29:25 字数 320 浏览 2 评论 0 原文

我读到,即使您剥离

我发现一个有趣的答案是这个 t>alert(1337)

你如何评估这个 preg 匹配?

echo preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $var);

此外,对于 XSS 攻击,我还应该注意其他任何标签吗?

I read that even if you strip <script> you are still vulnerable to XSS.

Something interesting I found as an answer is this <scrip<script></script>t>alert(1337)</script>

How do you evaluate this preg match?

echo preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $var);

Additionally, is there any other tags I should be aware for XSS attacks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红衣飘飘貌似仙 2024-12-09 04:29:25

strip_tags 足以消除 XSS 问题。但使用单个正则表达式则不然,因为您需要清理所有 HTML 属性和标签并将其列入白名单。浏览器非常宽容,甚至允许不符合标准的格式错误的 HTML(还有 IE 错误)。这就是为什么使用正则表达式来实现这一点几乎是不可行的。 (尽管有愚蠢的 SO meme,但可以将 HTML 与当代正则表达式语言相匹配,只是太费力了。)

您会发现的所有正则表达式解决方案都是黑名单,它们不被认为是可靠的解决方案。他们将错过一半可能的漏洞 http://ha.ckers.org/xss.html

strip_tags is sufficient to get rid of XSS issues. But using a single regex is not, as you need to cleanse and whitelist all HTML attributes and tags. Browsers are extremely forgiving and allow even malformed HTML that's not standards-compliant (also IE bugs). That's why it is pretty much unfeasible to use a regex for that. (Despite the silly SO meme it is possible to match HTML with a contemporary regex language, just way too much effort.)

All the regex solutions you will find are blacklists, which are not considered a reliable solution. They will miss half of the possible exploits http://ha.ckers.org/xss.html

赠佳期 2024-12-09 04:29:25

正则表达式不足以过滤危险的 HTML。您必须正确解析 HTML,并删除格式错误的标签以及非白名单标签。使用现有的库,例如 HTML purifier很容易犯这个错误。

Regular expressions are not sufficient to filter dangerous HTML. You must properly parse the HTML, and drop malformed tags as well as non-whitelisted tags. Use an existing library such as HTML purifier; it is far too easy to get this wrong.

生生不灭 2024-12-09 04:29:25

您可以尝试在 while 循环中消除脚本标签,直到找不到更多脚本标签:

while (preg_match("'[<]script.*?/script[>]'is",$data))
{
    $data = preg_replace("'[<]script.*?/script[>]'is","",$data);
}

您还应该检查 onevent 元素属性,例如:onclick、onfocus 等。它们还可能包含不需要的 XSS。

You could try eliminating script tags in a while loop, until there is no more script tags to be found:

while (preg_match("'[<]script.*?/script[>]'is",$data))
{
    $data = preg_replace("'[<]script.*?/script[>]'is","",$data);
}

You should check onevent element properties also, like: onclick, onfocus, etc. They can also contain unwanted XSS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文