作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如果用户在服务上请求文件,我通常可以使用 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/
如果用户在服务上请求文件,我通常可以使用 PHP 的 realpath() 来保护他们不访问我想要的范围之外的文件。并确保它在根目录下。比如像这样: $path = realpath($_GET['p
我想知道这个小片段是否受到本地文件注入(inject)漏洞的影响。
我是一名优秀的程序员,十分优秀!