|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Just because the form you wrote uses the POST method to submit data to your CGI script, for example, that doesnt mean that the data will come in that way. Rather than assuming that the data will be on standard in (stdin) where youre expecting it, you can check the REQUEST_METHOD environment variable to determine whether the GET or POST method was used and read the data accordingly. A truly well-written CGI script will accept data no matter what method was used to submit it and will be made more secure in the process. Listing 35.1 shows an example in Perl.
Listing 35.1 Cgi_read.plA Robust Reading Form Input # Takes the maximum length allowed as a parameter # Returns 1 and the raw form data, or 0 and the error text sub cgi_Read { local($input_Max) = 1024 unless $input_Max = $_[0]; local($input_Method) = $ENV{REQUEST_METHOD}; # Check for each possible REQUEST_METHODs if ($input_Method eq GET) { # GET local($input_Size) = length($ENV{QUERY_STRING}); # Check the size of the input return (0, Input too big) if ($input_Size > $input_Max); # Read the input from QUERY_STRING return (1,$ENV{QUERY_STRING}); } elsif ($input_Method eq POST) { # POST local($input_Size) = $ENV{CONTENT_LENGTH}; local($input_Data); # Check the size of the input return (0,Input too big) if ($input_Size > $input_Max); # Read the input from stdin return (0,Could not read STDIN) unless (read(STDIN,$input_Data,$input_Size)); return (1,$input_Data); } # Unrecognized METHOD return (0,METHOD not GET or POST); }
Dont Trust Path DataAnother type of data the user can alter is the PATH_INFO server environment variable. This variable is filled with any path information that follows the scripts filename in a CGI URL. For instance, if sample.sh is a CGI shell script, the URL http://www.yourserver.com/cgi-bin/sample.sh/extra/path/info will cause /extra/path/info to be placed in the PATH_INFO environment variable when sample.sh is run. If you use this PATH_INFO environment variable, you must be careful to completely validate its contents. Just as form data can be altered in any number of ways, so can PATH_INFOaccidentally or on purpose. A CGI script that blindly acts on the path file specified in PATH_INFO can enable malicious users to wreak havoc on the server. If a CGI script is designed to print out the file thats referenced in PATH_INFO, for instance, a user who edits the CGI URL can read almost any file on your computer, as in the following script: #!/bin/sh # Send the header echo Content-type: text/html echo # Wrap the file in some HTML echo <html><header><title>File</title></header><body> echo Here is the file you requested:<pre>\n cat $PATH_INFO echo </pre></body></html> Although this script works fine if the user is content to click only predefined linksfor example, http://www.yourserver.com/cgi-bin/showfile.sh/public/faq.txta more creative (or spiteful) user could use it to receive any file on your server. If she were to jump to
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |