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