use*_*289 7 javascript php xss html-entities
问题:
什么是最好的safe1(),safe2(),safe3()和safe4()函数来避免UTS8编码页面的XSS?它在所有浏览器(特别是IE6)中也是安全的吗?
<body><?php echo safe1($xss)?></body>
<body id="<?php echo safe2($xss)?>"></body>
<script type="text/javascript">
var a = "<?php echo safe3($xss)?>";
</script>
<style type="text/css">
.myclass {width:<?php echo safe4($xss)?>}
</style>
Run Code Online (Sandbox Code Playgroud)
.
很多人说可以做的绝对最好的是:
// safe1 & safe2
$s = htmlentities($s, ENT_QUOTES, "UTF-8");
// But how would you compare the above to:
// https://github.com/shadowhand/purifier
// OR http://kohanaframework.org/3.0/guide/api/Security#xss_clean
// OR is there an even better if not perfect solution?
Run Code Online (Sandbox Code Playgroud)
.
// safe3
$s = mb_convert_encoding($s, "UTF-8", "UTF-8");
$s = htmlentities($s, ENT_QUOTES, "UTF-8");
// How would you compare this to using using mysql_real_escape_string($s)?
// (Yes, I know this is a DB function)
// Some other people also recommend calling json_encode() before passing to htmlentities
// What's the best solution?
Run Code Online (Sandbox Code Playgroud)
.
关于PHP和XSS的帖子很多.大多数人只是说"使用HTMLPurifier"或"使用htmlspecialchars",或者说错了.其他人说使用OWASP - 但它非常慢.我遇到的一些好帖子如下:
safe2()
显然是htmlspecialchars()
相反,safe1()
您应该真正使用它HTMLPurifier
来清理完整的 HTML 块。它会去除不需要的属性、标签,特别是任何 javascript 风格的东西。是的,它很慢,但它涵盖了所有小的边缘情况(即使是较旧的 IE 版本),允许安全地重用 HTML 用户片段。但请查看http://htmlpurifier.org/comparison以获取替代方案。-- 如果您真的只想在那里显示原始用户文本(没有过滤的 html),那么htmlspecialchars(strip_tags($src))
实际上可以正常工作。
safe3()
尖叫正则表达式。在这里,您实际上只能将白名单应用于您真正想要的内容:
var a = "<?php echo preg_replace('/[^-\w\d .,]/', "", $xss)?>";
Run Code Online (Sandbox Code Playgroud)
您当然可以使用json_encode
此处来获取完全有效的 JS 语法和变量。但是,您只是将该字符串的可利用性延迟到了 JS 代码中,然后您必须在其中照顾它。
它在所有浏览器(特别是 IE6)中也安全吗?
如果您明确指定字符集,那么 IE 将不会执行其可怕的内容检测魔法,因此可以忽略 UTF7 漏洞。
归档时间: |
|
查看次数: |
5605 次 |
最近记录: |