I'm writing an application that keeps track of contact information for my cousins. I just finished the part that updates the data file based on input into a form.
I'd like to ensure my data isn't tainted and the script isn't exploitable. After reading Untainting Safely and Ovid's CGI Tutorial, I came up with the code at the end of this posting.
If it's not too much touble, can I get some comments on my first attempt at untainting data. Am I missing anything or doing anything risky?
Thanks for your help.
#!/usr/local/bin/perl -wT
.
.
.
sub saveChanges {
sub untaint {
# remove any characters except those listed in regex
+
my $s = $_[0];
$s =~ s/[^\w \-\@\(\)\,\.\/]//g; # untaint the data
return $s;
}
# only put valid keys in the hash
my %c = ();
my @keys = qw(name birthday email phone cell addr1 addr2 ICQ AIM MS
+N Y!);
foreach my $i (@keys) {
if ( defined param("$i") && !param("$i") =~ /^\s*$/ ) {
$c{$i} = untaint( param("$i") );
}
}
# update datafile
my $tmpFile = $$config{"data"} . ".tmp";
open (TFILE, ">$tmpFile") or die "Can't open file: $!\n";
flock (TFILE, 2) or die "Can't lock file: $!\n";
my $cousins = &readData(); # read the current datafile
foreach my $cousin ( keys %$cousins ) {
if ( $cousin eq $c{"name"} ) { $$cousins{"name"} = \%c; }
print TFILE "\nname :: $cousin\n";
foreach my $field ( keys %{$$cousins{$cousin}} ) {
print TFILE "$field :: $$cousins{$cousin}{$field}\n";
}
}
flock(TFILE, 8);
close(TFILE);
rename( $tmpFile, $$config{"data"} );
}
Update: Added first line of the script to the code block. I'm assuming the -T argument runs the script in taint mode.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|