Skip to content

Commit

Permalink
ADD XSS attacks filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sulaiman0dawod committed Jan 31, 2023
1 parent 1309f46 commit 8c7d596
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions includes/pbboardCodeparse.class.php
Expand Up @@ -637,7 +637,7 @@ function censor_words($text)
$text = str_ireplace('rel="nofollow" href="#', 'href="#', $text);
$text = str_ireplace('rel="dofollow" href="#', 'href="#', $text);
eval($PowerBB->functions->get_fetch_hooks('BBCodeParseHooks3'));

// $text = strip_tags($text);
$censorwords = preg_split('#[ \r\n\t]+#', $PowerBB->_CONF['info_row']['censorwords'], -1, PREG_SPLIT_NO_EMPTY);
$text = str_ireplace($censorwords,'**', $text);
$blankasciistrip ="160 173 u8205 u8204 u8237 u8238";
Expand Down Expand Up @@ -760,7 +760,8 @@ function censor_words($text)
$text = str_replace(' rel="dofollow" ', ' rel="dofollow" ', $text);

$text = str_replace("<br>", "<br />", $text);

//XSS filtering function
$text = $this->xss_clean($text);
eval($PowerBB->functions->get_fetch_hooks('BBCodeParseHooks_cr'));


Expand Down Expand Up @@ -818,6 +819,44 @@ function censor_words($text)

return $this->replace($text."n-l-2-b-r");
}

//XSS filtering function
function xss_clean($data)
{
// Fix &entity\n;
$data = str_replace(array('&amp;','&lt;','&gt;'), array('&amp;amp;','&amp;lt;','&amp;gt;'), $data);
$data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
$data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
$data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');

// Remove any attribute starting with "on" or xmlns
$data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);

// Remove javascript: and vbscript: protocols
$data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);

// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);

// Remove namespaced elements (we do not need them)
$data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);

do
{
// Remove really unwanted tags
$old_data = $data;
$data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
}
while ($old_data !== $data);

// we are done...
return $data;
}

// long URL, Shortening Long URLs With PHP
function shortenurl($Aurl,$Burl,$lg_max)
{
Expand Down Expand Up @@ -1553,7 +1592,6 @@ function replace_htmlentities($string)
$string = preg_replace_callback($li_not, function($lidd) {
return $this->li_not_bar($lidd[1]);
}, $string);

return $string;
}

Expand Down

0 comments on commit 8c7d596

Please sign in to comment.