Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

untainting unicode text using \w

by danmcb (Monk)
on Aug 24, 2007 at 20:44 UTC ( [id://634960]=perlquestion: print w/replies, xml ) Need Help??

danmcb has asked for the wisdom of the Perl Monks concerning the following question:

I searched for something like this, but I don't see any previous questions on the subject. This is not so much a question, as a request for the input of monks who may have already been here.

My problem is - how to launder CGI parameters as being "printable" when they are not limited to ASCII, or even ISO-8559-1. They could be in Mandarin, or Hindi, say.

Now, I'm reading the regex docs, and \w looks like the answer, or part of it. To quote from perlrecharclass.pod : "If it's in UTF-8 format, \w matches those characters that are considered word characters in the Unicode database. That is, it not only matches ASCII letters, but also Thai letters, Greek letters, etc."

So I guess my untaint regex, to allow a user to send a note by email in any language but not do evil things, should be something like:

$p =~ /^[\w\s\.,]+$/;

That is - any "word" character, any whitespace, full stops and commas.

Does that seem safe? Let's say that this string will get piped to sendmail.

And more to the point, is this perhaps an Untaint module that should be on CPAN but isn't? This must be a common requirement these days.

Replies are listed 'Best First'.
Re: untainting unicode text using \w
by Joost (Canon) on Aug 24, 2007 at 22:19 UTC
    Wouldn't it be better to encode the mail body using base64 or quoted printable? That way you don't need to worry about what's in the body at all.

    In general, if you can't strictly validate input (i.e. match it against known-good data), it's better to make the process completely indifferent to the input. In the same way that using placeholders with DBI is better than grepping on (un)safe characters.

      ah! yes, it probably would. I have to admit that although I'd heard of these encodings, I have never checked out what they actually are, nor had it occurred to me that this might be a useful side effect.

      Thanks, Joost!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://634960]
Approved by randyk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-25 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found