in reply to Filter apache web get request using cgi-perl
Technically, this is an Apache question, and not a Perl one. But if you must do it with Perl, here's something to get you started:
#!/usr/bin/perl -wT use CGI; use File::MimeInfo qw( mimetype ); use CGI::Carp qw( fatalsToBrowser ); #Of course, you'll want a more el +egant method # of error reporting than I use h +ere use constant FILES_DIR => "/path/to/files/"; my $cgi = new CGI; #Get the requested filename my $wanted_file = $cgi->param( "file" ); #my $wanted_file = $cgi->keywords; #Check if data is tainted - VERY IMPORTANT unless( $wanted_file =~ /\w{1}[\w-.]*\@[\w-.]+/ ) { #User tried to pass tainted data, e.g. "/etc/passwd" die( "Nice try, buddy." ); } #Check if that actually exists unless( -f FILES_DIR.$wanted_file" ) { die( "That file cannot be found." ); } #Check if the user is allowed to see that file #NOTE: Using cookies to store user id's is a bad idea. You # should use a session or a token of some sort. my $given_userid = $cgi->cookie( "user_id" ); my @allowed_people = get_an_array_of_allowed_people_from_somewhere(); unless( grep($given_userid, @allowed_userid) ) { die( "You are not allowed to see this file." ); } #Since we got here, we can give the user their file my $content_type = mimetype( FILES_DIR.$wanted_file ); open( FILE, "<", FILES_DIR.$wanted_file ) or die( "Cannot open file: $ +!" ); print $cgi->header( -type => $content_type ); print while <FILE>; close FILE;
|
|---|