在使用白名单执行文件之前清理包含文件

时间:2012-04-10 14:57:53

标签: php include sanitization whitelist

是否可以在执行包之前对其进行清理,以确保它在服务器上存在?

我想避免攻击者使用某种白名单来破坏文件路径,这可能吗?

我的包含如下:

require_once('../includes/front/header.php');

3 个答案:

答案 0 :(得分:3)

道路怎么会受到损害? (除非您的require_once包含用户输入 - 请避免这种情况!)

您可以使用file_exists检查文件是否存在:

例如

if(file_exists('../includes/front/header.php')) {
  require_once('../includes/front/headers.php');
}

如果您确实需要白名单,但可以创建array 允许的路径/文件名,然后使用in_array检查其有效性。

答案 1 :(得分:1)

应该可以使用PHP的file_exists功能和白名单:

$allowed_files = array('../includes/front/header.php','../includes/front/footer.php');
$include_file = <string with file/path>; //EG '../includes/front/header.php'

if (in_array($include_file,$allowed_files && file_exists($include_file)) {
    require_once($include_file);
}

如果您的路径被硬编码到PHP脚本中而不接受文件名的用户输入,那么您不需要清理自己的文件名/路径

答案 2 :(得分:0)

你可以写你自己的包含,它可以根据'允许'路径列表进行验证。

即。使用上面的内容,但将其包装在一个函数中。