这个preg_match能成功阻止XSS吗?
我读到,即使您剥离 您仍然容易受到 XSS 攻击。
我发现一个有趣的答案是这个
你如何评估这个 preg 匹配?
echo preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $var);
此外,对于 XSS 攻击,我还应该注意其他任何标签吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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
正则表达式不足以过滤危险的 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.
您可以尝试在 while 循环中消除脚本标签,直到找不到更多脚本标签:
您还应该检查 onevent 元素属性,例如:onclick、onfocus 等。它们还可能包含不需要的 XSS。
You could try eliminating script tags in a while loop, until there is no more script tags to be found:
You should check onevent element properties also, like: onclick, onfocus, etc. They can also contain unwanted XSS.