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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 39 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 40 | /** |
| 41 | * Security Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Security |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/security.html |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 48 | */ |
| 49 | class CI_Security { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 50 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 51 | /** |
Hunter Wu | a8d6d3b | 2013-08-03 23:17:45 +0800 | [diff] [blame] | 52 | * List of sanitize filename strings |
| 53 | * |
| 54 | * @var array |
| 55 | */ |
Hunter Wu | 4495cc7 | 2013-08-04 12:31:52 +0800 | [diff] [blame] | 56 | public $filename_bad_chars = array( |
Hunter Wu | a8d6d3b | 2013-08-03 23:17:45 +0800 | [diff] [blame] | 57 | '../', '<!--', '-->', '<', '>', |
| 58 | "'", '"', '&', '$', '#', |
| 59 | '{', '}', '[', ']', '=', |
| 60 | ';', '?', '%20', '%22', |
| 61 | '%3c', // < |
| 62 | '%253c', // < |
| 63 | '%3e', // > |
| 64 | '%0e', // > |
| 65 | '%28', // ( |
| 66 | '%29', // ) |
| 67 | '%2528', // ( |
| 68 | '%26', // & |
| 69 | '%24', // $ |
| 70 | '%3f', // ? |
| 71 | '%3b', // ; |
| 72 | '%3d' // = |
| 73 | ); |
| 74 | |
| 75 | /** |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 76 | * Character set |
Andrey Andreev | c67c3fb | 2014-01-22 13:26:00 +0200 | [diff] [blame] | 77 | * |
Claudio Galdiolo | 325d22d | 2015-01-29 11:41:17 -0500 | [diff] [blame] | 78 | * Will be overridden by the constructor. |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 79 | * |
| 80 | * @var string |
Andrey Andreev | c67c3fb | 2014-01-22 13:26:00 +0200 | [diff] [blame] | 81 | */ |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 82 | public $charset = 'UTF-8'; |
Andrey Andreev | c67c3fb | 2014-01-22 13:26:00 +0200 | [diff] [blame] | 83 | |
| 84 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 85 | * XSS Hash |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 86 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 87 | * Random Hash for protecting URLs. |
| 88 | * |
| 89 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 90 | */ |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 91 | protected $_xss_hash; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 92 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 93 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 94 | * CSRF Hash |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 95 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 96 | * Random hash for Cross Site Request Forgery protection cookie |
| 97 | * |
| 98 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 99 | */ |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 100 | protected $_csrf_hash; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 101 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 102 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 103 | * CSRF Expire time |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 104 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 105 | * Expiration time for Cross Site Request Forgery protection cookie. |
| 106 | * Defaults to two hours (in seconds). |
| 107 | * |
| 108 | * @var int |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 109 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 110 | protected $_csrf_expire = 7200; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 111 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 112 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 113 | * CSRF Token name |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 114 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 115 | * Token name for Cross Site Request Forgery protection cookie. |
| 116 | * |
| 117 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 118 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 119 | protected $_csrf_token_name = 'ci_csrf_token'; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 120 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 121 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 122 | * CSRF Cookie name |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 123 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 124 | * Cookie name for Cross Site Request Forgery protection cookie. |
| 125 | * |
| 126 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 127 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 128 | protected $_csrf_cookie_name = 'ci_csrf_token'; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 129 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 130 | /** |
| 131 | * List of never allowed strings |
| 132 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 133 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 134 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 135 | protected $_never_allowed_str = array( |
Andrey Andreev | 57fa143 | 2016-10-28 17:46:31 +0300 | [diff] [blame] | 136 | 'document.cookie' => '[removed]', |
| 137 | 'document.write' => '[removed]', |
| 138 | '.parentNode' => '[removed]', |
| 139 | '.innerHTML' => '[removed]', |
| 140 | '-moz-binding' => '[removed]', |
| 141 | '<!--' => '<!--', |
| 142 | '-->' => '-->', |
| 143 | '<![CDATA[' => '<![CDATA[', |
| 144 | '<comment>' => '<comment>', |
| 145 | '<%' => '<%' |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 146 | ); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 147 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 148 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 149 | * List of never allowed regex replacements |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 150 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 151 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 152 | */ |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 153 | protected $_never_allowed_regex = array( |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 154 | 'javascript\s*:', |
Andrey Andreev | 1bbc564 | 2014-01-07 12:45:27 +0200 | [diff] [blame] | 155 | '(document|(document\.)?window)\.(location|on\w*)', |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 156 | 'expression\s*(\(|&\#40;)', // CSS and IE |
| 157 | 'vbscript\s*:', // IE, surprise! |
Andrey Andreev | a30a717 | 2014-02-10 09:17:25 +0200 | [diff] [blame] | 158 | 'wscript\s*:', // IE |
Andrey Andreev | f7f9dca | 2014-02-10 12:41:00 +0200 | [diff] [blame] | 159 | 'jscript\s*:', // IE |
Andrey Andreev | a30a717 | 2014-02-10 09:17:25 +0200 | [diff] [blame] | 160 | 'vbs\s*:', // IE |
Andrey Andreev | 4356806 | 2014-01-21 23:52:31 +0200 | [diff] [blame] | 161 | 'Redirect\s+30\d', |
Wes Baker | d348135 | 2012-05-07 16:49:33 -0400 | [diff] [blame] | 162 | "([\"'])?data\s*:[^\\1]*?base64[^\\1]*?,[^\\1]*?\\1?" |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 163 | ); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 164 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 165 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 166 | * Class constructor |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 167 | * |
| 168 | * @return void |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 169 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 170 | public function __construct() |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 171 | { |
Andrey Andreev | 67ccdc0 | 2012-02-27 23:57:58 +0200 | [diff] [blame] | 172 | // Is CSRF protection enabled? |
Andrey Andreev | d444d44 | 2014-10-06 00:00:08 +0300 | [diff] [blame] | 173 | if (config_item('csrf_protection')) |
patwork | ef1a55a | 2011-04-09 13:04:06 +0200 | [diff] [blame] | 174 | { |
Andrey Andreev | 67ccdc0 | 2012-02-27 23:57:58 +0200 | [diff] [blame] | 175 | // CSRF config |
| 176 | foreach (array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key) |
patwork | ef1a55a | 2011-04-09 13:04:06 +0200 | [diff] [blame] | 177 | { |
Andrey Andreev | 7cc3e99 | 2014-10-06 08:04:44 +0300 | [diff] [blame] | 178 | if (NULL !== ($val = config_item($key))) |
Andrey Andreev | 67ccdc0 | 2012-02-27 23:57:58 +0200 | [diff] [blame] | 179 | { |
| 180 | $this->{'_'.$key} = $val; |
| 181 | } |
patwork | ef1a55a | 2011-04-09 13:04:06 +0200 | [diff] [blame] | 182 | } |
patwork | ef1a55a | 2011-04-09 13:04:06 +0200 | [diff] [blame] | 183 | |
Andrey Andreev | 67ccdc0 | 2012-02-27 23:57:58 +0200 | [diff] [blame] | 184 | // Append application specific cookie prefix |
Andrey Andreev | d444d44 | 2014-10-06 00:00:08 +0300 | [diff] [blame] | 185 | if ($cookie_prefix = config_item('cookie_prefix')) |
Andrey Andreev | 67ccdc0 | 2012-02-27 23:57:58 +0200 | [diff] [blame] | 186 | { |
Andrey Andreev | d444d44 | 2014-10-06 00:00:08 +0300 | [diff] [blame] | 187 | $this->_csrf_cookie_name = $cookie_prefix.$this->_csrf_cookie_name; |
Andrey Andreev | 67ccdc0 | 2012-02-27 23:57:58 +0200 | [diff] [blame] | 188 | } |
Derek Jones | b3f10a2 | 2010-07-25 19:11:26 -0500 | [diff] [blame] | 189 | |
Andrey Andreev | 67ccdc0 | 2012-02-27 23:57:58 +0200 | [diff] [blame] | 190 | // Set the CSRF hash |
| 191 | $this->_csrf_set_hash(); |
| 192 | } |
Derek Allard | 958543a | 2010-07-22 14:10:26 -0400 | [diff] [blame] | 193 | |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 194 | $this->charset = strtoupper(config_item('charset')); |
| 195 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 196 | log_message('info', 'Security Class Initialized'); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 201 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 202 | * CSRF Verify |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 203 | * |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 204 | * @return CI_Security |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 205 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 206 | public function csrf_verify() |
Derek Allard | 958543a | 2010-07-22 14:10:26 -0400 | [diff] [blame] | 207 | { |
Andrey Andreev | 5d27c43 | 2012-03-08 12:01:52 +0200 | [diff] [blame] | 208 | // If it's not a POST request we will set the CSRF cookie |
| 209 | if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 210 | { |
| 211 | return $this->csrf_set_cookie(); |
| 212 | } |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 213 | |
Alex Bilbie | aeb2c3e | 2011-08-21 16:14:54 +0100 | [diff] [blame] | 214 | // Check if URI has been whitelisted from CSRF checks |
| 215 | if ($exclude_uris = config_item('csrf_exclude_uris')) |
| 216 | { |
| 217 | $uri = load_class('URI', 'core'); |
caseyh | 5ac7c77 | 2014-08-18 05:10:24 -0400 | [diff] [blame] | 218 | foreach ($exclude_uris as $excluded) |
| 219 | { |
Andrey Andreev | 6c52096 | 2014-08-18 12:24:42 +0300 | [diff] [blame] | 220 | if (preg_match('#^'.$excluded.'$#i'.(UTF8_ENABLED ? 'u' : ''), $uri->uri_string())) |
| 221 | { |
| 222 | return $this; |
| 223 | } |
| 224 | } |
Alex Bilbie | aeb2c3e | 2011-08-21 16:14:54 +0100 | [diff] [blame] | 225 | } |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 226 | |
Andrey Andreev | 5a2390d | 2017-01-04 16:01:27 +0200 | [diff] [blame] | 227 | // Check CSRF token validity, but don't error on mismatch just yet - we'll want to regenerate |
| 228 | $valid = isset($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name]) |
| 229 | && hash_equals($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name]); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 230 | |
Andrey Andreev | 1d0bd83 | 2016-08-29 14:14:54 +0300 | [diff] [blame] | 231 | // We kill this since we're done and we don't want to pollute the _POST array |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 232 | unset($_POST[$this->_csrf_token_name]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 233 | |
RS71 | 2be25a6 | 2011-12-31 16:02:04 -0200 | [diff] [blame] | 234 | // Regenerate on every submission? |
| 235 | if (config_item('csrf_regenerate')) |
| 236 | { |
| 237 | // Nothing should last forever |
| 238 | unset($_COOKIE[$this->_csrf_cookie_name]); |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 239 | $this->_csrf_hash = NULL; |
RS71 | 2be25a6 | 2011-12-31 16:02:04 -0200 | [diff] [blame] | 240 | } |
Andrey Andreev | 8a7d078 | 2012-01-08 05:43:42 +0200 | [diff] [blame] | 241 | |
Derek Jones | b3f10a2 | 2010-07-25 19:11:26 -0500 | [diff] [blame] | 242 | $this->_csrf_set_hash(); |
| 243 | $this->csrf_set_cookie(); |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 244 | |
Andrey Andreev | 5a2390d | 2017-01-04 16:01:27 +0200 | [diff] [blame] | 245 | if ($valid !== TRUE) |
| 246 | { |
| 247 | $this->csrf_show_error(); |
| 248 | } |
| 249 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 250 | log_message('info', 'CSRF token verified'); |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 251 | return $this; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 252 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 254 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 256 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 257 | * CSRF Set Cookie |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 258 | * |
Taufan Aditya | 6c7526c | 2012-05-27 13:51:27 +0700 | [diff] [blame] | 259 | * @codeCoverageIgnore |
Andrew Podner | 4296a65 | 2012-12-17 07:51:15 -0500 | [diff] [blame] | 260 | * @return CI_Security |
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_set_cookie() |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 263 | { |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 264 | $expire = time() + $this->_csrf_expire; |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 265 | $secure_cookie = (bool) config_item('cookie_secure'); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 266 | |
Andrey Andreev | 3fb0267 | 2012-10-22 16:48:01 +0300 | [diff] [blame] | 267 | if ($secure_cookie && ! is_https()) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 268 | { |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 269 | return FALSE; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 270 | } |
Derek Allard | 958543a | 2010-07-22 14:10:26 -0400 | [diff] [blame] | 271 | |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 272 | setcookie( |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 273 | $this->_csrf_cookie_name, |
| 274 | $this->_csrf_hash, |
| 275 | $expire, |
| 276 | config_item('cookie_path'), |
| 277 | config_item('cookie_domain'), |
freewil | 4ad0fd8 | 2012-03-13 22:37:42 -0400 | [diff] [blame] | 278 | $secure_cookie, |
| 279 | config_item('cookie_httponly') |
| 280 | ); |
Mohammad Sadegh Dehghan Niri | 7881fd1 | 2015-07-15 17:48:57 +0430 | [diff] [blame] | 281 | log_message('info', 'CSRF cookie sent'); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 282 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 283 | return $this; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 287 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 288 | /** |
| 289 | * Show CSRF Error |
| 290 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 291 | * @return void |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 292 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 293 | public function csrf_show_error() |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 294 | { |
Kyle Valade | 05fcc09 | 2014-07-06 13:43:20 -0700 | [diff] [blame] | 295 | show_error('The action you have requested is not allowed.', 403); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 300 | /** |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 301 | * Get CSRF Hash |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 302 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 303 | * @see CI_Security::$_csrf_hash |
| 304 | * @return string CSRF hash |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 305 | */ |
| 306 | public function get_csrf_hash() |
| 307 | { |
| 308 | return $this->_csrf_hash; |
| 309 | } |
| 310 | |
| 311 | // -------------------------------------------------------------------- |
| 312 | |
| 313 | /** |
| 314 | * Get CSRF Token Name |
| 315 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 316 | * @see CI_Security::$_csrf_token_name |
| 317 | * @return string CSRF token name |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 318 | */ |
| 319 | public function get_csrf_token_name() |
| 320 | { |
| 321 | return $this->_csrf_token_name; |
| 322 | } |
| 323 | |
| 324 | // -------------------------------------------------------------------- |
| 325 | |
| 326 | /** |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 327 | * XSS Clean |
| 328 | * |
| 329 | * Sanitizes data so that Cross Site Scripting Hacks can be |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 330 | * prevented. This method does a fair amount of work but |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 331 | * it is extremely thorough, designed to prevent even the |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 332 | * most obscure XSS attempts. Nothing is ever 100% foolproof, |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 333 | * of course, but I haven't been able to get anything passed |
| 334 | * the filter. |
| 335 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 336 | * Note: Should only be used to deal with data upon submission. |
| 337 | * It's not something that should be used for general |
| 338 | * runtime processing. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 339 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 340 | * @link http://channel.bitflux.ch/wiki/XSS_Prevention |
| 341 | * Based in part on some code and ideas from Bitflux. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 342 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 343 | * @link http://ha.ckers.org/xss.html |
| 344 | * To help develop this script I used this great list of |
| 345 | * vulnerabilities along with a few other hacks I've |
| 346 | * harvested from examining vulnerabilities in other programs. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 347 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 348 | * @param string|string[] $str Input data |
| 349 | * @param bool $is_image Whether the input is an image |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 350 | * @return string |
| 351 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 352 | public function xss_clean($str, $is_image = FALSE) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 353 | { |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 354 | // Is the string an array? |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 355 | if (is_array($str)) |
| 356 | { |
Andrey Andreev | 4316a15 | 2017-01-20 15:46:17 +0200 | [diff] [blame] | 357 | foreach ($str as $key => &$value) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 358 | { |
Andrey Andreev | 4316a15 | 2017-01-20 15:46:17 +0200 | [diff] [blame] | 359 | $str[$key] = $this->xss_clean($value); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 360 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 361 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 362 | return $str; |
| 363 | } |
| 364 | |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 365 | // Remove Invisible Characters |
| 366 | $str = remove_invisible_characters($str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * URL Decode |
| 370 | * |
| 371 | * Just in case stuff like this is submitted: |
| 372 | * |
| 373 | * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a> |
| 374 | * |
| 375 | * Note: Use rawurldecode() so it does not remove plus signs |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 376 | */ |
Andrey Andreev | 4028234 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 377 | if (stripos($str, '%') !== false) |
Andrey Andreev | 29e1264 | 2014-02-10 13:24:44 +0200 | [diff] [blame] | 378 | { |
Andrey Andreev | 4028234 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 379 | do |
| 380 | { |
| 381 | $oldstr = $str; |
| 382 | $str = rawurldecode($str); |
| 383 | $str = preg_replace_callback('#%(?:\s*[0-9a-f]){2,}#i', array($this, '_urldecodespaces'), $str); |
| 384 | } |
| 385 | while ($oldstr !== $str); |
| 386 | unset($oldstr); |
Andrey Andreev | 29e1264 | 2014-02-10 13:24:44 +0200 | [diff] [blame] | 387 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 389 | /* |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 390 | * Convert character entities to ASCII |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 391 | * |
| 392 | * This permits our tests below to work reliably. |
| 393 | * We only convert entities that are within tags since |
| 394 | * these are the ones that will pose security problems. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 395 | */ |
Andrey Andreev | 9b8286c | 2014-08-05 11:46:57 +0300 | [diff] [blame] | 396 | $str = preg_replace_callback("/[^a-z0-9>]+[a-z0-9]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str); |
brian978 | 07ccbe5 | 2012-12-11 20:24:12 +0200 | [diff] [blame] | 397 | $str = preg_replace_callback('/<\w+.*/si', array($this, '_decode_entity'), $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 398 | |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 399 | // Remove Invisible Characters Again! |
Greg Aker | 757dda6 | 2010-04-14 19:06:19 -0500 | [diff] [blame] | 400 | $str = remove_invisible_characters($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 401 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 402 | /* |
| 403 | * Convert all tabs to spaces |
| 404 | * |
| 405 | * This prevents strings like this: ja vascript |
| 406 | * NOTE: we deal with spaces between characters later. |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 407 | * NOTE: preg_replace was found to be amazingly slow here on |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 408 | * large blocks of data, so we use str_replace. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 409 | */ |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 410 | $str = str_replace("\t", ' ', $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | |
Andrey Andreev | 4562f2c | 2012-01-09 23:39:50 +0200 | [diff] [blame] | 412 | // Capture converted string for later comparison |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 413 | $converted_string = $str; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 414 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 415 | // Remove Strings that are never allowed |
| 416 | $str = $this->_do_never_allowed($str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * Makes PHP tags safe |
| 420 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 421 | * Note: XML tags are inadvertently replaced too: |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 422 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 423 | * <?xml |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 424 | * |
| 425 | * But it doesn't seem to pose a problem. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 426 | */ |
| 427 | if ($is_image === TRUE) |
| 428 | { |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 429 | // Images have a tendency to have the PHP short opening and |
| 430 | // closing tags every so often so we skip those and only |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 431 | // do the long opening tags. |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 432 | $str = preg_replace('/<\?(php)/i', '<?\\1', $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 433 | } |
| 434 | else |
| 435 | { |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 436 | $str = str_replace(array('<?', '?'.'>'), array('<?', '?>'), $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 437 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 439 | /* |
| 440 | * Compact any exploded words |
| 441 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 442 | * 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] | 443 | * These words are compacted back to their correct state. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 444 | */ |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 445 | $words = array( |
Andrey Andreev | a30a717 | 2014-02-10 09:17:25 +0200 | [diff] [blame] | 446 | 'javascript', 'expression', 'vbscript', 'jscript', 'wscript', |
| 447 | 'vbs', 'script', 'base64', 'applet', 'alert', 'document', |
Andrey Andreev | 2a2578b | 2015-09-14 11:16:33 +0300 | [diff] [blame] | 448 | 'write', 'cookie', 'window', 'confirm', 'prompt', 'eval' |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 449 | ); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 450 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 451 | foreach ($words as $word) |
| 452 | { |
Andrey Andreev | 67ccdc0 | 2012-02-27 23:57:58 +0200 | [diff] [blame] | 453 | $word = implode('\s*', str_split($word)).'\s*'; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 454 | |
| 455 | // We only want to do this when it is followed by a non-word character |
| 456 | // That way valid stuff like "dealer to" does not become "dealerto" |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 457 | $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] | 458 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 460 | /* |
| 461 | * Remove disallowed Javascript in links or img tags |
Andrey Andreev | f9a615a | 2014-09-28 20:24:06 +0300 | [diff] [blame] | 462 | * We used to do some version comparisons and use of stripos(), |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 463 | * but it is dog slow compared to these simplified non-capturing |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 464 | * preg_match(), especially if the pattern exists in the string |
Andrey Andreev | 12445ca | 2014-01-25 01:55:52 +0200 | [diff] [blame] | 465 | * |
| 466 | * Note: It was reported that not only space characters, but all in |
| 467 | * the following pattern can be parsed as separators between a tag name |
| 468 | * and its attributes: [\d\s"\'`;,\/\=\(\x00\x0B\x09\x0C] |
| 469 | * ... however, remove_invisible_characters() above already strips the |
| 470 | * hex-encoded ones, so we'll skip them below. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 471 | */ |
| 472 | do |
| 473 | { |
| 474 | $original = $str; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 475 | |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 476 | if (preg_match('/<a/i', $str)) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 477 | { |
Andrey Andreev | 4028234 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 478 | $str = preg_replace_callback('#<a(?:rea)?[^a-z0-9>]+([^>]*?)(?:>|$)#si', array($this, '_js_link_removal'), $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 479 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 480 | |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 481 | if (preg_match('/<img/i', $str)) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 482 | { |
Andrey Andreev | ebb3aa0 | 2014-03-18 19:18:19 +0200 | [diff] [blame] | 483 | $str = preg_replace_callback('#<img[^a-z0-9]+([^>]*?)(?:\s?/?>|$)#si', array($this, '_js_img_removal'), $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 484 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
vlakoff | a81f60c | 2012-07-02 15:20:11 +0200 | [diff] [blame] | 486 | if (preg_match('/script|xss/i', $str)) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 487 | { |
vlakoff | a81f60c | 2012-07-02 15:20:11 +0200 | [diff] [blame] | 488 | $str = preg_replace('#</*(?:script|xss).*?>#si', '[removed]', $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 489 | } |
| 490 | } |
vlakoff | a81f60c | 2012-07-02 15:20:11 +0200 | [diff] [blame] | 491 | while ($original !== $str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 492 | unset($original); |
| 493 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 494 | /* |
| 495 | * Sanitize naughty HTML elements |
| 496 | * |
| 497 | * If a tag containing any of the words in the list |
| 498 | * below is found, the tag gets converted to entities. |
| 499 | * |
| 500 | * So this: <blink> |
| 501 | * Becomes: <blink> |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 502 | */ |
Andrey Andreev | bc78748 | 2015-09-11 18:11:32 +0300 | [diff] [blame] | 503 | $pattern = '#' |
Andrey Andreev | 2ab1c19 | 2017-01-04 15:26:35 +0200 | [diff] [blame] | 504 | .'<((?<slash>/*\s*)((?<tagName>[a-z0-9]+)(?=[^a-z0-9]|$)|.+)' // tag start and name, followed by a non-tag character |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 505 | .'[^\s\042\047a-z0-9>/=]*' // a valid attribute character immediately after the tag would count as a separator |
Andrey Andreev | bc78748 | 2015-09-11 18:11:32 +0300 | [diff] [blame] | 506 | // optional attributes |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 507 | .'(?<attributes>(?:[\s\042\047/=]*' // non-attribute characters, excluding > (tag close) for obvious reasons |
Andrey Andreev | bc78748 | 2015-09-11 18:11:32 +0300 | [diff] [blame] | 508 | .'[^\s\042\047>/=]+' // attribute characters |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 509 | // optional attribute-value |
Andrey Andreev | f0f47da | 2015-10-05 12:37:16 +0300 | [diff] [blame] | 510 | .'(?:\s*=' // attribute-value separator |
| 511 | .'(?:[^\s\042\047=><`]+|\s*\042[^\042]*\042|\s*\047[^\047]*\047|\s*(?U:[^\s\042\047=><`]*))' // single, double or non-quoted value |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 512 | .')?' // end optional attribute-value group |
| 513 | .')*)' // end optional attributes group |
Andrey Andreev | 088e57d | 2015-09-17 15:55:57 +0300 | [diff] [blame] | 514 | .'[^>]*)(?<closeTag>\>)?#isS'; |
Andrey Andreev | bc78748 | 2015-09-11 18:11:32 +0300 | [diff] [blame] | 515 | |
| 516 | // Note: It would be nice to optimize this for speed, BUT |
| 517 | // only matching the naughty elements here results in |
| 518 | // false positives and in turn - vulnerabilities! |
| 519 | do |
| 520 | { |
| 521 | $old_str = $str; |
| 522 | $str = preg_replace_callback($pattern, array($this, '_sanitize_naughty_html'), $str); |
| 523 | } |
| 524 | while ($old_str !== $str); |
| 525 | unset($old_str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 526 | |
| 527 | /* |
| 528 | * Sanitize naughty scripting elements |
| 529 | * |
| 530 | * Similar to above, only instead of looking for |
| 531 | * tags it looks for PHP and JavaScript commands |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 532 | * that are disallowed. Rather than removing the |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 533 | * code, it simply converts the parenthesis to entities |
| 534 | * rendering the code un-executable. |
| 535 | * |
| 536 | * For example: eval('some code') |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 537 | * Becomes: eval('some code') |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 538 | */ |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 539 | $str = preg_replace( |
| 540 | '#(alert|prompt|confirm|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', |
| 541 | '\\1\\2(\\3)', |
| 542 | $str |
| 543 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 544 | |
Andrey Andreev | 3d204b6 | 2018-03-10 02:40:22 +0200 | [diff] [blame^] | 545 | // Same thing, but for "tag functions" (e.g. eval`some code`) |
| 546 | // See https://github.com/bcit-ci/CodeIgniter/issues/5420 |
| 547 | $str = preg_replace( |
| 548 | '#(alert|prompt|confirm|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)`(.*?)`#si', |
| 549 | '\\1\\2`\\3`', |
| 550 | $str |
| 551 | ); |
| 552 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 553 | // Final clean up |
| 554 | // This adds a bit of extra precaution in case |
| 555 | // something got through the above filters |
| 556 | $str = $this->_do_never_allowed($str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 557 | |
| 558 | /* |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 559 | * Images are Handled in a Special Way |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 560 | * - Essentially, we want to know that after all of the character |
| 561 | * conversion is done whether any unwanted, likely XSS, code was found. |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 562 | * If not, we return TRUE, as the image is clean. |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 563 | * However, if the string post-conversion does not matched the |
| 564 | * 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] | 565 | * code found and removed/changed during processing. |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 566 | */ |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 567 | if ($is_image === TRUE) |
| 568 | { |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 569 | return ($str === $converted_string); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 570 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 571 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 572 | return $str; |
| 573 | } |
| 574 | |
| 575 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 576 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 577 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 578 | * XSS Hash |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 579 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 580 | * Generates the XSS hash if needed and returns it. |
| 581 | * |
| 582 | * @see CI_Security::$_xss_hash |
| 583 | * @return string XSS hash |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 584 | */ |
Eric Barnes | 9805ecc | 2011-01-16 23:35:16 -0500 | [diff] [blame] | 585 | public function xss_hash() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 586 | { |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 587 | if ($this->_xss_hash === NULL) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 588 | { |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 589 | $rand = $this->get_random_bytes(16); |
| 590 | $this->_xss_hash = ($rand === FALSE) |
| 591 | ? md5(uniqid(mt_rand(), TRUE)) |
| 592 | : bin2hex($rand); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 593 | } |
| 594 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 595 | return $this->_xss_hash; |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | // -------------------------------------------------------------------- |
| 599 | |
| 600 | /** |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 601 | * Get random bytes |
| 602 | * |
| 603 | * @param int $length Output length |
| 604 | * @return string |
| 605 | */ |
| 606 | public function get_random_bytes($length) |
| 607 | { |
Andrey Andreev | efe33a2 | 2014-08-28 09:53:44 +0300 | [diff] [blame] | 608 | if (empty($length) OR ! ctype_digit((string) $length)) |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 609 | { |
| 610 | return FALSE; |
| 611 | } |
| 612 | |
Andrey Andreev | 5afa348 | 2015-11-24 11:48:39 +0200 | [diff] [blame] | 613 | if (function_exists('random_bytes')) |
| 614 | { |
| 615 | try |
| 616 | { |
| 617 | // The cast is required to avoid TypeError |
| 618 | return random_bytes((int) $length); |
| 619 | } |
| 620 | catch (Exception $e) |
| 621 | { |
| 622 | // If random_bytes() can't do the job, we can't either ... |
| 623 | // There's no point in using fallbacks. |
| 624 | log_message('error', $e->getMessage()); |
| 625 | return FALSE; |
| 626 | } |
| 627 | } |
| 628 | |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 629 | // Unfortunately, none of the following PRNGs is guaranteed to exist ... |
Andrey Andreev | 607d5e2 | 2014-09-17 14:54:05 +0300 | [diff] [blame] | 630 | if (defined('MCRYPT_DEV_URANDOM') && ($output = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM)) !== FALSE) |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 631 | { |
| 632 | return $output; |
| 633 | } |
| 634 | |
| 635 | |
| 636 | if (is_readable('/dev/urandom') && ($fp = fopen('/dev/urandom', 'rb')) !== FALSE) |
| 637 | { |
Andrey Andreev | e4b9cd6 | 2014-10-02 02:19:06 +0300 | [diff] [blame] | 638 | // Try not to waste entropy ... |
| 639 | is_php('5.4') && stream_set_chunk_size($fp, $length); |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 640 | $output = fread($fp, $length); |
| 641 | fclose($fp); |
| 642 | if ($output !== FALSE) |
| 643 | { |
| 644 | return $output; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | if (function_exists('openssl_random_pseudo_bytes')) |
| 649 | { |
| 650 | return openssl_random_pseudo_bytes($length); |
| 651 | } |
| 652 | |
| 653 | return FALSE; |
| 654 | } |
| 655 | |
| 656 | // -------------------------------------------------------------------- |
| 657 | |
| 658 | /** |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 659 | * HTML Entities Decode |
| 660 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 661 | * A replacement for html_entity_decode() |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 662 | * |
Pascal Kriete | c38e3b6 | 2011-11-14 13:55:00 -0500 | [diff] [blame] | 663 | * The reason we are not using html_entity_decode() by itself is because |
| 664 | * while it is not technically correct to leave out the semicolon |
| 665 | * 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] | 666 | * correctly. html_entity_decode() does not convert entities without |
Pascal Kriete | c38e3b6 | 2011-11-14 13:55:00 -0500 | [diff] [blame] | 667 | * semicolons, so we are left with our own little solution here. Bummer. |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 668 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 669 | * @link http://php.net/html-entity-decode |
| 670 | * |
| 671 | * @param string $str Input |
| 672 | * @param string $charset Character set |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 673 | * @return string |
| 674 | */ |
freewil | 8cc0cfe | 2011-08-27 21:53:00 -0400 | [diff] [blame] | 675 | public function entity_decode($str, $charset = NULL) |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 676 | { |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 677 | if (strpos($str, '&') === FALSE) |
freewil | 5c9b0d1 | 2011-08-28 12:15:23 -0400 | [diff] [blame] | 678 | { |
| 679 | return $str; |
| 680 | } |
Andrey Andreev | 3d113bd | 2011-10-05 00:03:20 +0300 | [diff] [blame] | 681 | |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 682 | static $_entities; |
| 683 | |
| 684 | isset($charset) OR $charset = $this->charset; |
| 685 | $flag = is_php('5.4') |
| 686 | ? ENT_COMPAT | ENT_HTML5 |
| 687 | : ENT_COMPAT; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 688 | |
Andrey Andreev | eea02de | 2016-09-27 14:59:37 +0300 | [diff] [blame] | 689 | if ( ! isset($_entities)) |
| 690 | { |
| 691 | $_entities = array_map('strtolower', get_html_translation_table(HTML_ENTITIES, $flag, $charset)); |
| 692 | |
| 693 | // If we're not on PHP 5.4+, add the possibly dangerous HTML 5 |
| 694 | // entities to the array manually |
| 695 | if ($flag === ENT_COMPAT) |
| 696 | { |
| 697 | $_entities[':'] = ':'; |
| 698 | $_entities['('] = '('; |
| 699 | $_entities[')'] = ')'; |
| 700 | $_entities["\n"] = '
'; |
| 701 | $_entities["\t"] = '	'; |
| 702 | } |
| 703 | } |
| 704 | |
brian978 | 638a9d2 | 2012-12-18 13:25:54 +0200 | [diff] [blame] | 705 | do |
| 706 | { |
Andrey Andreev | e7a2aa0 | 2014-03-18 18:44:53 +0200 | [diff] [blame] | 707 | $str_compare = $str; |
brian978 | 07ccbe5 | 2012-12-11 20:24:12 +0200 | [diff] [blame] | 708 | |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 709 | // Decode standard entities, avoiding false positives |
Andrey Andreev | 074a214 | 2015-02-09 14:03:14 +0200 | [diff] [blame] | 710 | if (preg_match_all('/&[a-z]{2,}(?![a-z;])/i', $str, $matches)) |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 711 | { |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 712 | $replace = array(); |
| 713 | $matches = array_unique(array_map('strtolower', $matches[0])); |
Andrey Andreev | 068ab20 | 2015-03-26 21:03:38 +0200 | [diff] [blame] | 714 | foreach ($matches as &$match) |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 715 | { |
Andrey Andreev | 068ab20 | 2015-03-26 21:03:38 +0200 | [diff] [blame] | 716 | if (($char = array_search($match.';', $_entities, TRUE)) !== FALSE) |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 717 | { |
Andrey Andreev | 068ab20 | 2015-03-26 21:03:38 +0200 | [diff] [blame] | 718 | $replace[$match] = $char; |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
Andrey Andreev | eea02de | 2016-09-27 14:59:37 +0300 | [diff] [blame] | 722 | $str = str_replace(array_keys($replace), array_values($replace), $str); |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // Decode numeric & UTF16 two byte entities |
| 726 | $str = html_entity_decode( |
warpcode | bb17798 | 2014-12-16 11:29:53 +0000 | [diff] [blame] | 727 | preg_replace('/(&#(?:x0*[0-9a-f]{2,5}(?![0-9a-f;])|(?:0*\d{2,4}(?![0-9;]))))/iS', '$1;', $str), |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 728 | $flag, |
| 729 | $charset |
| 730 | ); |
Andrey Andreev | eea02de | 2016-09-27 14:59:37 +0300 | [diff] [blame] | 731 | |
| 732 | if ($flag === ENT_COMPAT) |
| 733 | { |
| 734 | $str = str_replace(array_values($_entities), array_keys($_entities), $str); |
| 735 | } |
brian978 | 638a9d2 | 2012-12-18 13:25:54 +0200 | [diff] [blame] | 736 | } |
Andrey Andreev | e7a2aa0 | 2014-03-18 18:44:53 +0200 | [diff] [blame] | 737 | while ($str_compare !== $str); |
brian978 | 638a9d2 | 2012-12-18 13:25:54 +0200 | [diff] [blame] | 738 | return $str; |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 739 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 740 | |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 741 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 742 | |
Derek Jones | a091147 | 2010-03-30 10:33:09 -0500 | [diff] [blame] | 743 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 744 | * Sanitize Filename |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 745 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 746 | * @param string $str Input file name |
| 747 | * @param bool $relative_path Whether to preserve paths |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 748 | * @return string |
| 749 | */ |
Hunter Wu | 8df3352 | 2013-08-03 22:36:05 +0800 | [diff] [blame] | 750 | public function sanitize_filename($str, $relative_path = FALSE) |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 751 | { |
Hunter Wu | 4495cc7 | 2013-08-04 12:31:52 +0800 | [diff] [blame] | 752 | $bad = $this->filename_bad_chars; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 753 | |
Derek Jones | 2ef3759 | 2010-10-06 17:51:59 -0500 | [diff] [blame] | 754 | if ( ! $relative_path) |
| 755 | { |
| 756 | $bad[] = './'; |
| 757 | $bad[] = '/'; |
| 758 | } |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 759 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 760 | $str = remove_invisible_characters($str, FALSE); |
Andrey Andreev | 7e55977 | 2013-01-29 15:38:33 +0200 | [diff] [blame] | 761 | |
| 762 | do |
| 763 | { |
| 764 | $old = $str; |
| 765 | $str = str_replace($bad, '', $str); |
| 766 | } |
| 767 | while ($old !== $str); |
| 768 | |
| 769 | return stripslashes($str); |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 770 | } |
| 771 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 772 | // ---------------------------------------------------------------- |
| 773 | |
| 774 | /** |
Andrey Andreev | 1a24a9d | 2012-06-27 00:52:47 +0300 | [diff] [blame] | 775 | * Strip Image Tags |
| 776 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 777 | * @param string $str |
Andrey Andreev | 1a24a9d | 2012-06-27 00:52:47 +0300 | [diff] [blame] | 778 | * @return string |
| 779 | */ |
| 780 | public function strip_image_tags($str) |
| 781 | { |
Andrey Andreev | 3c0d8da | 2016-03-07 10:52:15 +0200 | [diff] [blame] | 782 | return preg_replace( |
| 783 | array( |
| 784 | '#<img[\s/]+.*?src\s*=\s*(["\'])([^\\1]+?)\\1.*?\>#i', |
| 785 | '#<img[\s/]+.*?src\s*=\s*?(([^\s"\'=<>`]+)).*?\>#i' |
| 786 | ), |
| 787 | '\\2', |
| 788 | $str |
| 789 | ); |
Andrey Andreev | 1a24a9d | 2012-06-27 00:52:47 +0300 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | // ---------------------------------------------------------------- |
| 793 | |
| 794 | /** |
Andrey Andreev | 4028234 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 795 | * URL-decode taking spaces into account |
| 796 | * |
| 797 | * @see https://github.com/bcit-ci/CodeIgniter/issues/4877 |
| 798 | * @param array $matches |
| 799 | * @return string |
| 800 | */ |
| 801 | protected function _urldecodespaces($matches) |
| 802 | { |
| 803 | $input = $matches[0]; |
| 804 | $nospaces = preg_replace('#\s+#', '', $input); |
| 805 | return ($nospaces === $input) |
| 806 | ? $input |
| 807 | : rawurldecode($nospaces); |
| 808 | } |
| 809 | |
| 810 | // ---------------------------------------------------------------- |
| 811 | |
| 812 | /** |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 813 | * Compact Exploded Words |
| 814 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 815 | * Callback method for xss_clean() to remove whitespace from |
| 816 | * things like 'j a v a s c r i p t'. |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 817 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 818 | * @used-by CI_Security::xss_clean() |
| 819 | * @param array $matches |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 820 | * @return string |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 821 | */ |
| 822 | protected function _compact_exploded_words($matches) |
| 823 | { |
| 824 | return preg_replace('/\s+/s', '', $matches[1]).$matches[2]; |
| 825 | } |
| 826 | |
| 827 | // -------------------------------------------------------------------- |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 828 | |
Timothy Warren | ad47505 | 2012-04-19 13:21:06 -0400 | [diff] [blame] | 829 | /** |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 830 | * Sanitize Naughty HTML |
| 831 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 832 | * Callback method for xss_clean() to remove naughty HTML elements. |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 833 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 834 | * @used-by CI_Security::xss_clean() |
| 835 | * @param array $matches |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 836 | * @return string |
| 837 | */ |
| 838 | protected function _sanitize_naughty_html($matches) |
| 839 | { |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 840 | static $naughty_tags = array( |
Andrey Andreev | 4028234 | 2016-10-26 17:41:18 +0300 | [diff] [blame] | 841 | 'alert', 'area', 'prompt', 'confirm', 'applet', 'audio', 'basefont', 'base', 'behavior', 'bgsound', |
Andrey Andreev | bc78748 | 2015-09-11 18:11:32 +0300 | [diff] [blame] | 842 | 'blink', 'body', 'embed', 'expression', 'form', 'frameset', 'frame', 'head', 'html', 'ilayer', |
| 843 | 'iframe', 'input', 'button', 'select', 'isindex', 'layer', 'link', 'meta', 'keygen', 'object', |
| 844 | 'plaintext', 'style', 'script', 'textarea', 'title', 'math', 'video', 'svg', 'xml', 'xss' |
| 845 | ); |
| 846 | |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 847 | static $evil_attributes = array( |
| 848 | 'on\w+', 'style', 'xmlns', 'formaction', 'form', 'xlink:href', 'FSCommand', 'seekSegmentTime' |
| 849 | ); |
| 850 | |
Andrey Andreev | 088e57d | 2015-09-17 15:55:57 +0300 | [diff] [blame] | 851 | // First, escape unclosed tags |
| 852 | if (empty($matches['closeTag'])) |
| 853 | { |
| 854 | return '<'.$matches[1]; |
| 855 | } |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 856 | // Is the element that we caught naughty? If so, escape it |
Andrey Andreev | 088e57d | 2015-09-17 15:55:57 +0300 | [diff] [blame] | 857 | elseif (in_array(strtolower($matches['tagName']), $naughty_tags, TRUE)) |
Andrey Andreev | bc78748 | 2015-09-11 18:11:32 +0300 | [diff] [blame] | 858 | { |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 859 | return '<'.$matches[1].'>'; |
| 860 | } |
| 861 | // For other tags, see if their attributes are "evil" and strip those |
| 862 | elseif (isset($matches['attributes'])) |
| 863 | { |
Andrey Andreev | 71b1b3f | 2015-10-27 12:30:18 +0200 | [diff] [blame] | 864 | // We'll store the already fitlered attributes here |
| 865 | $attributes = array(); |
| 866 | |
| 867 | // Attribute-catching pattern |
| 868 | $attributes_pattern = '#' |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 869 | .'(?<name>[^\s\042\047>/=]+)' // attribute characters |
| 870 | // optional attribute-value |
Andrey Andreev | f0f47da | 2015-10-05 12:37:16 +0300 | [diff] [blame] | 871 | .'(?:\s*=(?<value>[^\s\042\047=><`]+|\s*\042[^\042]*\042|\s*\047[^\047]*\047|\s*(?U:[^\s\042\047=><`]*)))' // attribute-value separator |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 872 | .'#i'; |
| 873 | |
Andrey Andreev | 71b1b3f | 2015-10-27 12:30:18 +0200 | [diff] [blame] | 874 | // Blacklist pattern for evil attribute names |
| 875 | $is_evil_pattern = '#^('.implode('|', $evil_attributes).')$#i'; |
| 876 | |
| 877 | // Each iteration filters a single attribute |
| 878 | do |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 879 | { |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 880 | // Strip any non-alpha characters that may precede an attribute. |
Andrey Andreev | 71b1b3f | 2015-10-27 12:30:18 +0200 | [diff] [blame] | 881 | // Browsers often parse these incorrectly and that has been a |
| 882 | // of numerous XSS issues we've had. |
| 883 | $matches['attributes'] = preg_replace('#^[^a-z]+#i', '', $matches['attributes']); |
| 884 | |
| 885 | if ( ! preg_match($attributes_pattern, $matches['attributes'], $attribute, PREG_OFFSET_CAPTURE)) |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 886 | { |
Andrey Andreev | 71b1b3f | 2015-10-27 12:30:18 +0200 | [diff] [blame] | 887 | // No (valid) attribute found? Discard everything else inside the tag |
| 888 | break; |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 889 | } |
| 890 | |
Andrey Andreev | 71b1b3f | 2015-10-27 12:30:18 +0200 | [diff] [blame] | 891 | if ( |
| 892 | // Is it indeed an "evil" attribute? |
| 893 | preg_match($is_evil_pattern, $attribute['name'][0]) |
| 894 | // Or does it have an equals sign, but no value and not quoted? Strip that too! |
| 895 | OR (trim($attribute['value'][0]) === '') |
| 896 | ) |
| 897 | { |
| 898 | $attributes[] = 'xss=removed'; |
| 899 | } |
| 900 | else |
| 901 | { |
| 902 | $attributes[] = $attribute[0][0]; |
| 903 | } |
| 904 | |
| 905 | $matches['attributes'] = substr($matches['attributes'], $attribute[0][1] + strlen($attribute[0][0])); |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 906 | } |
Andrey Andreev | 71b1b3f | 2015-10-27 12:30:18 +0200 | [diff] [blame] | 907 | while ($matches['attributes'] !== ''); |
| 908 | |
| 909 | $attributes = empty($attributes) |
| 910 | ? '' |
| 911 | : ' '.implode(' ', $attributes); |
| 912 | return '<'.$matches['slash'].$matches['tagName'].$attributes.'>'; |
Andrey Andreev | bc78748 | 2015-09-11 18:11:32 +0300 | [diff] [blame] | 913 | } |
| 914 | |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 915 | return $matches[0]; |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | // -------------------------------------------------------------------- |
| 919 | |
| 920 | /** |
| 921 | * JS Link Removal |
| 922 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 923 | * Callback method for xss_clean() to sanitize links. |
| 924 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 925 | * This limits the PCRE backtracks, making it more performance friendly |
| 926 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 927 | * PHP 5.2+ on link-heavy strings. |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 928 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 929 | * @used-by CI_Security::xss_clean() |
| 930 | * @param array $match |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 931 | * @return string |
| 932 | */ |
| 933 | protected function _js_link_removal($match) |
| 934 | { |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 935 | return str_replace( |
| 936 | $match[1], |
| 937 | preg_replace( |
Andrey Andreev | 3d204b6 | 2018-03-10 02:40:22 +0200 | [diff] [blame^] | 938 | '#href=.*?(?:(?:alert|prompt|confirm)(?:\(|&\#40;|`|&\#96;)|javascript:|livescript:|mocha:|charset=|window\.|document\.|\.cookie|<script|<xss|d\s*a\s*t\s*a\s*:)#si', |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 939 | '', |
Andrey Andreev | 4fbf2d1 | 2015-09-21 16:17:48 +0300 | [diff] [blame] | 940 | $this->_filter_attributes($match[1]) |
Andrey Andreev | 3ceb14a | 2015-09-17 15:03:03 +0300 | [diff] [blame] | 941 | ), |
| 942 | $match[0] |
| 943 | ); |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | // -------------------------------------------------------------------- |
| 947 | |
| 948 | /** |
| 949 | * JS Image Removal |
| 950 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 951 | * Callback method for xss_clean() to sanitize image tags. |
| 952 | * |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 953 | * This limits the PCRE backtracks, making it more performance friendly |
| 954 | * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 955 | * PHP 5.2+ on image tag heavy strings. |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 956 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 957 | * @used-by CI_Security::xss_clean() |
| 958 | * @param array $match |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 959 | * @return string |
| 960 | */ |
| 961 | protected function _js_img_removal($match) |
| 962 | { |
Andrey Andreev | 2a2578b | 2015-09-14 11:16:33 +0300 | [diff] [blame] | 963 | return str_replace( |
| 964 | $match[1], |
| 965 | preg_replace( |
Andrey Andreev | 3d204b6 | 2018-03-10 02:40:22 +0200 | [diff] [blame^] | 966 | '#src=.*?(?:(?:alert|prompt|confirm|eval)(?:\(|&\#40;|`|&\#96;)|javascript:|livescript:|mocha:|charset=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si', |
Andrey Andreev | 2a2578b | 2015-09-14 11:16:33 +0300 | [diff] [blame] | 967 | '', |
Andrey Andreev | 4fbf2d1 | 2015-09-21 16:17:48 +0300 | [diff] [blame] | 968 | $this->_filter_attributes($match[1]) |
Andrey Andreev | 2a2578b | 2015-09-14 11:16:33 +0300 | [diff] [blame] | 969 | ), |
| 970 | $match[0] |
| 971 | ); |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | // -------------------------------------------------------------------- |
| 975 | |
| 976 | /** |
| 977 | * Attribute Conversion |
| 978 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 979 | * @used-by CI_Security::xss_clean() |
| 980 | * @param array $match |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 981 | * @return string |
| 982 | */ |
| 983 | protected function _convert_attribute($match) |
| 984 | { |
| 985 | return str_replace(array('>', '<', '\\'), array('>', '<', '\\\\'), $match[0]); |
| 986 | } |
| 987 | |
| 988 | // -------------------------------------------------------------------- |
| 989 | |
| 990 | /** |
| 991 | * Filter Attributes |
| 992 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 993 | * Filters tag attributes for consistency and safety. |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 994 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 995 | * @used-by CI_Security::_js_img_removal() |
| 996 | * @used-by CI_Security::_js_link_removal() |
| 997 | * @param string $str |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 998 | * @return string |
| 999 | */ |
| 1000 | protected function _filter_attributes($str) |
| 1001 | { |
| 1002 | $out = ''; |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1003 | if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches)) |
| 1004 | { |
| 1005 | foreach ($matches[0] as $match) |
| 1006 | { |
Andrey Andreev | 4562f2c | 2012-01-09 23:39:50 +0200 | [diff] [blame] | 1007 | $out .= preg_replace('#/\*.*?\*/#s', '', $match); |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | return $out; |
| 1012 | } |
| 1013 | |
| 1014 | // -------------------------------------------------------------------- |
| 1015 | |
| 1016 | /** |
| 1017 | * HTML Entity Decode Callback |
| 1018 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 1019 | * @used-by CI_Security::xss_clean() |
| 1020 | * @param array $match |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1021 | * @return string |
| 1022 | */ |
| 1023 | protected function _decode_entity($match) |
| 1024 | { |
Andrey Andreev | 487d1ae | 2014-05-23 14:41:32 +0300 | [diff] [blame] | 1025 | // Protect GET variables in URLs |
| 1026 | // 901119URL5918AMP18930PROTECT8198 |
| 1027 | $match = preg_replace('|\&([a-z\_0-9\-]+)\=([a-z\_0-9\-/]+)|i', $this->xss_hash().'\\1=\\2', $match[0]); |
| 1028 | |
| 1029 | // Decode, then un-protect URL GET vars |
| 1030 | return str_replace( |
| 1031 | $this->xss_hash(), |
| 1032 | '&', |
| 1033 | $this->entity_decode($match, $this->charset) |
Andrey Andreev | c67c3fb | 2014-01-22 13:26:00 +0200 | [diff] [blame] | 1034 | ); |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | // -------------------------------------------------------------------- |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 1038 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1039 | /** |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1040 | * Do Never Allowed |
| 1041 | * |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 1042 | * @used-by CI_Security::xss_clean() |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1043 | * @param string |
| 1044 | * @return string |
| 1045 | */ |
| 1046 | protected function _do_never_allowed($str) |
| 1047 | { |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 1048 | $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] | 1049 | |
Andrey Andreev | bb488dc | 2012-01-07 23:35:16 +0200 | [diff] [blame] | 1050 | foreach ($this->_never_allowed_regex as $regex) |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1051 | { |
Wes Baker | 5335bc3 | 2012-04-24 15:17:14 -0400 | [diff] [blame] | 1052 | $str = preg_replace('#'.$regex.'#is', '[removed]', $str); |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1053 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 1054 | |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1055 | return $str; |
| 1056 | } |
| 1057 | |
| 1058 | // -------------------------------------------------------------------- |
| 1059 | |
| 1060 | /** |
Andrey Andreev | 6435410 | 2012-10-28 14:16:02 +0200 | [diff] [blame] | 1061 | * Set CSRF Hash and Cookie |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1062 | * |
| 1063 | * @return string |
| 1064 | */ |
| 1065 | protected function _csrf_set_hash() |
| 1066 | { |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 1067 | if ($this->_csrf_hash === NULL) |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1068 | { |
vlakoff | 3a3d5f6 | 2013-10-17 22:22:16 +0200 | [diff] [blame] | 1069 | // If the cookie exists we will use its value. |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1070 | // We don't necessarily want to regenerate it with |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 1071 | // each page load since a page could contain embedded |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1072 | // sub-pages causing this feature to fail |
Andrey Andreev | 162b1a9 | 2014-12-08 10:59:51 +0200 | [diff] [blame] | 1073 | if (isset($_COOKIE[$this->_csrf_cookie_name]) && is_string($_COOKIE[$this->_csrf_cookie_name]) |
| 1074 | && 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] | 1075 | { |
| 1076 | return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name]; |
| 1077 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 1078 | |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 1079 | $rand = $this->get_random_bytes(16); |
| 1080 | $this->_csrf_hash = ($rand === FALSE) |
| 1081 | ? md5(uniqid(mt_rand(), TRUE)) |
| 1082 | : bin2hex($rand); |
Pascal Kriete | c9c045a | 2011-04-05 14:50:41 -0400 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | return $this->_csrf_hash; |
| 1086 | } |
| 1087 | |
Derek Jones | e701d76 | 2010-03-02 18:17:01 -0600 | [diff] [blame] | 1088 | } |