如何保护以下基于 DOM 的 XSS 攻击?
具体来说,是否有一个 protect() 函数可以使下面的内容安全? 如果不是,那么还有其他解决方案吗? 例如:给 div 一个 id,然后为元素分配一个 onclick 处理程序
<?php
function protect()
{
// For non-DOM XSS attacks, hex-encoding all non-alphanumeric characters
// with ASCII values less than 256 works (ie: \xHH)
// But is it possible to augment this function to protect against
// the below DOM based XSS attack?
}
?>
<body>
<div id="mydiv"></div>
<script type="text/javascript">
var xss = "<?php echo protect($_GET["xss"]) ?>";
$("#mydiv").html("<div onclick='myfunc(\""+xss+"\")'></div>")
</script>
</body>
我希望得到的答案不是“避免使用 innerHTML”或“将 xss 变量正则表达式为 [a-zA-Z0-9]”...即:是否有更通用的解决方案?
谢谢
最佳答案
扩展 Vineet 的回复,这里有一组要研究的测试用例:
关于php - 基于 DOM 的 XSS 攻击和 InnerHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6025418/