I have a small confusion. When I type on Google, almost all articles suggest Filter input, escape output. If I didn't confuse the terms escaping and filtering all my life, it should be the opposite.
You get loads of articles which does something like
$username = htmlentities(htmlspecialchars(strip_tags($_POST['username')));
and suggest doing it.
We should not filter input. We should escape it (previously we did it with mysql_real_
escape_string, nowadays prepared statements handle them for us.) We should insert user's submitted data to databaseas-is, without changing it using functions likehtmlspecialchars. We should always keep the original input in our database, sohtmlspecialcharsduring input is wrong. HTML is not harmful for database.We should filter output, so malicious code (html, js, whatever) won't run on the browser. This is called
XSS filtering, notXSS escaping. For example,{{{ $var }}}on Laravel 4 is called asXSS filteringand this should always be used on user submitted content's output.
If the statement Filter input escape output is correct, why it is not mysql_real_filter_string() and preventing XSS isn't being called as XSS escaping?
Also, ircmaxell once said:
Filtering is not about preventing security vulnerabilities, it's about not populating your database with garbage. If you're expecting a date, make sure it at least looks like a date prior to storing it.
This is called validation, and you can't rely on validation only. (Especially on older versions of PHP) You need to both escape and validate input. Filtering may not be used for security vulnerabilities but escaping is.
Well, this sums my confusion. Can someone explain this to me?