An inbetween step would be to allow completely safe characters, disallow or escape completely unsafe characters, and for all others just delete them from the input. That's pretty close to the right thing to do for a lot of weird input characters.
You said in your post you don't understand what you're protecting against. Here's a little bit of the flavor of SQL injection attacks.
At the most fundamental level, you're trying to prevent a statement like this:
from becoming nasty if the user enters something like Scott; DELETE FROM table for their name.my $sql = "SELECT * FROM table WHERE NAME=$name"
So, you change that to:
That works for our simple case, but now the user can enter their name as Tom'; DELETE FROM table; SELECT * FROM table WHERE NAME='Bob, which will result in the SQL statement:my $sql = "SELECT * FROM table WHERE NAME='$name'";
SELECT * FROM table WHERE NAME='Tom'; DELETE FROM table; SELECT * FROM table WHERE NAME='Bob'
So now you have to escape quotes, which you can do with the $dbh->quote function, or by using placeholders as others have described.
Other characters that are dangerous to your database will depend on the database, but unless your DBD driver is really crappy, $dbh->quote and placeholders should both be safe.
The remaining dangers, then, depend on what you do with the data. If you're displaying it on a Web page, you want to make sure it doesn't contain HTML tags, particular JavaScript code. If you're using it to send an email, you want to make sure it doesn't have any characters special to the mailing program (for example sending a ~ to /bin/mail, the source of the security bug in setuidperl IIRC).
One school of thought says all data in the DB should be trustworthy, so you should make sure it doesn't have anything dangerous for any application. Another school of thought says put whatever you want in the DB, and the application using it is responsible for making sure it untaints it on the way out. You need to make sure that you treat data from the DB as tainted in this case. The most paranoid school of thought says you should do both---stop characters that are likely to be dangerous from getting into the DB, and applications using the data check to make sure the data really is safe. That last one is what I usually try to do.
In reply to Re: Common untainting methods?
by sgifford
in thread Common untainting methods?
by Wally Hartshorn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |