mirror of
https://github.com/icecoder/ICEcoder.git
synced 2026-03-15 04:47:05 +01:00
Rewrite of xssClean function to be neater
This commit is contained in:
@@ -61,38 +61,40 @@ function numClean($var) {
|
||||
}
|
||||
|
||||
// Clean XSS attempts using different contexts
|
||||
function xssClean($data,$types) {
|
||||
function xssClean($data,$type) {
|
||||
|
||||
// 'html'
|
||||
if (strpos($types,"html")>-1) {
|
||||
$bad = array("<", ">", "=", "&", "(");
|
||||
$good = array("<", ">", "=", "&", "(");
|
||||
$data = str_replace($bad, $good, $data);
|
||||
// === html ===
|
||||
if ($type == "html") {
|
||||
$bad = array("<", ">", "=", "&", "(", ")", "\"", "'");
|
||||
$good = array("<", ">", "=", "&", "(", ")", """, "'");
|
||||
}
|
||||
|
||||
// 'style'
|
||||
if (strpos($types,"style")>-1) {
|
||||
$bad = array("\"", "``", "(", "&", ".", "\\");
|
||||
$good = array(""", "``", "(", "&", ".", "\");
|
||||
$data = str_replace($bad, $good, $data);
|
||||
// === style ===
|
||||
if ($type == "style") {
|
||||
$bad = array("\"", "``", "(", ")", "&", ".", "\\");
|
||||
$good = array(""", "``", "(", ")", "&", ".", "\");
|
||||
}
|
||||
|
||||
// 'tags'
|
||||
if (strpos($types,"tags")>-1) {
|
||||
$data = strip_tags($data);
|
||||
// === attribute ===
|
||||
if ($type == "attribute") {
|
||||
$bad = array("\"", "``");
|
||||
$good = array(""", "``");
|
||||
}
|
||||
|
||||
// 'multi'
|
||||
if (strpos($types,"multi")>-1) {
|
||||
$bad = array( '@<script[^>]*?>.*?</script>@si', // Strip out javascript
|
||||
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
|
||||
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly
|
||||
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments including CDATA
|
||||
);
|
||||
$good = "";
|
||||
$data = preg_replace($bad, $good, $data);
|
||||
// === script ===
|
||||
if ($type == "script") {
|
||||
$bad = array("<", ">", "(", ")", "[", "]", "\"", "'", ";",);
|
||||
$good = array("<", ">", "(", ")", "[", "]", """, "'", ";");
|
||||
}
|
||||
|
||||
// === url ===
|
||||
if ($type == "url") {
|
||||
$bad = array("\"", "``");
|
||||
$good = array("%22", "%60%60");
|
||||
}
|
||||
|
||||
$data = str_replace($bad, $good, $data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user