perl cgi解压缩上传的文件

问题描述:

我正在使用一个Web服务器,用户可以上传一个zip文件。上传后,应该解压缩文件。我尝试了系统命令unzip以及Archive::Extract模块。我仍然得到错误:perl cgi解压缩上传的文件

Insecure dependency in eval while running with -T switch at /usr/lib/perl5/5.10.0/Module/Load/Conditional.pm line 332, <GEN3> line 34 

请帮助。

+2

而你的代码是? – marto

+0

尝试归档:: Zip –

我猜你正在做的事情一样

system "unzip $value_from_browser"; 

所以整个事情是shell代码注入的邀请(文件名中的浏览器 “bla.zip; RM -fr *”)

使用multiargument语法,避免了壳:

system 'unzip', $value_from_browser; 

一个更安全的办法可能是使用Archive::zip,这里是我的一些工作的代码,它应该给你如何做到这一点的要点。 Archive::zip模块也有一些信息。

sub unpack_zipfiles 
{ 
my $zipfile = shift; # Name of uploaded zip file 
my $zip = Archive::Zip->new(); 
my $status = $zip->read($zipfile); 
if ($status == AZ_OK) 
    { 
    my @members = $zip->memberNames(); 
    my $n = 1; 
    foreach my $f (@members) 
     { 
     my @f2 = split(/\//,$f); 
# Part 1 - get a filename, and save the file 
     my $zf = getNextFile($f2[$#f2]); 
     my $unzipped = qq{$unpack_dir/$zf}; 
     $zip->extractMemberWithoutPaths($f,$unzipped); 
# Part 2 - check if it's new or the same, and adjust the filename if necessary 
     $zf = check_or_revert($unpack_dir,$zf,$f2[$#f2]); 
     $n++; 
     push @msgs,qq{Unzipped file: $zf}; 
# Add file to list of unpacked file (for diagnostic purposes) 
     push @{$data{unpacked}},$unzipped; 
# Unzipped files are plonked back in the file queue, because there is a loop that deals with them. 
     push @{$data{fileq}},$unzipped; 
     } 
    } 
else 
    { 
    die "Error: $zipfile is not a valid zip file, Zip status=$status"; 
    } 
}