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