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