diff --git a/classes/context/Context.class.php b/classes/context/Context.class.php index 86c33c211..ba96dda4a 100644 --- a/classes/context/Context.class.php +++ b/classes/context/Context.class.php @@ -1407,20 +1407,17 @@ class Context } elseif($_val = trim($_val)) { - if(in_array($key, array('page', 'cpage')) || ends_with('srl', $key, false)) + if(in_array($key, array('page', 'cpage')) || ends_with('srl', $key, false) && preg_match('/[^0-9,]/', $_val)) { - if(preg_match('/[^0-9,]/', $_val)) - { - $_val = (int)$_val; - } + $_val = (int)$_val; } - elseif(in_array($key, array('mid', 'search_keyword', 'xe_validator_id'))) + elseif(in_array($key, array('mid', 'vid', 'search_target', 'search_keyword', 'xe_validator_id')) || count($_GET)) { $_val = escape($_val, false); - } - elseif($key === 'vid') - { - $_val = urlencode($_val); + if(ends_with('url', $key, false)) + { + $_val = strtr($_val, array('&' => '&')); + } } } $result[escape($_key)] = $_val; diff --git a/classes/security/UploadFileFilter.class.php b/classes/security/UploadFileFilter.class.php index 1b3b73a34..85aae19ba 100644 --- a/classes/security/UploadFileFilter.class.php +++ b/classes/security/UploadFileFilter.class.php @@ -1,5 +1,4 @@ */ class UploadFileFilter { @@ -19,7 +18,7 @@ class UploadFileFilter } // Return error if the file size is zero. - if (!filesize($file)) + if (($filesize = filesize($file)) == 0) { return false; } @@ -27,38 +26,124 @@ class UploadFileFilter // Get the extension. $ext = $filename ? strtolower(substr(strrchr($filename, '.'), 1)) : ''; + // Check the first 4KB of the file for possible XML content. + $fp = fopen($file, 'rb'); + $first4kb = fread($fp, 4096); + $is_xml = preg_match('/<(?:\?xml|!DOCTYPE|html|head|body|meta|script|svg)\b/i', $first4kb); + // Check SVG files. - if ($ext === 'svg' && !self::_checkSVG($file)) + if (($ext === 'svg' || $is_xml) && !self::_checkSVG($fp, 0, $filesize)) { + fclose($fp); + return false; + } + + // Check XML files. + if (($ext === 'xml' || $is_xml) && !self::_checkXML($fp, 0, $filesize)) + { + fclose($fp); + return false; + } + + // Check HTML files. + if (($ext === 'html' || $ext === 'shtml' || $ext === 'xhtml' || $ext === 'phtml' || $is_xml) && !self::_checkHTML($fp, 0, $filesize)) + { + fclose($fp); return false; } // Return true if everything is OK. + fclose($fp); return true; } /** * Check SVG file for XSS or SSRF vulnerabilities (#1088, #1089) * - * @param string $file + * @param resource $fp + * @param int $from + * @param int $to * @return bool */ - protected static function _checkSVG($file) + protected static function _checkSVG($fp, $from, $to) { - $content = file_get_contents($file); - - if (preg_match('/xlink:href\s*=\s*"(?!data:)/i', $content)) + if (self::_matchStream('/ 0) + { + if (preg_match($regexp, $content)) + { + return true; + } + fseek($fp, min($to, $position += $block_size)); + } + return false; + } } /* End of file : UploadFileFilter.class.php */