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