javascript 我应该同时使用 striptags() 和 htmlspecialchars() 来防止 XSS 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7232793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:24:42  来源:igfitidea点击:

Should I use both striptags() and htmlspecialchars() to prevent XSS?

phpjavascriptsecurityxss

提问by KRB

Does this depend on if the input is going to be printed to the user? In my case I need to return the input back to the user (comments and bio).

这是否取决于输入是否要打印给用户?在我的情况下,我需要将输入返回给用户(评论和生物)。

Thanks!!!

谢谢!!!

回答by Arnaud Le Blanc

htmlspecialchars()is enough to prevent XSS.

htmlspecialchars()足以防止XSS。

Strip tags removes tags but not special characters like "or ', so if you use strip_tags()you also have to use htmlspecialchars().

Strip 标签会删除标签,但不会删除像"or'这样的特殊字符,所以如果你使用,strip_tags()你也必须使用htmlspecialchars().

If you want users' comments to be displayed like they typed them, don't use strip_tags, use htmlspecialchars() only.

如果您希望用户的评论像他们输入的一样显示,不要使用 strip_tags,只使用 htmlspecialchars()。