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