svsingh has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
•Re: First Time Untainting Data
by merlyn (Sage) on Oct 10, 2003 at 15:32 UTC | |
by svsingh (Priest) on Oct 10, 2003 at 20:47 UTC | |
by merlyn (Sage) on Oct 11, 2003 at 15:23 UTC | |
by zigdon (Deacon) on Oct 13, 2003 at 15:21 UTC | |
Re: First Time Untainting Data
by Abigail-II (Bishop) on Oct 10, 2003 at 15:39 UTC | |
by svsingh (Priest) on Oct 10, 2003 at 17:05 UTC | |
by Abigail-II (Bishop) on Oct 10, 2003 at 17:10 UTC |
Back to
Seekers of Perl Wisdom