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