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