Search code examples
perlsecuritycgi-bin

Perl webscript doubtful security


Recently with a project I inherited a simple perl script that generates a PDF of the page:

#!/usr/bin/perl
use CGI;

my $file="showdata.pdf";
my $filepath= "/tmp/$file";
system("wkhtmltopdf \"sample.com/showdata.php?".$ENV{"QUERY_STRING"}."\" $filepath");
print ("Content-Type:application/x-download\n");
print ("Content-Disposition: attachment; filename=$file\n\n");

open FILE, "< $filepath" or die "can't open : $!";
binmode FILE;
local $/ = \10240;
while (<FILE>){
    print $_;
}

close FILE;
unlink ($filepath);

I am concerned direct substitution variable $ENV{"QUERY_STRING"}. However, in a cursory testing, I did not reveal any problems. I was not able to create/delete files in a known writable directory. I tried not well or problems in the script should not be?


Solution

  • Accessing $ENV{"QUERY_STRING"} directly is insecure. In my case, my digital parameter 'o' must be forced to be integer. There is secure script version:

    #!/usr/bin/perl
    use CGI;
    my $query = new CGI;
    my $o = int($query->param('o'));
    my $file="showdata.pdf";
    my $filepath= "/tmp/$file";
    system("wkhtmltopdf \"sample.com/showdata.php?o=".$o."\" $filepath");
    print ("Content-Type:application/x-download\n");
    print ("Content-Disposition: attachment; filename=$file\n\n");
    
    open FILE, "< $filepath" or die "can't open : $!";
    binmode FILE;
    local $/ = \10240;
    while (<FILE>){
        print $_;
    }
    
    close FILE;
    unlink ($filepath);