PHP:如何用 strip_tags($_POST[...]) 批量替换 $_POST[...]
我目前正在从严重的 XSS 攻击中恢复,并意识到我从未清理过网站上几个表单上的输入。我使用 Notepad++ 的“在文件中查找”功能在所有 PHP 文件中搜索 $_POST
,并获得了近 5,000 个结果。现在,我真的不想手动将 strip_tags
添加到每一个结果中,但是全部替换并不能解决问题......而且我完全是个菜鸟它涉及到正则表达式之类的东西。
有什么办法可以让这个过程变得不那么乏味吗?
I'm currently recovering from a nasty XSS attack, and realized I never sanitized inputs on several of the forms on my site. I used Notepad++'s Find In Files feature to search for $_POST
in all my PHP files, and got almost 5,000 results. Now, I really don't want to go and manually add strip_tags
to every one of those results, but a replace-all wouldn't do the trick... and I'm a total noob when it comes to things like regular expressions.
Is there any way to make this a little less tedious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯,我认为
array_walk_recursive
会做这件事:Hmm, I think
array_walk_recursive
would do the trick:您可以将其放入文件中(例如 safe.php)
,然后将
require_once("safe.php");
放入您的每个 php 文件中(或所有 php 文件已包含的文件中) )这是一个丑陋的黑客..但它可能会节省你的时间。
you can put this in a file (e.g safe.php)
Then put
require_once("safe.php");
in every each of your php files (or a file that all of your php file already included )It's an ugly hack.. but it may save your time.
很简单。将其放在每个文件的顶部或放在一个公共头文件中,该文件每次都会在一开始就被调用:
Very simple. Put this on top of your every file OR in a common header file which gets called in the very beginning every time:
您只需
array_map
strip_tags
到$ _POST
,但最好编写一个自定义函数来从中获取数据:这将使您的代码更具可读性(其他研究它的人通常会假设
$_POST['foo'] 是表单字段
foo
中的数据,不是您已经预处理过的数据),不会导致尝试直接访问 $_POST 的插件或库出现问题,可以轻松添加更多逻辑到$_POST
预处理(当魔法引号时取消转义启用是一种常见的方法),而无需查找代码中使用 POST 数据的所有位置,并且当您意识到有一些 POST 字段确实需要 HTML 标记时,可以让您免遭巨大的麻烦。一般来说,直接改变任何超全局变量都是一个非常糟糕的主意。此外,最好在输出时清理数据,而不是在输入时清理数据。不同的用途需要不同的方法,例如,如果您使用
$user_name
,那么它就是一个XSS攻击向量,而strip_tags
根本无法帮助抵御它;你需要htmlspecialchars。如果用户数据用作 URL,您将需要另一种方法来防御javascript:
URL 等。You could just
array_map
strip_tags
to$_POST
, but it is much nicer to write a custom function for obtaining data from it:This will make your code more readable (others looking into it will generally assume that
$_POST['foo']
is the data in form fieldfoo
, not somethin you have already preprocessed), won't cause you problems with plugins or libraries which try to access $_POST directly, makes it easy to add more logic to$_POST
preprocessing (unescape when magic quotes are enabled is a common one) without hunting down all the places in your code where you have used POST data, and saves you from huge headaches when you realize there are a few POST fields where you do need HTML tags. Generally, it is a really bad idea to directly change any of the superglobals.Also, it is better to sanitize data on output, not on input. Different uses will require different methods, for example, if you use
then
$user_name
is an XSS attack vector, andstrip_tags
does not help against it at all; you would need htmlspecialchars. If user data is used as an URL, you would need yet another method to defend againstjavascript:
URLs and so on.只需使用
array_map()
即可。或者,如果您希望它返回到
$_POST
变量:使用不同的变量并将所有出现的
$_POST
更改为可能是一个更好的主意文件中的 $Clean
。Just use
array_map()
.Or if you want it to go back to the
$_POST
variable:It's probably a better idea though to use a different variable and change all occurrence of
$_POST
to$Clean
in your files.