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

Html 将未初始化的用户输入放入文本输入中是否存在XSS风险?,html,forms,security,xss,Html,Forms,Security,Xss,我理解,当您在站点上显示用户输入时,使用php中的htmlentities()之类的函数对其进行清理是很有用的。但这样的事情是否会带来XSS风险 <input type="text" value="<?=user input drawn from the database?>" /> 是的,只要在将数据插入数据库之前未对其进行消毒,类似的情况就会出现XSS风险 Would it be better to sanitize the input like this? 是

我理解,当您在站点上显示用户输入时,使用php中的htmlentities()之类的函数对其进行清理是很有用的。但这样的事情是否会带来XSS风险

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

是的,只要在将数据插入数据库之前未对其进行消毒,类似的情况就会出现XSS风险

Would it be better to sanitize the input like this?

是的,在将每个用户输入显示在网页上之前对其进行清理是很简单的。也看一下

可能是,考虑数据库中的用户输入可能有双引号。 你应该使用
htmlentities($str,entu引号)以转换引号


我可以像这样输入文本

escape!" /><script>alert("I escaped, because you didn't escape!");</script><img src="
逃跑!“/>警惕(“我逃走了,因为你没有逃走!”);
这将为您提供以下输出:(格式化为可读性)


警惕(“我逃走了,因为你没逃走!”);

您只是连接字符串,而不是操作DOM,因此您仍然需要注意引号。

如果没有处理html,为什么会出现XSS风险?例如,如果我将文本输入值设置为“示例文本”,输入框按原样显示文本,而不是示例文本。如果不将编码指定为
htmlentities
,则可能存在风险。(为什么每个人都使用
htmlspecialchars
而不是
htmlspecialchars
?)
因为
htmlspecialchars($string)
htmlspecialties($string)
可能带来unicode风险。
Would it be better to sanitize the input like this?
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="" />