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