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