gpt4 book ai didi

php - 当文件尚不存在时防止类似 LFI 的漏洞

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:13 24 4
gpt4 key购买 nike

如果用户在服务上请求文件,我通常可以使用 PHP 的 realpath() 来保护他们不访问我想要的范围之外的文件。并确保它在根目录下。比如像这样:

$path = realpath($_GET['path']);
// Protect against LFI vulnerabilities
if (substr($path, 0, strlen($root)) == $root)
{
// safe
}

然而,realpath() 只适用于已经存在的文件。如果我想确保用户要将我的脚本写入的位置在根目录下怎么办?

我不能使用 realpath(),我应该只检查并删除“..”引用吗?或者有更好的方法吗?

最佳答案

如何检查 realpath(dirname($file)) 是否在根目录下?

$dir = $path;
$found = false;

while ($dir) {
$dir = dirname($dir);
if (!is_dir($dir)) {
continue;
}

if (strpos(realpath($dir), $root) === 0) {
$found = true;
}

break;
}

var_dump($found);

关于php - 当文件尚不存在时防止类似 LFI 的漏洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11655360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com