验证用户提供的相对文件系统路径



我想允许用户浏览我的 Web 服务器文件系统的一部分,确保他们无法访问父目录。此部分是动态的,因此无法定义。

我相信我最好的选择是将用户提供的相对路径附加到常量基本路径上,即:

use Cwd;
use CGI qw/:standard/;
use File::Spec::Functions;
my $base_path = catfile( getcwd, 'base' );
my $rel_path  = param('rel_path');
my $full_path = catfile( $base_path, $rel_path );
print $full_path;

我必须在用户提供的相对路径/完整路径上执行哪些验证/清理。我不能 100% 确定用户操纵相对路径并破坏系统和应用程序完整性的不同方式。

谢谢

克里斯

只需检查 $full_path 前面是否有 $base_path。

unless ( $full_path =~ m{^$base_path} ) { $full_path = $base_path; }

或者明确禁止 $rel_path 中的双点:

$rel_path = '' if $rel_path =~ /../;

最新更新