Superlman has asked for the wisdom of the Perl Monks concerning the following question:

I have a script which, given the name of an image, sends the corresponding image to the browser. It would be used in HTML like this:

(Blah blah HTML) <IMG SRC = "http://www.server.com/cgi-bin/image.pl?iwant=5 (blah blah HTML)

And this would then produce whatever image the server things should be produced for the number "5". My current method of doing things is as follows:

#!/usr/bin/perl use POSIX; my $path = "/path/to/images"; my $imagereq = param('iwant'); #If you don't give an image, it assumes you #want image 0, the defaut image. unless($imagereq) { $imagereq = 0; } #If the image you requested exists (all are JPEG), then #we set our path to reflect this if(-e "$path/$imagereq".".jpg") { $path = "$path/$imagereq".".jpg"; } #Otherwise, we give them an special image else { $path = "$path"."404.jpg"; } #Open the image and 'print' it, which the browser #is quite happy to accept and display as an image open IMAGE, "$path" or die "ACK! $!"; print "Content-type: image/jpeg\n\n"; binmode STDOUT; while(<IMAGE>) { print STDOUT $_; }

Now, this works just fine. However, I would greatly appreciate any suggestions others may have, either in improving the current code or suggesting a whole other approach.

Some points:

  1. Is tainting going to be an issue here? All images are numeric (i.e. 1.jpg, 11234.jpg, etc), and since the ".jpg" extension is hardcoded, people shouldnt' be able to use this to try to peek at things they shouldn't be able to, right?
  2. Actually opening an image and printing it like this seems like a rather brain-dead way of going about it. Is there a better way?
  3. No, I cannot just set the HTML tag to point to a static location and have the image served that way.
  4. Is there anything else to consider?

Replies are listed 'Best First'.
Re: 'Printing' an Image to a Browser
by Aristotle (Chancellor) on Aug 11, 2002 at 04:22 UTC
    First thing: warnings and strict of course. You seem to have missed the use CGI; on pasting. There are a bunch of places you could write more idiomatic Perl. use POSIX; is redundant, as is the STDOUT in print STDOUT (unless you've used select to change the default handle..). I propose use CGI::Carp; to have nicer webserver errorlogs. Since you don't check your iwant in the current form, someone might pass something like ?iwant=../../../../../../etc/passwd%00 to have your /etc/passwd printed back to them, or since you're using the two-parameter form of open() without specifically using a "<", they might give you ?iwant=>0 to clobber 0.jpg (or combine it with a directory traversal to clobber other files on your box that your script can write to). So check that the parameter only contains what you expected and specify an explicit opening mode for your file, which I prefer to do using the absolutely unambiguous three-argument form of open(). Btw, writing "$path" is functionaly the same as $path; you're not writing a shell script here..
    #!/usr/bin/perl -w use strict; use CGI qw(:standard); use CGI::Carp; my $path = "/path/to/images"; my $imagereq = param('iwant'); $imagereq ||= 0; # defaults to image 0 my $image_path = "$path/$imagereq.jpg"; # check there are digits only in the request and that the file exists $image_path = "$path/404.jpg" unless $imagereq =~ /^\d+$/ and -e $imag +e_path; open(IMAGE, "<", $image_path) or die "ACK! $!"; print "Content-type: image/jpeg\n\n"; binmode STDOUT; $/ = \8192; # read data in 8kbyte chunks rather than from LF to LF # see perldoc perlvar print while <IMAGE>;

    It would probably be cleaner to use File::Spec::Functions to do the path concatenations. Of course, if the files are located inside your DocumentRoot, rather than sending the file you could just send a Location: redirect.

    HTH :-)

    Update Aug 12, 2002 (including minor code fix): silly twit, I am. Of course, if $image_path is "$path/$imagereq.jpg", passing ?iwant=>0 to the script will not clobber anything, and simply lead to the -e test failing harmlessly. I'm surprised noone called me on it. Using the three-argument open() is generally recommended anyway; simply a matter of defensive coding.

    Makeshifts last the longest.

•Re: 'Printing' an Image to a Browser
by merlyn (Sage) on Aug 11, 2002 at 10:48 UTC