在文本输入中放入未初始化的用户输入是否存在XSS风险



我知道,当你在网站上显示用户输入时,使用php中的htmlentities()函数来净化用户输入是很有用的。但是像这样的东西会带来XSS风险吗?

<input type="text" value="<?=user input drawn from the database?>" />

这样对输入进行消毒会更好吗?

<input type="text" value="<?=htmlentities(user input drawn from the database)?>" />

我应该指出,我只是在谈论输入值属性的安全风险,我知道如果我想在网站的其他地方显示用户输入,我仍然必须对其进行消毒。

我可以输入这样的文本,

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,所以您仍然需要注意引号。

可能是的,考虑到来自数据库的用户输入可能有双引号。

您应该使用htmlentities($str, ENT_QUOTES);来转换引号。

http://php.net/manual/en/function.htmlentities.php

But would something like this present an XSS risk?

是的,只要数据在插入数据库之前还没有经过净化,类似的事情就会带来XSS风险。

Would it be better to sanitize the input like this?

是的,在将每个用户输入显示在网页上之前对其进行消毒是很琐碎的。另请查看htmlspecialchar()

最新更新