From da4ffc2a0cae0f6c96e1904d8a1aab613938dfca Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Fri, 16 Oct 2015 19:01:56 +0000 Subject: [PATCH] * Mail/Wiki/Sitemgr: reworked XSS tests to allow eg. font-names containing "script" and other patterns forbidden by previous test, also added new html5 specific tests --- phpgwapi/inc/class.egw_htmLawed.inc.php | 6 +- phpgwapi/inc/common_functions.inc.php | 98 ++++++++++++++++++++----- 2 files changed, 81 insertions(+), 23 deletions(-) diff --git a/phpgwapi/inc/class.egw_htmLawed.inc.php b/phpgwapi/inc/class.egw_htmLawed.inc.php index e145977886..8a406bcbd7 100644 --- a/phpgwapi/inc/class.egw_htmLawed.inc.php +++ b/phpgwapi/inc/class.egw_htmLawed.inc.php @@ -138,7 +138,7 @@ function hl_my_tag_transform($element, $attribute_array=0) //if ($element=='img') error_log(__METHOD__.__LINE__." ".$element.'->'.array2string($attribute_array)); if ($element=='td' && isset($attribute_array['background'])) { - if (stripos($attribute_array['background'],$GLOBALS['egw']->link('/index.php'))!==false) + if (is_object($GLOBALS['egw']) && stripos($attribute_array['background'],$GLOBALS['egw']->link('/index.php'))!==false) { //error_log(__METHOD__.__LINE__.array2string($attribute_array)); //$attribute_array['background'] = 'url('.$attribute_array['background'].');'; @@ -160,10 +160,6 @@ function hl_my_tag_transform($element, $attribute_array=0) { if (strpos($attribute_array['title'],'@')!==false) $attribute_array['title']=str_replace('@','(at)',$attribute_array['title']); } - if (isset($attribute_array['face'])) - { - if (stripos($attribute_array['face'],'script')!==false) $attribute_array['face']=str_ireplace('script','',$attribute_array['face']); - } if ($element == 'blockquote') { if (isset($attribute_array['cite'])) diff --git a/phpgwapi/inc/common_functions.inc.php b/phpgwapi/inc/common_functions.inc.php index 59d7dc5bdb..3b0e0a44e6 100755 --- a/phpgwapi/inc/common_functions.inc.php +++ b/phpgwapi/inc/common_functions.inc.php @@ -1451,6 +1451,17 @@ function function_backtrace($remove=0) */ function _check_script_tag(&$var,$name='') { + static $preg=null; + //old: '/<\/?[^>]*\b(iframe|script|javascript|on(before)?(abort|blur|change|click|dblclick|error|focus|keydown|keypress|keyup|load|mousedown|mousemove|mouseout|mouseover|mouseup|reset|select|submit|unload))\b[^>]*>/i'; + if (!isset($preg)) $preg = + // forbidden tags like iframe or script + '/(<(\s*\/)?\s*(iframe|script|object|embed|math|meta)|'. + // on* attributes + '<[^>]*on(before)?(abort|blur|change|click|dblclick|error|focus|keydown|keypress|keyup|load|mouse[^=]+|reset|select|submit|unload|resize|propertychange|page[^=]*|scroll|readystatechange|start|popstate|form[^=]+|input)\s*=|'. + // ="javascript:*" diverse javascript attribute value + '<[^>]+(href|src|dynsrc|lowsrc|background|style|poster|action)\s*=\s*("|\')?[^"\']*javascript|'. + // benavior:url and expression in style attribute + '<[^>]+style\s*=\s*("|\')[^>]*(behavior\s*:\s*url|expression)\s*\()/i'; if (is_array($var)) { foreach($var as $key => $val) @@ -1461,7 +1472,6 @@ function _check_script_tag(&$var,$name='') } elseif(strpos($val, '<') !== false) // speedup: ignore everything without < { - static $preg = '/<\/?[^>]*\b(iframe|script|javascript|on(before)?(abort|blur|change|click|dblclick|error|focus|keydown|keypress|keyup|load|mousedown|mousemove|mouseout|mouseover|mouseup|reset|select|submit|unload))\b[^>]*>/i'; if (preg_match($preg,$val)) { // special handling for $_POST[json_data], to decend into it's decoded content, fixing json direct might break json syntax @@ -1501,11 +1511,26 @@ if (isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE_ $total = $num_failed = 0; $patterns = array( // pattern => true: should fail, false: should not fail - '' => true, - 'blah' => true, - 'Click Me' => true, + '< script >alert(1)< / script >' => true, + 'blah' => true, + 'Click Me' => true, + // from https://www.acunetix.com/websitesecurity/cross-site-scripting/ + '' => true, + '' => true, + '