It even depends on the type of input-forms you have. I have several functions called _POST($str) and _GET($str) which do some checks. I work plenty with regular expressions and never use $_GET[] outside this special functions. If I want to get an id of the adressbar, I only allow 0-9 by regular expression and a strlen of, lets say, 10. I also got get-functions for strings and for strings and numbers together. The same goes for post. Numeric only, string only, numeric and string, array only (and there, each entry is also checked).
The big problem comes along with input-types with no maxlength, a forum-post, userprofile-description, private message, whatever.
I think filtering special words is useless as some attack-vectors in the cheatsheet show. htmlspecialchars, htmlentities (don't forget the parameter ENT_QUOTES) are doing a great job here. I'm not sure if it's possible to bypass these functions with some special charsets or whatever, but I think they are safe (or am I wrong?).
You have to consider all input as evil. Don't forget that every form may be rewritten by a user to exploit your form-evaluating script (the form action). So a dropdown-box could easily be converted to a normal input box and allow the user to insert values you don't expect.
By using my own _POST and _GET-functions, I even think twice what kind of data I will accept.
Maybe a small code example for you:
function is_number($str) {
return (preg_match("/^([0-9])*$/", $str));
}
function _GETNUM($str, $digits = 12) {
if (!isset($_GET[$str])) {
return "";
}
if (strlen($_GET[$str]) <= $digits && is_number($_GET[$str]))
return $_GET[$str];
return "";
}
Don't forget to disable error-messages at your hosted website. You don't need them and they give hints for attackers.
Edited 2 time(s). Last edit at 01/10/2007 05:24AM by ckore.