Validating allowed characters or validating disallowed characters
Posted
by
Tom
on Programmers
See other posts from Programmers
or by Tom
Published on 2012-04-04T11:06:56Z
Indexed on
2012/04/04
11:40 UTC
Read the original article
Hit count: 365
I've always validated my user input based on a list of valid/allowed characters, rather than a list of invalid/disallowed characters (or simply no validation). It's just a habit I picked up, probably on this site and I've never really questioned it until now.
It makes sense if you wish to, say, validate a phone number, or validate an area code, however recently I've realised I'm also validating input such as Bio Text fields, User Comments, etc. for which the input has no solid syntax.
The main advantage has always seemed to be: Validating allowed chars reduces the risk of you missing a potentially malicious character, but increases the risk the of you not allowing a character which the user may want to use. The former is more important.
But, providing I am correctly preventing SQL Injection (with prepared statements) and also escaping output, is there any need for this extra barrier of protection? It seems to me as if I am just allowing practically every character on the keyboard, and am forgetting to allow some common characters.
Is there an accepted practice for this situation? Or am I missing something obvious?
Thanks.
© Programmers or respective owner