This looks strange to me:

$file =~ m/^.*(\\|\/)(.*)/; my $file_name = $2; open (FILE, "$name") or die "unable to open file";

Where is $name delcared? Oh, and the quotes around "$name" are unnecessary.

Also, what happens if somebody submits a filename like '>blah.txt'? Oops, you just deleted the contents of the file. As of perl 5.6, you can use a three-argument form of open() to avoid this problem:

open(FILE, '<', $name) or die "Can't open file: $!\n";

Better still is to do some further cleansing on the name of the file. I see you at least have some basic matching, but even better is to restrict exactly what files are allowed to be submitted via a hash:

my %allowed_files = ( file1 => '/path/to/file1', file2 => '/path/to/file2', file3 => '/path/to/file3', ); # $file already processed from CGI params my $file_name = defined($allowed_files{$file}) ? $allowed_files{$file} : die "Bad file name\n";

Admittedly, the above might become a maintence nightmare, depending on the number of files on your allowed list and if that list changes a lot.


In reply to Re: CGI file very odd by hardburn
in thread CGI file very odd by Anonymous Monk

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.