In a chatterbox discussion earlier this evening, Chrisf and myself were discussing potential security issues with CGI file upload scripts. After a bit of discussion involving the use of temporary files, the main points of which I have covered previously here, I put together the following block of code - Note that this code is not considered to be a complete example, but merely sufficient to highlight the elements of the CGI upload interface discussed.

#!/usr/bin/perl -Tw use CGI; use CGI::Carp qw/ fatalsToBrowser /; use Fcntl; use File::Basename; use POSIX; use strict; $CGI::POST_MAX = 1048576; # Maximal file upload size $CGI::DISABLE_UPLOADS = 0; my $config = { 'allowed_file_types' => [ 'png', 'gif', 'jpg' ], 'upload_fields' => [ 'image_file' ] }; my $cgi = CGI->new; foreach my $field ( @{ $config->{ 'upload_fields' } } ) { if ( defined $cgi->param( $field ) ) { my $fextension = ( fileparse( $cgi->param($field), '\..*' ) )[ +2]; if ( grep { lc( $_ ) eq lc( $fextension ) } @{ $config->{ 'all +owed_file_types' } } ) { my $fname; { local *FH; do { $fname = tmpnam(); } until sysopen ( FH, $fname, O_RDWR|O_CREAT|O_EXCL, 0 +666 ); my $buffer; while ( read( $cgi->param( $field ), $buffer, 1024 ) ) + { syswrite FH, $buffer, length( $buffer ); } close FH; } # continue on and do stuff with uploaded file - file res +ides in temporary directory with file name $fname } else { # bad file extension } } }

Now while any form of file system interaction via a CGI interface is going to come with a number of inherent security concerns, are there any other immediate concerns which should be addressed in a script similar to that above?

Any and all suggestions for the tightening and/or improvement of this code process, particularly from a security point of view, are welcomed.

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'


In reply to Security issues for CGI file upload by rob_au

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.