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.


In reply to Re: Is this safe?? by chromatic
in thread Is this safe?? by SilverB1rd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.