xss.php
<?php
header('Content-Type:text/html; charset=UTF-8');
var_dump(ini_get('filter.default'));
if (isset($_GET['name'])) {
echo $_GET['name'];
exit();
}
browsing http://localhost/xss.php?name=%3Cscript%3Ealert('XSS')%3C/script%3E
output:
string(10) "unsafe_raw"
I was under impression that this would be safe against XSS vulnerabilities because of filter extension, but it is not! It outputs a javascript alert dialog. My questions are:
- Why is the default filter unsafe_raw. I read that unsafe_raw does not protect against XSS?
- How can I protect PHP against this vulnerability. I could modify my
php.ini
, but I would like to do it with at runtime butini_set
,.htaccess
aren't working by default on my Ubuntu box. I want to have it affect at runtime so that all php instances(when deployed on other machines) are safe. Is that possible, or do I really need to sprinkle my code withfilter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_CHARS);
to make it safe.
P.S: This code is safe and also not looking that bad, but if I could set it up on runtime it would be nicer.
<?php
header('Content-Type:text/html; charset=UTF-8');
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
if (isset($_GET['name'])) {
echo $_GET['name'];
exit();
}