Tuesday 23 August 2016

php - The ultimate clean/secure function



I have a lot of user inputs from $_GET and $_POST... At the moment I always write mysql_real_escape_string($_GET['var'])..



I would like to know whether you could make a function that secures, escapes and cleans the $_GET/$_POST arrays right away, so you won't have to deal with it each time you are working with user inputs and such.




I was thinking of an function, e.g cleanMe($input), and inside it, it should do mysql_real_escape_string, htmlspecialchars, strip_tags, stripslashes (I think that would be all to make it clean & secure) and then return the $input.



So is this possible? Making a function that works for all $_GET and $_POST, so you would do only this:



$_GET  = cleanMe($_GET);
$_POST = cleanMe($_POST);


So in your code later, when you work with e.g $_GET['blabla'] or $_POST['haha'] , they are secured, stripped and so on?




Tried myself a little:



function cleanMe($input) {
$input = mysql_real_escape_string($input);
$input = htmlspecialchars($input, ENT_IGNORE, 'utf-8');
$input = strip_tags($input);
$input = stripslashes($input);
return $input;
}


Answer



The idea of a generic sanitation function is a broken concept.



There is one right sanitation method for every purpose. Running them all indiscriminately on a string will often break it - escaping a piece of HTML code for a SQL query will break it for use in a web page, and vice versa. Sanitation should be applied right before using the data:




  • before running a database query. The right sanitation method depends on the library you use; they are listed in How can I prevent SQL injection in PHP?


  • htmlspecialchars() for safe HTML output


  • preg_quote() for use in a regular expression


  • escapeshellarg() / escapeshellcmd() for use in an external command



  • etc. etc.




Using a "one size fits all" sanitation function is like using five kinds of highly toxic insecticide on a plant that can by definition only contain one kind of bug - only to find out that your plants are infested by a sixth kind, on which none of the insecticides work.



Always use that one right method, ideally straight before passing the data to the function. Never mix methods unless you need to.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...