在 PHP 站点中避免 xss 攻击的最佳实践是什么 [英] What are the best practices for avoiding xss attacks in a PHP site
问题描述
我对 PHP 进行了配置,以便打开魔术引号并关闭注册全局变量.
I have PHP configured so that magic quotes are on and register globals are off.
对于我输出的任何源自用户输入的内容,我都会尽我最大的努力始终调用 htmlentities().
I do my best to always call htmlentities() for anything I am outputing that is derived from user input.
我偶尔也会在我的数据库中搜索在 xss 附件中使用的常用内容,例如...
I also occasionally seach my database for common things used in xss attached such as...
<script
我还应该做什么以及我如何确保我正在尝试做的事情总是完成.
What else should I be doing and how can I make sure that the things I am trying to do are always done.
推荐答案
转义输入并不是成功预防 XSS 的最佳方法.还必须对输出进行转义.如果您使用 Smarty 模板引擎,您可以使用 |escape:'htmlall'
修饰符将所有敏感字符转换为 HTML 实体(我使用自己的 |e
修饰符,它是以上).
Escaping input is not the best you can do for successful XSS prevention. Also output must be escaped. If you use Smarty template engine, you may use |escape:'htmlall'
modifier to convert all sensitive characters to HTML entities (I use own |e
modifier which is alias to the above).
我的输入/输出安全方法是:
My approach to input/output security is:
- 存储未修改的用户输入(输入时没有 HTML 转义,仅通过 PDO 准备好的语句完成 DB-aware 转义)
- 在输出时进行转义,具体取决于您使用的输出格式(例如 HTML 和 JSON 需要不同的转义规则)
这篇关于在 PHP 站点中避免 xss 攻击的最佳实践是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!