rob_au has asked for the wisdom of the Perl Monks concerning the following question:
#!/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'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Security issues for CGI file upload
by Hero Zzyzzx (Curate) on Jan 18, 2002 at 22:38 UTC | |
by rob_au (Abbot) on Jan 19, 2002 at 04:52 UTC |