Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Security Class |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
| 35 | * @category Security |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 36 | * @author EllisLab Dev Team |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/libraries/security.html |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 38 | */ |
| 39 | class CI_Security { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 40 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 41 | /** |
| 42 | * Random Hash for protecting URLs |
| 43 | * |
| 44 | * @var string |
| 45 | * @access protected |
| 46 | */ |
| 47 | protected $_xss_hash = ''; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 48 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 49 | /** |
| 50 | * Random Hash for Cross Site Request Forgery Protection Cookie |
| 51 | * |
| 52 | * @var string |
| 53 | * @access protected |
| 54 | */ |
| 55 | protected $_csrf_hash = ''; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 56 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 57 | /** |
| 58 | * Expiration time for Cross Site Request Forgery Protection Cookie |
| 59 | * Defaults to two hours (in seconds) |
| 60 | * |
| 61 | * @var int |
| 62 | * @access protected |
| 63 | */ |
| 64 | protected $_csrf_expire = 7200; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 65 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 66 | /** |
| 67 | * Token name for Cross Site Request Forgery Protection Cookie |
| 68 | * |
| 69 | * @var string |
| 70 | * @access protected |
| 71 | */ |
| 72 | protected $_csrf_token_name = 'ci_csrf_token'; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 73 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 74 | /** |
| 75 | * Cookie name for Cross Site Request Forgery Protection Cookie |
| 76 | * |
| 77 | * @var string |
| 78 | * @access protected |
| 79 | */ |
| 80 | protected $_csrf_cookie_name = 'ci_csrf_token'; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 81 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 82 | /** |
| 83 | * List of never allowed strings |
| 84 | * |
| 85 | * @var array |
| 86 | * @access protected |
| 87 | */ |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 88 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 89 | protected $_never_allowed_str = array( |
| 90 | 'document.cookie' => '[removed]', |
| 91 | 'document.write' => '[removed]', |
| 92 | '.parentNode' => '[removed]', |
| 93 | '.innerHTML' => '[removed]', |
| 94 | 'window.location' => '[removed]', |
| 95 | '-moz-binding' => '[removed]', |
| 96 | '<!--' => '<!--', |
| 97 | '-->' => '-->', |
| 98 | '<![CDATA[' => '<![CDATA[' |
| 99 | ); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 100 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 101 | /** |
| 102 | * List of never allowed regex replacement |
| 103 | * |
| 104 | * @var array |
| 105 | * @access protected |
| 106 | */ |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 107 | protected $_never_allowed_regex = array( |
| 108 | "javascript\s*:" => '[removed]', |
| 109 | "expression\s*(\(|&\#40;)" => '[removed]', // CSS and IE |
| 110 | "vbscript\s*:" => '[removed]', // IE, surprise! |
| 111 | "Redirect\s+302" => '[removed]' |
| 112 | ); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 113 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 114 | /** |
| 115 | * Constructor |
| 116 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 117 | public function __construct() |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 118 | { |
patwork | ef1a55a | 2011-04-09 13:04:06 +0200 | [diff] [blame] | 119 | // CSRF config |
| 120 | foreach(array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key) |
| 121 | { |
| 122 | if (FALSE !== ($val = config_item($key))) |
| 123 | { |
| 124 | $this->{'_'.$key} = $val; |
| 125 | } |
| 126 | } |
| 127 | |
patwork | 9e26798 | 2011-04-11 13:02:32 +0200 | [diff] [blame] | 128 | // Append application specific cookie prefix |
Greg Aker | b3e614d | 2011-04-19 20:19:17 -0500 | [diff] [blame] | 129 | if (config_item('cookie_prefix')) |
| 130 | { |
patwork | 9e26798 | 2011-04-11 13:02:32 +0200 | [diff] [blame] | 131 | $this->_csrf_cookie_name = config_item('cookie_prefix').$this->_csrf_cookie_name; |
| 132 | } |
Derek Jones | b3f10a2 | 2010-07-25 19:11:26 -0500 | [diff] [blame] | 133 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 134 | // Set the CSRF hash |
| 135 | $this->_csrf_set_hash(); |
Derek Allard | 958543a | 2010-07-22 14:10:26 -0400 | [diff] [blame] | 136 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 137 | log_message('debug', "Security Class Initialized"); |
| 138 | } |
| 139 | |
| 140 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 142 | /** |
| 143 | * Verify Cross Site Request Forgery Protection |
| 144 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 145 | * @return object |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 146 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 147 | public function csrf_verify() |
Derek Allard | 958543a | 2010-07-22 14:10:26 -0400 | [diff] [blame] | 148 | { |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 149 | // If no POST data exists we will set the CSRF cookie |
| 150 | if (count($_POST) == 0) |
| 151 | { |
| 152 | return $this->csrf_set_cookie(); |
| 153 | } |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 154 | |
Alex Bilbie | aeb2c3e | 2011-08-21 16:14:54 +0100 | [diff] [blame] | 155 | // Check if URI has been whitelisted from CSRF checks |
| 156 | if ($exclude_uris = config_item('csrf_exclude_uris')) |
| 157 | { |
| 158 | $uri = load_class('URI', 'core'); |
| 159 | if (in_array($uri->uri_string(), $exclude_uris)) |
| 160 | { |
| 161 | return $this; |
| 162 | } |
| 163 | } |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 164 | |
| 165 | // Do the tokens exist in both the _POST and _COOKIE arrays? |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 166 | if ( ! isset($_POST[$this->_csrf_token_name]) OR |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 167 | ! isset($_COOKIE[$this->_csrf_cookie_name])) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 168 | { |
| 169 | $this->csrf_show_error(); |
| 170 | } |
| 171 | |
| 172 | // Do the tokens match? |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 173 | if ($_POST[$this->_csrf_token_name] != $_COOKIE[$this->_csrf_cookie_name]) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 174 | { |
| 175 | $this->csrf_show_error(); |
| 176 | } |
| 177 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 178 | // We kill this since we're done and we don't want to |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 179 | // polute the _POST array |
| 180 | unset($_POST[$this->_csrf_token_name]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Derek Jones | b3f10a2 | 2010-07-25 19:11:26 -0500 | [diff] [blame] | 182 | // Nothing should last forever |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 183 | unset($_COOKIE[$this->_csrf_cookie_name]); |
Rommel Castro A | e378a39 | 2011-09-22 18:52:25 -0600 | [diff] [blame] | 184 | $this->_csrf_hash = ''; |
Derek Jones | b3f10a2 | 2010-07-25 19:11:26 -0500 | [diff] [blame] | 185 | $this->_csrf_set_hash(); |
| 186 | $this->csrf_set_cookie(); |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 187 | |
Alex Bilbie | aeb2c3e | 2011-08-21 16:14:54 +0100 | [diff] [blame] | 188 | log_message('debug', "CSRF token verified"); |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 189 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 190 | return $this; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 191 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 192 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 193 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 194 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 195 | /** |
| 196 | * Set Cross Site Request Forgery Protection Cookie |
| 197 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 198 | * @return object |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 199 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 200 | public function csrf_set_cookie() |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 201 | { |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 202 | $expire = time() + $this->_csrf_expire; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 203 | $secure_cookie = (bool) config_item('cookie_secure'); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 204 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 205 | if ($secure_cookie) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 206 | { |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 207 | $req = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : FALSE; |
| 208 | |
| 209 | if ( ! $req OR $req == 'off') |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 210 | { |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 211 | return FALSE; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 212 | } |
| 213 | } |
Derek Allard | 958543a | 2010-07-22 14:10:26 -0400 | [diff] [blame] | 214 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 215 | setcookie($this->_csrf_cookie_name, $this->_csrf_hash, $expire, config_item('cookie_path'), config_item('cookie_domain'), $secure_cookie); |
| 216 | |
| 217 | log_message('debug', "CRSF cookie Set"); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 218 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 219 | return $this; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 224 | /** |
| 225 | * Show CSRF Error |
| 226 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 227 | * @return void |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 228 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 229 | public function csrf_show_error() |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 230 | { |
| 231 | show_error('The action you have requested is not allowed.'); |
| 232 | } |
| 233 | |
| 234 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 235 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 236 | /** |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 237 | * Get CSRF Hash |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 238 | * |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 239 | * Getter Method |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 240 | * |
| 241 | * @return string self::_csrf_hash |
| 242 | */ |
| 243 | public function get_csrf_hash() |
| 244 | { |
| 245 | return $this->_csrf_hash; |
| 246 | } |
| 247 | |
| 248 | // -------------------------------------------------------------------- |
| 249 | |
| 250 | /** |
| 251 | * Get CSRF Token Name |
| 252 | * |
| 253 | * Getter Method |
| 254 | * |
| 255 | * @return string self::csrf_token_name |
| 256 | */ |
| 257 | public function get_csrf_token_name() |
| 258 | { |
| 259 | return $this->_csrf_token_name; |
| 260 | } |
| 261 | |
| 262 | // -------------------------------------------------------------------- |
| 263 | |
| 264 | /** |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 265 | * XSS Clean |
| 266 | * |
| 267 | * Sanitizes data so that Cross Site Scripting Hacks can be |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 268 | * prevented. This function does a fair amount of work but |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 269 | * it is extremely thorough, designed to prevent even the |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 270 | * most obscure XSS attempts. Nothing is ever 100% foolproof, |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 271 | * of course, but I haven't been able to get anything passed |
| 272 | * the filter. |
| 273 | * |
| 274 | * Note: This function should only be used to deal with data |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 275 | * upon submission. It's not something that should |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 276 | * be used for general runtime processing. |
| 277 | * |
| 278 | * This function was based in part on some code and ideas I |
| 279 | * got from Bitflux: http://channel.bitflux.ch/wiki/XSS_Prevention |
| 280 | * |
| 281 | * To help develop this script I used this great list of |
| 282 | * vulnerabilities along with a few other hacks I've |
| 283 | * harvested from examining vulnerabilities in other programs: |
| 284 | * http://ha.ckers.org/xss.html |
| 285 | * |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 286 | * @param mixed string or array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 287 | * @param bool |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 288 | * @return string |
| 289 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 290 | public function xss_clean($str, $is_image = FALSE) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 291 | { |
| 292 | /* |
| 293 | * Is the string an array? |
| 294 | * |
| 295 | */ |
| 296 | if (is_array($str)) |
| 297 | { |
| 298 | while (list($key) = each($str)) |
| 299 | { |
| 300 | $str[$key] = $this->xss_clean($str[$key]); |
| 301 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 303 | return $str; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Remove Invisible Characters |
| 308 | */ |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 309 | $str = remove_invisible_characters($str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 310 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 311 | // Validate Entities in URLs |
| 312 | $str = $this->_validate_entities($str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * URL Decode |
| 316 | * |
| 317 | * Just in case stuff like this is submitted: |
| 318 | * |
| 319 | * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a> |
| 320 | * |
| 321 | * Note: Use rawurldecode() so it does not remove plus signs |
| 322 | * |
| 323 | */ |
| 324 | $str = rawurldecode($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 325 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 326 | /* |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 327 | * Convert character entities to ASCII |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 328 | * |
| 329 | * This permits our tests below to work reliably. |
| 330 | * We only convert entities that are within tags since |
| 331 | * these are the ones that will pose security problems. |
| 332 | * |
| 333 | */ |
| 334 | |
| 335 | $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 336 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 337 | $str = preg_replace_callback("/<\w+.*?(?=>|<|$)/si", array($this, '_decode_entity'), $str); |
| 338 | |
| 339 | /* |
| 340 | * Remove Invisible Characters Again! |
| 341 | */ |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 342 | $str = remove_invisible_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 344 | /* |
| 345 | * Convert all tabs to spaces |
| 346 | * |
| 347 | * This prevents strings like this: ja vascript |
| 348 | * NOTE: we deal with spaces between characters later. |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 349 | * NOTE: preg_replace was found to be amazingly slow here on |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 350 | * large blocks of data, so we use str_replace. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 351 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 352 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 353 | if (strpos($str, "\t") !== FALSE) |
| 354 | { |
| 355 | $str = str_replace("\t", ' ', $str); |
| 356 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 357 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 358 | /* |
| 359 | * Capture converted string for later comparison |
| 360 | */ |
| 361 | $converted_string = $str; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 363 | // Remove Strings that are never allowed |
| 364 | $str = $this->_do_never_allowed($str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * Makes PHP tags safe |
| 368 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 369 | * Note: XML tags are inadvertently replaced too: |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 370 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 371 | * <?xml |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 372 | * |
| 373 | * But it doesn't seem to pose a problem. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 374 | */ |
| 375 | if ($is_image === TRUE) |
| 376 | { |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 377 | // Images have a tendency to have the PHP short opening and |
| 378 | // closing tags every so often so we skip those and only |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 379 | // do the long opening tags. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 380 | $str = preg_replace('/<\?(php)/i', "<?\\1", $str); |
| 381 | } |
| 382 | else |
| 383 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 384 | $str = str_replace(array('<?', '?'.'>'), array('<?', '?>'), $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 385 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 386 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 387 | /* |
| 388 | * Compact any exploded words |
| 389 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 390 | * This corrects words like: j a v a s c r i p t |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 391 | * These words are compacted back to their correct state. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 392 | */ |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 393 | $words = array( |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 394 | 'javascript', 'expression', 'vbscript', 'script', |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 395 | 'applet', 'alert', 'document', 'write', 'cookie', 'window' |
| 396 | ); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 397 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 398 | foreach ($words as $word) |
| 399 | { |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 400 | $word = implode("\s*", str_split($word)) . "\s*"; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 401 | |
| 402 | // We only want to do this when it is followed by a non-word character |
| 403 | // That way valid stuff like "dealer to" does not become "dealerto" |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 404 | $str = preg_replace_callback('#('.substr($word, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 405 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 407 | /* |
| 408 | * Remove disallowed Javascript in links or img tags |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 409 | * We used to do some version comparisons and use of stripos for PHP5, |
| 410 | * but it is dog slow compared to these simplified non-capturing |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 411 | * preg_match(), especially if the pattern exists in the string |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 412 | */ |
| 413 | do |
| 414 | { |
| 415 | $original = $str; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 417 | if (preg_match("/<a/i", $str)) |
| 418 | { |
| 419 | $str = preg_replace_callback("#<a\s+([^>]*?)(>|$)#si", array($this, '_js_link_removal'), $str); |
| 420 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 421 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 422 | if (preg_match("/<img/i", $str)) |
| 423 | { |
| 424 | $str = preg_replace_callback("#<img\s+([^>]*?)(\s?/?>|$)#si", array($this, '_js_img_removal'), $str); |
| 425 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 426 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 427 | if (preg_match("/script/i", $str) OR preg_match("/xss/i", $str)) |
| 428 | { |
| 429 | $str = preg_replace("#<(/*)(script|xss)(.*?)\>#si", '[removed]', $str); |
| 430 | } |
| 431 | } |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 432 | while($original != $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 433 | |
| 434 | unset($original); |
| 435 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 436 | // Remove evil attributes such as style, onclick and xmlns |
| 437 | $str = $this->_remove_evil_attributes($str, $is_image); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 439 | /* |
| 440 | * Sanitize naughty HTML elements |
| 441 | * |
| 442 | * If a tag containing any of the words in the list |
| 443 | * below is found, the tag gets converted to entities. |
| 444 | * |
| 445 | * So this: <blink> |
| 446 | * Becomes: <blink> |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 447 | */ |
| 448 | $naughty = 'alert|applet|audio|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|isindex|layer|link|meta|object|plaintext|style|script|textarea|title|video|xml|xss'; |
| 449 | $str = preg_replace_callback('#<(/*\s*)('.$naughty.')([^><]*)([><]*)#is', array($this, '_sanitize_naughty_html'), $str); |
| 450 | |
| 451 | /* |
| 452 | * Sanitize naughty scripting elements |
| 453 | * |
| 454 | * Similar to above, only instead of looking for |
| 455 | * tags it looks for PHP and JavaScript commands |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 456 | * that are disallowed. Rather than removing the |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 457 | * code, it simply converts the parenthesis to entities |
| 458 | * rendering the code un-executable. |
| 459 | * |
| 460 | * For example: eval('some code') |
| 461 | * Becomes: eval('some code') |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 462 | */ |
| 463 | $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2(\\3)", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 464 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 465 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 466 | // Final clean up |
| 467 | // This adds a bit of extra precaution in case |
| 468 | // something got through the above filters |
| 469 | $str = $this->_do_never_allowed($str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 470 | |
| 471 | /* |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 472 | * Images are Handled in a Special Way |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 473 | * - Essentially, we want to know that after all of the character |
| 474 | * conversion is done whether any unwanted, likely XSS, code was found. |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 475 | * If not, we return TRUE, as the image is clean. |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 476 | * However, if the string post-conversion does not matched the |
| 477 | * string post-removal of XSS, then it fails, as there was unwanted XSS |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 478 | * code found and removed/changed during processing. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 479 | */ |
| 480 | |
| 481 | if ($is_image === TRUE) |
| 482 | { |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 483 | return ($str === $converted_string) ? TRUE : FALSE; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 484 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 486 | log_message('debug', "XSS Filtering completed"); |
| 487 | return $str; |
| 488 | } |
| 489 | |
| 490 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 492 | /** |
| 493 | * Random Hash for protecting URLs |
| 494 | * |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 495 | * @return string |
| 496 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 497 | public function xss_hash() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 498 | { |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 499 | if ($this->_xss_hash == '') |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 500 | { |
| 501 | if (phpversion() >= 4.2) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 502 | { |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 503 | mt_srand(); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 504 | } |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 505 | else |
| 506 | { |
| 507 | mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff); |
| 508 | } |
| 509 | |
| 510 | $this->_xss_hash = md5(time() + mt_rand(0, 1999999999)); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 511 | } |
| 512 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 513 | return $this->_xss_hash; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | // -------------------------------------------------------------------- |
| 517 | |
| 518 | /** |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 519 | * HTML Entities Decode |
| 520 | * |
| 521 | * This function is a replacement for html_entity_decode() |
| 522 | * |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 523 | * @param string |
| 524 | * @param string |
| 525 | * @return string |
| 526 | */ |
freewil | 8cc0cfe | 2011-08-27 21:53:00 -0400 | [diff] [blame] | 527 | public function entity_decode($str, $charset = NULL) |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 528 | { |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 529 | if (strpos($str, '&') === FALSE) |
freewil | 5c9b0d1 | 2011-08-28 12:15:23 -0400 | [diff] [blame] | 530 | { |
| 531 | return $str; |
| 532 | } |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 533 | |
freewil | 5c9b0d1 | 2011-08-28 12:15:23 -0400 | [diff] [blame] | 534 | if (empty($charset)) |
| 535 | { |
| 536 | $charset = config_item('charset'); |
| 537 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 538 | |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 539 | // The reason we are not using html_entity_decode() by itself is because |
| 540 | // while it is not technically correct to leave out the semicolon |
| 541 | // at the end of an entity most browsers will still interpret the entity |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 542 | // correctly. html_entity_decode() does not convert entities without |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 543 | // semicolons, so we are left with our own little solution here. Bummer. |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 544 | $str = html_entity_decode($str, ENT_COMPAT, $charset); |
| 545 | $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str); |
| 546 | return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str); |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 547 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 548 | |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 549 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 550 | |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 551 | /** |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 552 | * Filename Security |
| 553 | * |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 554 | * @param string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 555 | * @param bool |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 556 | * @return string |
| 557 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 558 | public function sanitize_filename($str, $relative_path = FALSE) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 559 | { |
| 560 | $bad = array( |
| 561 | "../", |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 562 | "<!--", |
| 563 | "-->", |
| 564 | "<", |
| 565 | ">", |
| 566 | "'", |
| 567 | '"', |
| 568 | '&', |
| 569 | '$', |
| 570 | '#', |
| 571 | '{', |
| 572 | '}', |
| 573 | '[', |
| 574 | ']', |
| 575 | '=', |
| 576 | ';', |
| 577 | '?', |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 578 | "%20", |
| 579 | "%22", |
| 580 | "%3c", // < |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 581 | "%253c", // < |
| 582 | "%3e", // > |
| 583 | "%0e", // > |
| 584 | "%28", // ( |
| 585 | "%29", // ) |
| 586 | "%2528", // ( |
| 587 | "%26", // & |
| 588 | "%24", // $ |
| 589 | "%3f", // ? |
| 590 | "%3b", // ; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 591 | "%3d" // = |
| 592 | ); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 593 | |
Derek Jones | 2ef3759 | 2010-10-06 17:51:59 -0500 | [diff] [blame] | 594 | if ( ! $relative_path) |
| 595 | { |
| 596 | $bad[] = './'; |
| 597 | $bad[] = '/'; |
| 598 | } |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 599 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 600 | $str = remove_invisible_characters($str, FALSE); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 601 | return stripslashes(str_replace($bad, '', $str)); |
| 602 | } |
| 603 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 604 | // ---------------------------------------------------------------- |
| 605 | |
| 606 | /** |
| 607 | * Compact Exploded Words |
| 608 | * |
| 609 | * Callback function for xss_clean() to remove whitespace from |
| 610 | * things like j a v a s c r i p t |
| 611 | * |
| 612 | * @param type |
| 613 | * @return type |
| 614 | */ |
| 615 | protected function _compact_exploded_words($matches) |
| 616 | { |
| 617 | return preg_replace('/\s+/s', '', $matches[1]).$matches[2]; |
| 618 | } |
| 619 | |
| 620 | // -------------------------------------------------------------------- |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 621 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 622 | /* |
| 623 | * Remove Evil HTML Attributes (like evenhandlers and style) |
| 624 | * |
| 625 | * It removes the evil attribute and either: |
| 626 | * - Everything up until a space |
| 627 | * For example, everything between the pipes: |
| 628 | * <a |style=document.write('hello');alert('world');| class=link> |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 629 | * - Everything inside the quotes |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 630 | * For example, everything between the pipes: |
| 631 | * <a |style="document.write('hello'); alert('world');"| class="link"> |
| 632 | * |
| 633 | * @param string $str The string to check |
| 634 | * @param boolean $is_image TRUE if this is an image |
| 635 | * @return string The string with the evil attributes removed |
| 636 | */ |
| 637 | protected function _remove_evil_attributes($str, $is_image) |
| 638 | { |
| 639 | // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns |
| 640 | $evil_attributes = array('on\w*', 'style', 'xmlns'); |
| 641 | |
| 642 | if ($is_image === TRUE) |
| 643 | { |
| 644 | /* |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 645 | * Adobe Photoshop puts XML metadata into JFIF images, |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 646 | * including namespacing, so we have to allow this for images. |
| 647 | */ |
| 648 | unset($evil_attributes[array_search('xmlns', $evil_attributes)]); |
| 649 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 650 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 651 | do { |
| 652 | $str = preg_replace( |
| 653 | "#<(/?[^><]+?)([^A-Za-z\-])(".implode('|', $evil_attributes).")(\s*=\s*)([\"][^>]*?[\"]|[\'][^>]*?[\']|[^>]*?)([\s><])([><]*)#i", |
| 654 | "<$1$6", |
| 655 | $str, -1, $count |
| 656 | ); |
| 657 | } while ($count); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 658 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 659 | return $str; |
| 660 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 661 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 662 | // -------------------------------------------------------------------- |
| 663 | |
| 664 | /** |
| 665 | * Sanitize Naughty HTML |
| 666 | * |
| 667 | * Callback function for xss_clean() to remove naughty HTML elements |
| 668 | * |
| 669 | * @param array |
| 670 | * @return string |
| 671 | */ |
| 672 | protected function _sanitize_naughty_html($matches) |
| 673 | { |
| 674 | // encode opening brace |
| 675 | $str = '<'.$matches[1].$matches[2].$matches[3]; |
| 676 | |
| 677 | // encode captured opening or closing brace to prevent recursive vectors |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 678 | $str .= str_replace(array('>', '<'), array('>', '<'), |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 679 | $matches[4]); |
| 680 | |
| 681 | return $str; |
| 682 | } |
| 683 | |
| 684 | // -------------------------------------------------------------------- |
| 685 | |
| 686 | /** |
| 687 | * JS Link Removal |
| 688 | * |
| 689 | * Callback function for xss_clean() to sanitize links |
| 690 | * This limits the PCRE backtracks, making it more performance friendly |
| 691 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
| 692 | * PHP 5.2+ on link-heavy strings |
| 693 | * |
| 694 | * @param array |
| 695 | * @return string |
| 696 | */ |
| 697 | protected function _js_link_removal($match) |
| 698 | { |
| 699 | $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1])); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 700 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 701 | return str_replace($match[1], preg_replace("#href=.*?(alert\(|alert&\#40;|javascript\:|livescript\:|mocha\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]); |
| 702 | } |
| 703 | |
| 704 | // -------------------------------------------------------------------- |
| 705 | |
| 706 | /** |
| 707 | * JS Image Removal |
| 708 | * |
| 709 | * Callback function for xss_clean() to sanitize image tags |
| 710 | * This limits the PCRE backtracks, making it more performance friendly |
| 711 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
| 712 | * PHP 5.2+ on image tag heavy strings |
| 713 | * |
| 714 | * @param array |
| 715 | * @return string |
| 716 | */ |
| 717 | protected function _js_img_removal($match) |
| 718 | { |
| 719 | $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1])); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 720 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 721 | return str_replace($match[1], preg_replace("#src=.*?(alert\(|alert&\#40;|javascript\:|livescript\:|mocha\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]); |
| 722 | } |
| 723 | |
| 724 | // -------------------------------------------------------------------- |
| 725 | |
| 726 | /** |
| 727 | * Attribute Conversion |
| 728 | * |
| 729 | * Used as a callback for XSS Clean |
| 730 | * |
| 731 | * @param array |
| 732 | * @return string |
| 733 | */ |
| 734 | protected function _convert_attribute($match) |
| 735 | { |
| 736 | return str_replace(array('>', '<', '\\'), array('>', '<', '\\\\'), $match[0]); |
| 737 | } |
| 738 | |
| 739 | // -------------------------------------------------------------------- |
| 740 | |
| 741 | /** |
| 742 | * Filter Attributes |
| 743 | * |
| 744 | * Filters tag attributes for consistency and safety |
| 745 | * |
| 746 | * @param string |
| 747 | * @return string |
| 748 | */ |
| 749 | protected function _filter_attributes($str) |
| 750 | { |
| 751 | $out = ''; |
| 752 | |
| 753 | if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches)) |
| 754 | { |
| 755 | foreach ($matches[0] as $match) |
| 756 | { |
| 757 | $out .= preg_replace("#/\*.*?\*/#s", '', $match); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | return $out; |
| 762 | } |
| 763 | |
| 764 | // -------------------------------------------------------------------- |
| 765 | |
| 766 | /** |
| 767 | * HTML Entity Decode Callback |
| 768 | * |
| 769 | * Used as a callback for XSS Clean |
| 770 | * |
| 771 | * @param array |
| 772 | * @return string |
| 773 | */ |
| 774 | protected function _decode_entity($match) |
| 775 | { |
| 776 | return $this->entity_decode($match[0], strtoupper(config_item('charset'))); |
| 777 | } |
| 778 | |
| 779 | // -------------------------------------------------------------------- |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 780 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 781 | /** |
| 782 | * Validate URL entities |
| 783 | * |
| 784 | * Called by xss_clean() |
| 785 | * |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 786 | * @param string |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 787 | * @return string |
| 788 | */ |
| 789 | protected function _validate_entities($str) |
| 790 | { |
| 791 | /* |
| 792 | * Protect GET variables in URLs |
| 793 | */ |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 794 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 795 | // 901119URL5918AMP18930PROTECT8198 |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 796 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 797 | $str = preg_replace('|\&([a-z\_0-9\-]+)\=([a-z\_0-9\-]+)|i', $this->xss_hash()."\\1=\\2", $str); |
| 798 | |
| 799 | /* |
| 800 | * Validate standard character entities |
| 801 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 802 | * Add a semicolon if missing. We do this to enable |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 803 | * the conversion of entities to ASCII later. |
| 804 | * |
| 805 | */ |
| 806 | $str = preg_replace('#(&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', "\\1;\\2", $str); |
| 807 | |
| 808 | /* |
| 809 | * Validate UTF16 two byte encoding (x00) |
| 810 | * |
| 811 | * Just as above, adds a semicolon if missing. |
| 812 | * |
| 813 | */ |
| 814 | $str = preg_replace('#(&\#x?)([0-9A-F]+);?#i',"\\1\\2;",$str); |
| 815 | |
| 816 | /* |
| 817 | * Un-Protect GET variables in URLs |
| 818 | */ |
| 819 | $str = str_replace($this->xss_hash(), '&', $str); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 820 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 821 | return $str; |
| 822 | } |
| 823 | |
| 824 | // ---------------------------------------------------------------------- |
| 825 | |
| 826 | /** |
| 827 | * Do Never Allowed |
| 828 | * |
| 829 | * A utility function for xss_clean() |
| 830 | * |
| 831 | * @param string |
| 832 | * @return string |
| 833 | */ |
| 834 | protected function _do_never_allowed($str) |
| 835 | { |
| 836 | foreach ($this->_never_allowed_str as $key => $val) |
| 837 | { |
| 838 | $str = str_replace($key, $val, $str); |
| 839 | } |
| 840 | |
| 841 | foreach ($this->_never_allowed_regex as $key => $val) |
| 842 | { |
| 843 | $str = preg_replace("#".$key."#i", $val, $str); |
| 844 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 845 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 846 | return $str; |
| 847 | } |
| 848 | |
| 849 | // -------------------------------------------------------------------- |
| 850 | |
| 851 | /** |
| 852 | * Set Cross Site Request Forgery Protection Cookie |
| 853 | * |
| 854 | * @return string |
| 855 | */ |
| 856 | protected function _csrf_set_hash() |
| 857 | { |
| 858 | if ($this->_csrf_hash == '') |
| 859 | { |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 860 | // If the cookie exists we will use it's value. |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 861 | // We don't necessarily want to regenerate it with |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 862 | // each page load since a page could contain embedded |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 863 | // sub-pages causing this feature to fail |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 864 | if (isset($_COOKIE[$this->_csrf_cookie_name]) && |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 865 | $_COOKIE[$this->_csrf_cookie_name] != '') |
| 866 | { |
| 867 | return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name]; |
| 868 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 869 | |
Chris Berthe | d93e6f3 | 2011-09-25 10:33:25 -0400 | [diff] [blame] | 870 | $this->_csrf_hash = md5(uniqid(rand(), TRUE)); |
| 871 | $this->csrf_set_cookie(); |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | return $this->_csrf_hash; |
| 875 | } |
| 876 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 877 | } |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 878 | |
| 879 | /* End of file Security.php */ |
purwandi | 064da7b | 2011-10-05 17:51:26 +0700 | [diff] [blame] | 880 | /* Location: ./system/core/Security.php */ |