PHP:如何用 strip_tags($_POST[...]) 批量替换 $_POST[...]

发布于 2024-09-15 12:52:57 字数 243 浏览 9 评论 0原文

我目前正在从严重的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幽蝶幻影 2024-09-22 12:52:58

嗯,我认为 array_walk_recursive 会做这件事:

function custom_strip(&$val, $index) {
   $val = strip_tags($val);
}
array_walk_recursive($_POST, 'custom_strip');

Hmm, I think array_walk_recursive would do the trick:

function custom_strip(&$val, $index) {
   $val = strip_tags($val);
}
array_walk_recursive($_POST, 'custom_strip');
情独悲 2024-09-22 12:52:58

您可以将其放入文件中(例如 safe.php)

foreach ($_POST as $key => $value) {
  $_POST[$key] = is_array($key) ? $_POST[$key]: strip_tags($_POST[$key]);
}

,然后将 require_once("safe.php"); 放入您的每个 php 文件中(或所有 php 文件已包含的文件中) )
这是一个丑陋的黑客..但它可能会节省你的时间。

you can put this in a file (e.g safe.php)

foreach ($_POST as $key => $value) {
  $_POST[$key] = is_array($key) ? $_POST[$key]: strip_tags($_POST[$key]);
}

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.

2024-09-22 12:52:58

很简单。将其放在每个文件的顶部或放在一个公共头文件中,该文件每次都会在一开始就被调用:

function mystriptag(&$item)
{
    $item = strip_tags($item);
}

array_walk($_POST, mystriptag);

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:

function mystriptag(&$item)
{
    $item = strip_tags($item);
}

array_walk($_POST, mystriptag);
荒人说梦 2024-09-22 12:52:58

您只需 array_map strip_tags$ _POST,但最好编写一个自定义函数来从中获取数据:

function post_data($name) {
    global $post_cache;
    if (in_array($name, $post_cache)) {
        return $post_cache[$name];
    }
    $val = $_POST[$name];
    if (is_string($val)) {
        $val = strip_tags($val);
    } else if (is_array($val)) {
        $val = array_map('strip_tags', $val);
    }
    $post_cache[$name] = $val;
    return $val;
}

这将使您的代码更具可读性(其他研究它的人通常会假设 $_POST['foo'] 是表单字段 foo 中的数据,不是您已经预处理过的数据),不会导致尝试直接访问 $_POST 的插件或库出现问题,可以轻松添加更多逻辑到 $_POST 预处理(当魔法引号时取消转义启用是一种常见的方法),而无需查找代码中使用 POST 数据的所有位置,并且当您意识到有一些 POST 字段确实需要 HTML 标记时,可以让您免遭巨大的麻烦。一般来说,直接改变任何超全局变量都是一个非常糟糕的主意。

此外,最好在输出时清理数据,而不是在输入时清理数据。不同的用途需要不同的方法,例如,如果您使用

<div class="user_photo">
   <img src="<?php echo photo_path($user_id) ?>" alt="<?php echo $user_name ?>" />
</div>

$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:

function post_data($name) {
    global $post_cache;
    if (in_array($name, $post_cache)) {
        return $post_cache[$name];
    }
    $val = $_POST[$name];
    if (is_string($val)) {
        $val = strip_tags($val);
    } else if (is_array($val)) {
        $val = array_map('strip_tags', $val);
    }
    $post_cache[$name] = $val;
    return $val;
}

This will make your code more readable (others looking into it will generally assume that $_POST['foo'] is the data in form field foo, 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

<div class="user_photo">
   <img src="<?php echo photo_path($user_id) ?>" alt="<?php echo $user_name ?>" />
</div>

then $user_name is an XSS attack vector, and strip_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 against javascript: URLs and so on.

渔村楼浪 2024-09-22 12:52:57

只需使用 array_map() 即可。

$Clean = array_map('strip_tags', $_POST);

或者,如果您希望它返回到 $_POST 变量:

$_POST = array_map('strip_tags', $_POST);

使用不同的变量并将所有出现的 $_POST 更改为 可能是一个更好的主意文件中的 $Clean

Just use array_map().

$Clean = array_map('strip_tags', $_POST);

Or if you want it to go back to the $_POST variable:

$_POST = array_map('strip_tags', $_POST);

It's probably a better idea though to use a different variable and change all occurrence of $_POST to $Clean in your files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文