In php the best xss filter:
htmlspecialchars($_POST['param'],ENT_QUOTES);
The reason you also need to encode quotes is because you don't need to <> use some xss. for example, this is vulnerable to xss:
print('<A HREF="http://www.xssed.com/'.htmlspecialchars($_REQUEST[xss]).'">link</a>');
You do not need <> to execute javascript in this case, because you can use onmouseover, here is an example attack:
$_REQUEST[xss]='" onMouseOver="alert(/xss/)"';
ENT_QUOTES does double quotes.
Email is a bit different, javascript should not be executed by the email client, and if that is the case then your site will not be affected due to the same origin policy. But to be safe, I would still use htmlspecialchars($var,ENT_QUOTES); . HOWEVER, the PHP mail () function can succumb to another type of vulnerability called CRLF injection. Here is an example of a vulnerability from PHP-Nuke . If you have a function call as follows: mail($fmail, $subject, $message, $header); Then you need to make sure that the user cannot enter \r\n in the $ header.
Vulnerable Code:
$header="From: \"$_GET[name]\" <$ymail>\nX-Mailer: PHP";
fixed:
$_GET[name]=str_replace(array("\r","\n"),$_GET[name]); $header="From: \"$_GET[name]\" <$ymail>\nX-Mailer: PHP";
rook
source share