Questions:
What are the best functions safe1 (), safe2 (), safe3 () and safe4 () to avoid XSS for UTF8 encoded pages? Is it also safe in all browsers (in particular, IE6)?
<body><?php echo safe1($xss)?></body> <body id="<?php echo safe2($xss)?>"></body> <script type="text/javascript"> var a = "<?php echo safe3($xss)?>"; </script> <style type="text/css"> .myclass {width:<?php echo safe4($xss)?>} </style>
.
Many say that the best thing you can do is:
// safe1 & safe2 $s = htmlentities($s, ENT_QUOTES, "UTF-8"); // But how would you compare the above to: // https://github.com/shadowhand/purifier // OR http://kohanaframework.org/3.0/guide/api/Security
.
// safe3 $s = mb_convert_encoding($s, "UTF-8", "UTF-8"); $s = htmlentities($s, ENT_QUOTES, "UTF-8"); // How would you compare this to using using mysql_real_escape_string($s)? // (Yes, I know this is a DB function) // Some other people also recommend calling json_encode() before passing to htmlentities // What the best solution?
.
There are many posts about PHP and XSS. Most simply say "use HTMLPurifier" or "use htmlspecialchars" or are mistaken. Others say they use OWASP - but it is EXTREMELY slow. Some of the good posts I've come across are listed below:
Do htmlspecialchars and mysql_real_escape_string provide PHP code from injection?
XSS Me Warnings - Real XSS Issues?
CodeIgniter - why use xss_clean
user324289
source share