From f786bb56f0f752fa7747e850619515620047dbaa Mon Sep 17 00:00:00 2001 From: SecretR Date: Mon, 9 Jun 2014 21:45:40 +0300 Subject: [PATCH] New parser is able to secure attribute values, attributes loop bugfix; e_parser made available for auto-loading --- e107_handlers/e107_class.php | 1 + e107_handlers/e_parse_class.php | 154 +++++++++++++++++++++----------- 2 files changed, 102 insertions(+), 53 deletions(-) diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php index ae4e34a59..619e91b44 100644 --- a/e107_handlers/e107_class.php +++ b/e107_handlers/e107_class.php @@ -170,6 +170,7 @@ class e107 'e_object' => '{e_HANDLER}model_class.php', 'e_online' => '{e_HANDLER}online_class.php', 'e_parse' => '{e_HANDLER}e_parse_class.php', + 'e_parser' => '{e_HANDLER}e_parse_class.php', 'e_parse_shortcode' => '{e_HANDLER}shortcode_handler.php', 'e_ranks' => '{e_HANDLER}e_ranks_class.php', 'e_shortcode' => '{e_HANDLER}shortcode_handler.php', diff --git a/e107_handlers/e_parse_class.php b/e107_handlers/e_parse_class.php index 0070c4d0e..1b9cca412 100644 --- a/e107_handlers/e_parse_class.php +++ b/e107_handlers/e_parse_class.php @@ -2537,25 +2537,34 @@ class e_parse extends e_parser */ class e_parser { - public $domObj = null; - private $removedList = array(); - private $nodesToDelete = array(); - private $nodesToConvert = array(); - private $pathList = array(); - private $allowedAttributes = array( + /** + * @var DOMDocument + */ + public $domObj = null; + protected $removedList = array(); + protected $nodesToDelete = array(); + protected $nodesToConvert = array(); + protected $pathList = array(); + protected $allowedAttributes = array( 'default' => array('id', 'style', 'class'), 'img' => array('id', 'src', 'style', 'class', 'alt', 'title', 'width', 'height'), 'a' => array('id', 'href', 'style', 'class', 'title', 'target'), 'script' => array('type', 'src', 'language'), 'iframe' => array('id', 'src', 'frameborder', 'class', 'width', 'height', 'style') - ); - private $badAttrValues = array("javascript[\s]*?:","alert\(","vbscript[\s]*?:","data:text\/html", "mhtml[\s]*?:", "data:[\s]*?image"); - private $allowedTags = array('html', 'body','div','a','img','table','tr', 'td', 'th', 'tbody', 'thead', 'colgroup', 'b', + ); + + protected $badAttrValues = array('javascript[\s]*?:','alert\(','vbscript[\s]*?:','data:text\/html', 'mhtml[\s]*?:', 'data:[\s]*?image'); + + protected $replaceAttrValues = array( + 'default' => array() + ); + + protected $allowedTags = array('html', 'body','div','a','img','table','tr', 'td', 'th', 'tbody', 'thead', 'colgroup', 'b', 'i', 'pre','code', 'strong', 'u', 'em','ul','li','img','h1','h2','h3','h4','h5','h6','p', 'div','pre','section','article', 'blockquote','hgroup','aside','figure','span', 'video', 'br', 'small', 'caption', 'noscript' ); - private $scriptTags = array('script','applet','iframe'); //allowed whem $pref['post_script'] is enabled. + protected $scriptTags = array('script','applet','iframe'); //allowed when $pref['post_script'] is enabled. protected $blockTags = array('pre','div','h1','h2','h3','h4','h5','h6','blockquote'); // element includes its own line-break. @@ -2586,7 +2595,6 @@ class e_parser $this->allowedTags = $array; } - /** * Set Allowed Attributes. * @param $array @@ -2596,8 +2604,15 @@ class e_parser $this->allowedAttributes = $array; } + /** + * Set Script Tags. + * @param $array + */ + public function setScriptTags($array=array()) + { + $this->scriptTags = $array; + } - /** * Add leading zeros to a number. eg. 3 might become 000003 * @param $num integer @@ -2959,6 +2974,7 @@ class e_parser { // $tp = e107::getParser(); $sql = e107::getDb(); + $tp = e107::getParser(); $html = $this->getXss(); @@ -2970,17 +2986,17 @@ class e_parser echo "

\$tp->dataFilter()

"; // echo $tp->dataFilter($html); // Remove Comment for a real mess! $sql->db_Mark_Time('------ Start Parser Test -------'); - print_a($this->dataFilter($html)); + print_a($tp->dataFilter($html)); $sql->db_Mark_Time('tp->dataFilter'); echo "

\$tp->toHtml()

"; // echo $tp->dataFilter($html); // Remove Comment for a real mess! - print_a($this->tohtml($html)); + print_a($tp->toHTML($html)); $sql->db_Mark_Time('tp->toHtml'); echo "

\$tp->toDB()

"; // echo $tp->dataFilter($html); // Remove Comment for a real mess! - print_a($this->toDB($html)); + print_a($tp->toDB($html)); $sql->db_Mark_Time('tp->toDB'); @@ -3005,13 +3021,15 @@ class e_parser /** - * Process and clean HTML from user input. - * @param $html raw HTML - * TODO Html5 tag support. + * Process and clean HTML from user input. + * TODO Html5 tag support. + * @param string $html raw HTML + * @param boolean $checkPref + * @return string */ - public function cleanHtml($html='') + public function cleanHtml($html='', $checkPref = true) { - if(!vartrue($html)){ return; } + if(empty($html)){ return; } // $html = mb_convert_encoding($html, 'UTF-8'); @@ -3031,17 +3049,20 @@ class e_parser { $this->init(); } - - $post_scripts = e107::getConfig()->get('post_script', e_UC_MAINADMIN); // Pref to Allow