Page 1 of 1

XSS Filter - Please Critique

Posted: Fri Jan 27, 2012 11:35 am
by php3ch0
I have a function that I am using to hopefully make sure that data is safe.

Code: Select all

function xssfilter($data) {
	
	if(!get_magic_quotes_gpc()) {
		$data = addslashes($data);
		    }
			$data = strip_tags(htmlspecialchars($data));
			
	$data = mysql_real_escape_string($data);
	
	return $data;
	
}
I use it as such

$var = xssfilter($_POST['var']);

Does this look secure enough?

Re: XSS Filter - Please Critique

Posted: Fri Jan 27, 2012 1:04 pm
by tr0gd0rr
You are confusing XSS with SQL injection.

The simplest way to avoid XSS is to run htmlspecialchars() before echoing anything. I don't recommend putting anything in the database with htmlspecialchars already run. Wait until output. Some day you may want to send your database data via a web service or print a check. It looks pretty stupid if you print a check made out to "Mr. O'Reilly".

The exception would be rich text. If you are accepting rich text like a blog post, you should run it through a full HTML parser and cleaner such as HTMLPurifier before putting it in the database.

As far as SQL injection, before putting anything into the database always use PDO binding or mysql_real_escape_string(). And addslashes() is unnecessary. In fact, addslashes() is rarely useful.

Re: XSS Filter - Please Critique

Posted: Fri Jan 27, 2012 4:43 pm
by social_experiment
If you are going to use the code, get rid of the magic quotes checks and apply filtering regardless of it; magic_quotes_gpc has been deprecated and shouldn't be relied upon at all.

Re: XSS Filter - Please Critique

Posted: Fri Jan 27, 2012 4:45 pm
by Celauran
social_experiment wrote:If you are going to use the code, get rid of the magic quotes checks and apply filtering regardless of it; magic_quotes_gpc has been deprecated and shouldn't be relied upon at all.
Though at that point all you're left with is a mysql_real_escape_string wrapper function.

Re: XSS Filter - Please Critique

Posted: Sat Jan 28, 2012 2:22 am
by social_experiment
I should have probably worded that better :) I mean he can leave the checks for magic_quotes_gpc() out; since it's no longer useful but still keep the other filters in place. This strip_tags(htmlspecialchars($data)); is redundant as htmlspecialchars() will make sure strip_tags() find nothing to strip, i would change it like this:

Code: Select all

<?php
function xssfilter($data) {        
        $data = addslashes($data);
        #
        $data = strip_tags($data);
         #               
        $data = mysql_real_escape_string($data);
        #
        return $data;        
}
?>

Re: XSS Filter - Please Critique

Posted: Sun Jan 29, 2012 5:30 am
by php3ch0
Thanks Will do.

Re: XSS Filter - Please Critique

Posted: Mon Jan 30, 2012 9:59 am
by tr0gd0rr
@social_experiment, your function probably behaves different than you are thinking. If you pass the string "Jane's <3 is Joe", you will get out of the database "Jane\'s ". Note how strip_tags() cuts off strings with less than signs in the middle. But if you run mysql_real_escape_string() alone on the way in, and htmlspecialchars() before displaying to the browser, the string will look identical.

Re: XSS Filter - Please Critique

Posted: Mon Jan 30, 2012 3:27 pm
by social_experiment
Thanks for pointing that out tr0gd0rr :) I personally don't use strip_tags() [and it shows doesn't it]; if you catch " or ' (which mysql_real_escape_string() does) and use htmlspecialchars() when displaying data then strip_tags() becomes redundant

Re: XSS Filter - Please Critique

Posted: Wed Feb 01, 2012 6:50 am
by Mordred

Code: Select all

$data = htmlspecialchars($data, ENT_QUOTES, 'utf8');
None of the other listed options so far will work correctly:
strip_tags() is of dubious value and will damage the original data,
mysql_real_escape_string() is about MySQL and SQL injection, not XSS
addslashes() is just silly
htmlspecialchars($data) is insufficient protection when used in attribute context.

Re: XSS Filter - Please Critique

Posted: Wed Feb 01, 2012 9:26 pm
by tr0gd0rr
Mordred wrote:htmlspecialchars($data) is insufficient protection when used in attribute context.
What do you mean exactly? Because an attribute might contain some JavaScript? Or how is it insufficient?

Re: XSS Filter - Please Critique

Posted: Thu Feb 02, 2012 4:41 am
by Mordred
If it's an attribute and you use the 'wrong' type of quotes, plain htmlspecialchars() will fail to protect against XSS, read the docs. It's amazing how stupid some of the PHP functions are - it's a security measure which is insecure by default!

Re: XSS Filter - Please Critique

Posted: Thu Feb 02, 2012 4:57 am
by social_experiment
So a function such as htmlentities() with ENT_QUOTES and charset should be the default if you wish to properly safeguard against XSS?

Re: XSS Filter - Please Critique

Posted: Thu Feb 02, 2012 7:14 am
by Mordred
Mordred wrote:

Code: Select all

$data = htmlspecialchars($data, ENT_QUOTES, 'utf8');