I've hacked together a couple of quick functions to handle untainting data in a cgi script. As I'm still fairly new to CGI, I was wondering if the fellow monks could offer some constructive criticism, specifically, are there any security problems with this (assuming I pass a proper regex)?
sub badinputerror ($) {
my $q = shift;
print $q->header('text/plain');
print <<"EOHTML";
There was an error with
your input. Please try again.
EOHTML
die "input did not pass taint checking\n";
}
sub untaint ($$$) {
my $q = shift;
my $name = shift;
my $re = shift;
my $tainted = $q->param($name);
my $untainted = undef;
$untainted = $1 if($tainted =~ m/^($re)$/);
badinputerror($q) unless($untainted);
return $untainted;
}
#
# And later on in the code, for example
#
my $username=untaint($q, 'user', "[a-zA-Z][a-zA-Z0-9_]+");
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.