http://qs1969.pair.com?node_id=56770


in reply to Is this safe??

On first glance, probably. On the other hand, are you willing to take the chance? Who knows what shell metacharacters may be used in the future? I'd rather deny everything I don't explicitly allow than allow everything I don't explicitly deny.

On the other hand, I would say it's fairly unlikely you'll have a query string AND posted data at the same time, for a normal operation. So it may be a moot point, as I don't expect this script to work very well.

I would recommend you read perlsec and enable Taint mode. Also enable warnings with perl -w and use the strict module. You should also use CGI for your form handling (see my home node or Ovid's CGI course for details).

If you want to be more paranoid, I would build up a hash of *allowed* files, then check the input against them:

my %files = ( '/var/www/index.html' => 1, '/var/www/home/index.html' => 1; ); my $request = $query->param('fulltxtpath'); if (exists($files{$request})) { open (FILE, "> " . $request) or dienice("couldn't open $request: $ +!"); # open and print file } else { # error }
Better yet, use opendir and readdir to build a list of allowed files. You might even use the keys of the hash as their values, and use $files{$request} in the open statement.

Finally, I'd replace the looping substitutions with a transliteration:

$var1 =~ tr/a-zA-Z0-9\/\.\-_//dc;

That's reasonably strict.