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