deadnancy has asked for the wisdom of the Perl Monks concerning the following question:
But these (for a true apostrophe) fail completely:$_value =~ s/>/>/g; $_value =~ s/</</g;
Documentation for CGI.pm says that "by default, all HTML that are emitted by the form-generating functions are passed through a function called escapeHTML()." I'm seeing nothing being escaped, tho, and I don't believe I've changed any defaults:$_value =~ s/’/’/g; # typed from keyboard $_value =~ s/%92/’/g; # uri encoding $_value =~ s/’/’/g; # should never work
Same documentation also advises that "if you manually change the charset, either by calling the charset() method explicitly or by passing a -charset argument to header(), then all characters will be replaced by their numeric entities, since CGI.pm has no lookup table for all the possible encodings." (Emphasis is mine.)our %_form; our $_value; our $_query = CGI->new(); my @_field_names = $_query->param; foreach (@_field_names) { $_value = $_query->param($_); # convert nasty and/or special chars to html codes $_form{$_} = $_value; }
Is this decent code? Is there some way to compress all those s// statements? (There are many of them, but input here is an artist's statement, and we can be creative.) Am I overlooking something horribly obvious? The if->then statement drops everything after a character it doesn't like, but that's handy for finding the point of trouble. Still, seems like it could be handled better.foreach (@_field_names) { $_value = $_query->param($_); # convert special chars to html codes $_value =~ s/\x91/‘/g; # smart quotes $_value =~ s/\x92/’/g; $_value =~ s/\x93/“/g; $_value =~ s/\x94/”/g; $_value =~ s/\x96/–/g; # dashes $_value =~ s/\x97/—/g; $_value =~ s/\x7C/|/g; # pipe $_value =~ s/</</g; # brackets $_value =~ s/>/>/g; $_value =~ s/{/{/g; $_value =~ s/}/}/g; # only allow the known good if ($_value =~ /([\w\s\.\@\&\ \!\'\"\-\,\/\#\:\;\(\)]+)/) { $_value = $1; } else { die("(Friendly error message)"); } $_form{$_} = $_value; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: apostrophes and security
by rinceWind (Monsignor) on Aug 15, 2005 at 12:14 UTC | |
|
Re: apostrophes and security
by ysth (Canon) on Aug 15, 2005 at 12:58 UTC | |
by deadnancy (Novice) on Aug 15, 2005 at 19:31 UTC | |
|
Re: apostrophes and security
by Roger (Parson) on Aug 15, 2005 at 12:23 UTC | |
|
Re: apostrophes and security
by trammell (Priest) on Aug 15, 2005 at 15:15 UTC | |
by deadnancy (Novice) on Aug 16, 2005 at 04:27 UTC | |
|
Re: apostrophes and security
by wfsp (Abbot) on Aug 18, 2005 at 11:42 UTC |