我知道当您在网站上显示用户输入时,使用像php中的htmlentities()这样的函数来清理用户输入是很有用的。但是这样会出现XSS风险吗?
<input type="text" value="<?=user input drawn from the database?>" />
像这样清理输入会更好吗?
<input type="text" value="<?=htmlentities(user input drawn from the database)?>" />
我应该指出我只是谈论输入值属性的安全风险,我知道如果我想在网站的其他地方显示它,我仍然需要清理用户输入。
答案 0 :(得分:3)
我可以输入这样的文字,
escape!" /><script>alert("I escaped, because you didn't escape!");</script><img src="
将为您提供以下输出:(为可读性而格式化)
<input type="text" value="escape!" />
<script>
alert("I escaped, because you didn't escape!");
</script>
<img src="" />
你只是连接字符串,而不是操纵DOM,所以你仍然需要注意引号。
答案 1 :(得分:2)
可能是的,考虑来自数据库的用户输入可能有双引号。
您应该使用htmlentities($str, ENT_QUOTES);
转换引号。
答案 2 :(得分:1)
But would something like this present an XSS risk?
是的,只要数据尚未经过清理,在将数据插入数据库之前就会出现XSS风险。
Would it be better to sanitize the input like this?
是的,在将每个用户输入显示在网页上之前清理它是微不足道的。另请查看htmlspecialchars()