我正在用 PHP 实现一个简单的目录列表脚本。
我想确保在打开目录句柄并echo
随意获取结果之前传递的路径是安全的。
$f = $_GET["f"];
if(! $f) {
$f = "/";
}
// make sure $f is safe
$farr = explode("/",$f);
$unsafe = false;
foreach($farr as $farre) {
// protect against directory traversal
if(strpos($farre,"..") != false) {
$unsafe = true;
break;
}
if(end($farr) != $farre) {
// make sure no dots are present (except after the last slash in the file path)
if(strpos($farre,".") != false) {
$unsafe = true;
break;
}
}
}
这足以确保用户发送的路径是安全的,还是我应该做其他事情来防止攻击?