Ovid has asked for the wisdom of the Perl Monks concerning the following question:
Well, I better get off my duff and get this done. I have a module, CGI::Safe, that currently makes the CGI environment a bit safer by deleting certain environment variables, disabling uploads, setting max post size, etc. Current syntax is like this:
use CGI::Safe qw/ taint /; my $q = CGI::Safe->new;
This is a subclass of CGI.pm, so you can use it as both objects or functions. It's pretty much the same thing.
Having 'taint' in the import list is currently a no-op. However, in future versions, this is intended to allow most CGI scripts to run unchanged. People can specify 'taint' and allow tainted variables to be returned:
use CGI::Safe qw/ :standard taint /; my $var = param( 'var' ) || ''; ( $var ) = ( $var =~ /^([\s\w\d]+)$/ );
Without 'taint' being specified, CGI::Safe is intended to not directly return untainted data. Default "tainted" values such as undef or an empty string will be returned, instead. However, I am not sure of how to specify the untainting regexes. Perhaps I could use Untaint or CGI::Untaint for this functionality. What do you think would be a clean, easy-to-use syntax for this?
use CGI::Safe; my $q = CGI::Safe->new; # set default tainted return to empty string $q->default_tainted( '' ); # assign the regex $q->untaint( foo => qr/^([\w\s\d]+)$/ ); # will return an empty string if it doesn't untaint my $foo = $q->param( 'foo' ); if ( ! $foo ) { error_routine( $q->tainted_param( 'foo' ) ); }
Alternatively (since no implementation of CGI::param seems to take a hashref):
my $foo = param( { foo => qr/^([\w\s\d]+)$/ } );
Of course, I'd also want to provide this for cookies, but this is just a start.
What do you think is clean? What would you like to see here? Any thoughts on implementation pitfalls that I should be aware of (other than users supplying bad regexes)? I also would prefer more concise methods than "default_tainted" and "tainted_param", but I would also prefer for this to be self-documenting.
Random thoughts:
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: CGI::Safe untaint syntax
by footpad (Abbot) on Jan 11, 2002 at 11:31 UTC | |
by tye (Sage) on Jan 12, 2002 at 04:30 UTC | |
|
(crazyinsomniac) Re: CGI::Safe untaint syntax
by crazyinsomniac (Prior) on Jan 11, 2002 at 11:23 UTC |