blob: 50d0ce0520560677a0842957713a1e684387f032 [file] [log] [blame]
Andrey Andreevbb488dc2012-01-07 23:35:16 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Jonese701d762010-03-02 18:17:01 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Jonese701d762010-03-02 18:17:01 -06006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevbb488dc2012-01-07 23:35:16 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevbb488dc2012-01-07 23:35:16 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Jonese701d762010-03-02 18:17:01 -060019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Jonese701d762010-03-02 18:17:01 -060023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Jonese701d762010-03-02 18:17:01 -060028/**
29 * Security Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Security
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Pascal Krietec9c045a2011-04-05 14:50:41 -040035 * @link http://codeigniter.com/user_guide/libraries/security.html
Derek Jonese701d762010-03-02 18:17:01 -060036 */
37class CI_Security {
Barry Mienydd671972010-10-04 16:33:58 +020038
David Behler07b53422011-08-15 00:25:06 +020039 /**
Andrey Andreev64354102012-10-28 14:16:02 +020040 * XSS Hash
David Behler07b53422011-08-15 00:25:06 +020041 *
Andrey Andreev64354102012-10-28 14:16:02 +020042 * Random Hash for protecting URLs.
43 *
44 * @var string
David Behler07b53422011-08-15 00:25:06 +020045 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040046 protected $_xss_hash = '';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030047
David Behler07b53422011-08-15 00:25:06 +020048 /**
Andrey Andreev64354102012-10-28 14:16:02 +020049 * CSRF Hash
David Behler07b53422011-08-15 00:25:06 +020050 *
Andrey Andreev64354102012-10-28 14:16:02 +020051 * Random hash for Cross Site Request Forgery protection cookie
52 *
53 * @var string
David Behler07b53422011-08-15 00:25:06 +020054 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040055 protected $_csrf_hash = '';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030056
David Behler07b53422011-08-15 00:25:06 +020057 /**
Andrey Andreev64354102012-10-28 14:16:02 +020058 * CSRF Expire time
David Behler07b53422011-08-15 00:25:06 +020059 *
Andrey Andreev64354102012-10-28 14:16:02 +020060 * Expiration time for Cross Site Request Forgery protection cookie.
61 * Defaults to two hours (in seconds).
62 *
63 * @var int
David Behler07b53422011-08-15 00:25:06 +020064 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040065 protected $_csrf_expire = 7200;
Andrey Andreev3d113bd2011-10-05 00:03:20 +030066
David Behler07b53422011-08-15 00:25:06 +020067 /**
Andrey Andreev64354102012-10-28 14:16:02 +020068 * CSRF Token name
David Behler07b53422011-08-15 00:25:06 +020069 *
Andrey Andreev64354102012-10-28 14:16:02 +020070 * Token name for Cross Site Request Forgery protection cookie.
71 *
72 * @var string
David Behler07b53422011-08-15 00:25:06 +020073 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040074 protected $_csrf_token_name = 'ci_csrf_token';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030075
David Behler07b53422011-08-15 00:25:06 +020076 /**
Andrey Andreev64354102012-10-28 14:16:02 +020077 * CSRF Cookie name
David Behler07b53422011-08-15 00:25:06 +020078 *
Andrey Andreev64354102012-10-28 14:16:02 +020079 * Cookie name for Cross Site Request Forgery protection cookie.
80 *
81 * @var string
David Behler07b53422011-08-15 00:25:06 +020082 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040083 protected $_csrf_cookie_name = 'ci_csrf_token';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030084
David Behler07b53422011-08-15 00:25:06 +020085 /**
86 * List of never allowed strings
87 *
Andrey Andreev64354102012-10-28 14:16:02 +020088 * @var array
David Behler07b53422011-08-15 00:25:06 +020089 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040090 protected $_never_allowed_str = array(
Timothy Warren40403d22012-04-19 16:38:50 -040091 'document.cookie' => '[removed]',
92 'document.write' => '[removed]',
93 '.parentNode' => '[removed]',
94 '.innerHTML' => '[removed]',
95 'window.location' => '[removed]',
96 '-moz-binding' => '[removed]',
97 '<!--' => '&lt;!--',
98 '-->' => '--&gt;',
99 '<![CDATA[' => '&lt;![CDATA[',
100 '<comment>' => '&lt;comment&gt;'
101 );
Derek Jonese701d762010-03-02 18:17:01 -0600102
David Behler07b53422011-08-15 00:25:06 +0200103 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200104 * List of never allowed regex replacements
David Behler07b53422011-08-15 00:25:06 +0200105 *
Andrey Andreev64354102012-10-28 14:16:02 +0200106 * @var array
David Behler07b53422011-08-15 00:25:06 +0200107 */
Pascal Krietec9c045a2011-04-05 14:50:41 -0400108 protected $_never_allowed_regex = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400109 'javascript\s*:',
110 'expression\s*(\(|&\#40;)', // CSS and IE
111 'vbscript\s*:', // IE, surprise!
Wes Bakerd3481352012-05-07 16:49:33 -0400112 'Redirect\s+302',
113 "([\"'])?data\s*:[^\\1]*?base64[^\\1]*?,[^\\1]*?\\1?"
Timothy Warren40403d22012-04-19 16:38:50 -0400114 );
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300115
Timothy Warrenad475052012-04-19 13:21:06 -0400116 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200117 * Class constructor
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300118 *
119 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -0400120 */
Greg Akera9263282010-11-10 15:26:43 -0600121 public function __construct()
Derek Jonese701d762010-03-02 18:17:01 -0600122 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200123 // Is CSRF protection enabled?
124 if (config_item('csrf_protection') === TRUE)
patworkef1a55a2011-04-09 13:04:06 +0200125 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200126 // CSRF config
127 foreach (array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key)
patworkef1a55a2011-04-09 13:04:06 +0200128 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200129 if (FALSE !== ($val = config_item($key)))
130 {
131 $this->{'_'.$key} = $val;
132 }
patworkef1a55a2011-04-09 13:04:06 +0200133 }
patworkef1a55a2011-04-09 13:04:06 +0200134
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200135 // Append application specific cookie prefix
136 if (config_item('cookie_prefix'))
137 {
138 $this->_csrf_cookie_name = config_item('cookie_prefix').$this->_csrf_cookie_name;
139 }
Derek Jonesb3f10a22010-07-25 19:11:26 -0500140
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200141 // Set the CSRF hash
142 $this->_csrf_set_hash();
143 }
Derek Allard958543a2010-07-22 14:10:26 -0400144
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200145 log_message('debug', 'Security Class Initialized');
Derek Jonese701d762010-03-02 18:17:01 -0600146 }
147
148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Jonese701d762010-03-02 18:17:01 -0600150 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200151 * CSRF Verify
Derek Jonese701d762010-03-02 18:17:01 -0600152 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400153 * @return object
Derek Jonese701d762010-03-02 18:17:01 -0600154 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500155 public function csrf_verify()
Derek Allard958543a2010-07-22 14:10:26 -0400156 {
Andrey Andreev5d27c432012-03-08 12:01:52 +0200157 // If it's not a POST request we will set the CSRF cookie
158 if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST')
Derek Jonese701d762010-03-02 18:17:01 -0600159 {
160 return $this->csrf_set_cookie();
161 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300162
Alex Bilbieaeb2c3e2011-08-21 16:14:54 +0100163 // Check if URI has been whitelisted from CSRF checks
164 if ($exclude_uris = config_item('csrf_exclude_uris'))
165 {
166 $uri = load_class('URI', 'core');
167 if (in_array($uri->uri_string(), $exclude_uris))
168 {
169 return $this;
170 }
171 }
Derek Jonese701d762010-03-02 18:17:01 -0600172
173 // Do the tokens exist in both the _POST and _COOKIE arrays?
Andrey Andreevf795ab52012-10-24 21:28:25 +0300174 if ( ! isset($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name])
Alex Bilbieed944a32012-06-02 11:07:47 +0100175 OR $_POST[$this->_csrf_token_name] !== $_COOKIE[$this->_csrf_cookie_name]) // Do the tokens match?
Derek Jonese701d762010-03-02 18:17:01 -0600176 {
177 $this->csrf_show_error();
178 }
179
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200180 // We kill this since we're done and we don't want to polute the _POST array
Pascal Krietec9c045a2011-04-05 14:50:41 -0400181 unset($_POST[$this->_csrf_token_name]);
Barry Mienydd671972010-10-04 16:33:58 +0200182
RS712be25a62011-12-31 16:02:04 -0200183 // Regenerate on every submission?
184 if (config_item('csrf_regenerate'))
185 {
186 // Nothing should last forever
187 unset($_COOKIE[$this->_csrf_cookie_name]);
188 $this->_csrf_hash = '';
189 }
Andrey Andreev8a7d0782012-01-08 05:43:42 +0200190
Derek Jonesb3f10a22010-07-25 19:11:26 -0500191 $this->_csrf_set_hash();
192 $this->csrf_set_cookie();
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300193
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200194 log_message('debug', 'CSRF token verified');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400195 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Jonese701d762010-03-02 18:17:01 -0600198 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Jonese701d762010-03-02 18:17:01 -0600200 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200201 * CSRF Set Cookie
Derek Jonese701d762010-03-02 18:17:01 -0600202 *
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700203 * @codeCoverageIgnore
Andrey Andreev64354102012-10-28 14:16:02 +0200204 * @return object
Derek Jonese701d762010-03-02 18:17:01 -0600205 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500206 public function csrf_set_cookie()
Derek Jonese701d762010-03-02 18:17:01 -0600207 {
Pascal Krietec9c045a2011-04-05 14:50:41 -0400208 $expire = time() + $this->_csrf_expire;
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300209 $secure_cookie = (bool) config_item('cookie_secure');
Derek Jonese701d762010-03-02 18:17:01 -0600210
Andrey Andreev3fb02672012-10-22 16:48:01 +0300211 if ($secure_cookie && ! is_https())
Derek Jonese701d762010-03-02 18:17:01 -0600212 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200213 return FALSE;
Derek Jonese701d762010-03-02 18:17:01 -0600214 }
Derek Allard958543a2010-07-22 14:10:26 -0400215
freewil4ad0fd82012-03-13 22:37:42 -0400216 setcookie(
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300217 $this->_csrf_cookie_name,
218 $this->_csrf_hash,
219 $expire,
220 config_item('cookie_path'),
221 config_item('cookie_domain'),
freewil4ad0fd82012-03-13 22:37:42 -0400222 $secure_cookie,
223 config_item('cookie_httponly')
224 );
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200225 log_message('debug', 'CRSF cookie Set');
David Behler07b53422011-08-15 00:25:06 +0200226
Pascal Krietec9c045a2011-04-05 14:50:41 -0400227 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600228 }
229
230 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Jonese701d762010-03-02 18:17:01 -0600232 /**
233 * Show CSRF Error
234 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400235 * @return void
Derek Jonese701d762010-03-02 18:17:01 -0600236 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500237 public function csrf_show_error()
Derek Jonese701d762010-03-02 18:17:01 -0600238 {
239 show_error('The action you have requested is not allowed.');
240 }
241
242 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Jonese701d762010-03-02 18:17:01 -0600244 /**
David Behler07b53422011-08-15 00:25:06 +0200245 * Get CSRF Hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400246 *
Andrey Andreev64354102012-10-28 14:16:02 +0200247 * @see CI_Security::$_csrf_hash
248 * @return string CSRF hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400249 */
250 public function get_csrf_hash()
251 {
252 return $this->_csrf_hash;
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Get CSRF Token Name
259 *
Andrey Andreev64354102012-10-28 14:16:02 +0200260 * @see CI_Security::$_csrf_token_name
261 * @return string CSRF token name
Pascal Krietec9c045a2011-04-05 14:50:41 -0400262 */
263 public function get_csrf_token_name()
264 {
265 return $this->_csrf_token_name;
266 }
267
268 // --------------------------------------------------------------------
269
270 /**
Derek Jonese701d762010-03-02 18:17:01 -0600271 * XSS Clean
272 *
273 * Sanitizes data so that Cross Site Scripting Hacks can be
Andrey Andreev64354102012-10-28 14:16:02 +0200274 * prevented. This method does a fair amount of work but
Derek Jonese701d762010-03-02 18:17:01 -0600275 * it is extremely thorough, designed to prevent even the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500276 * most obscure XSS attempts. Nothing is ever 100% foolproof,
Derek Jonese701d762010-03-02 18:17:01 -0600277 * of course, but I haven't been able to get anything passed
278 * the filter.
279 *
Andrey Andreev64354102012-10-28 14:16:02 +0200280 * Note: Should only be used to deal with data upon submission.
281 * It's not something that should be used for general
282 * runtime processing.
Derek Jonese701d762010-03-02 18:17:01 -0600283 *
Andrey Andreev64354102012-10-28 14:16:02 +0200284 * @link http://channel.bitflux.ch/wiki/XSS_Prevention
285 * Based in part on some code and ideas from Bitflux.
Derek Jonese701d762010-03-02 18:17:01 -0600286 *
Andrey Andreev64354102012-10-28 14:16:02 +0200287 * @link http://ha.ckers.org/xss.html
288 * To help develop this script I used this great list of
289 * vulnerabilities along with a few other hacks I've
290 * harvested from examining vulnerabilities in other programs.
Derek Jonese701d762010-03-02 18:17:01 -0600291 *
Andrey Andreev64354102012-10-28 14:16:02 +0200292 * @param string|string[] $str Input data
293 * @param bool $is_image Whether the input is an image
Derek Jonese701d762010-03-02 18:17:01 -0600294 * @return string
295 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500296 public function xss_clean($str, $is_image = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600297 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200298 // Is the string an array?
Derek Jonese701d762010-03-02 18:17:01 -0600299 if (is_array($str))
300 {
301 while (list($key) = each($str))
302 {
303 $str[$key] = $this->xss_clean($str[$key]);
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Jonese701d762010-03-02 18:17:01 -0600306 return $str;
307 }
308
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200309 // Remove Invisible Characters and validate entities in URLs
310 $str = $this->_validate_entities(remove_invisible_characters($str));
Derek Jonese701d762010-03-02 18:17:01 -0600311
312 /*
313 * URL Decode
314 *
315 * Just in case stuff like this is submitted:
316 *
317 * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
318 *
319 * Note: Use rawurldecode() so it does not remove plus signs
Derek Jonese701d762010-03-02 18:17:01 -0600320 */
321 $str = rawurldecode($str);
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Jonese701d762010-03-02 18:17:01 -0600323 /*
Barry Mienydd671972010-10-04 16:33:58 +0200324 * Convert character entities to ASCII
Derek Jonese701d762010-03-02 18:17:01 -0600325 *
326 * This permits our tests below to work reliably.
327 * We only convert entities that are within tags since
328 * these are the ones that will pose security problems.
Derek Jonese701d762010-03-02 18:17:01 -0600329 */
Derek Jonese701d762010-03-02 18:17:01 -0600330 $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str);
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200331 $str = preg_replace_callback('/<\w+.*?(?=>|<|$)/si', array($this, '_decode_entity'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600332
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200333 // Remove Invisible Characters Again!
Greg Aker757dda62010-04-14 19:06:19 -0500334 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Jonese701d762010-03-02 18:17:01 -0600336 /*
337 * Convert all tabs to spaces
338 *
339 * This prevents strings like this: ja vascript
340 * NOTE: we deal with spaces between characters later.
David Behler07b53422011-08-15 00:25:06 +0200341 * NOTE: preg_replace was found to be amazingly slow here on
Pascal Krietec9c045a2011-04-05 14:50:41 -0400342 * large blocks of data, so we use str_replace.
Derek Jonese701d762010-03-02 18:17:01 -0600343 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200344 $str = str_replace("\t", ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200345
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200346 // Capture converted string for later comparison
Derek Jonese701d762010-03-02 18:17:01 -0600347 $converted_string = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200348
Pascal Krietec9c045a2011-04-05 14:50:41 -0400349 // Remove Strings that are never allowed
350 $str = $this->_do_never_allowed($str);
Derek Jonese701d762010-03-02 18:17:01 -0600351
352 /*
353 * Makes PHP tags safe
354 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400355 * Note: XML tags are inadvertently replaced too:
Derek Jonese701d762010-03-02 18:17:01 -0600356 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400357 * <?xml
Derek Jonese701d762010-03-02 18:17:01 -0600358 *
359 * But it doesn't seem to pose a problem.
Derek Jonese701d762010-03-02 18:17:01 -0600360 */
361 if ($is_image === TRUE)
362 {
David Behler07b53422011-08-15 00:25:06 +0200363 // Images have a tendency to have the PHP short opening and
364 // closing tags every so often so we skip those and only
Pascal Krietec9c045a2011-04-05 14:50:41 -0400365 // do the long opening tags.
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200366 $str = preg_replace('/<\?(php)/i', '&lt;?\\1', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600367 }
368 else
369 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500370 $str = str_replace(array('<?', '?'.'>'), array('&lt;?', '?&gt;'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Derek Jonese701d762010-03-02 18:17:01 -0600373 /*
374 * Compact any exploded words
375 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500376 * This corrects words like: j a v a s c r i p t
Derek Jonese701d762010-03-02 18:17:01 -0600377 * These words are compacted back to their correct state.
Derek Jonese701d762010-03-02 18:17:01 -0600378 */
Pascal Krietec9c045a2011-04-05 14:50:41 -0400379 $words = array(
Wes Bakerd3481352012-05-07 16:49:33 -0400380 'javascript', 'expression', 'vbscript', 'script', 'base64',
Timothy Warren40403d22012-04-19 16:38:50 -0400381 'applet', 'alert', 'document', 'write', 'cookie', 'window'
382 );
David Behler07b53422011-08-15 00:25:06 +0200383
Derek Jones37f4b9c2011-07-01 17:56:50 -0500384
Derek Jonese701d762010-03-02 18:17:01 -0600385 foreach ($words as $word)
386 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200387 $word = implode('\s*', str_split($word)).'\s*';
Derek Jonese701d762010-03-02 18:17:01 -0600388
389 // We only want to do this when it is followed by a non-word character
390 // That way valid stuff like "dealer to" does not become "dealerto"
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300391 $str = preg_replace_callback('#('.substr($word, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600392 }
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Jonese701d762010-03-02 18:17:01 -0600394 /*
395 * Remove disallowed Javascript in links or img tags
David Behler07b53422011-08-15 00:25:06 +0200396 * We used to do some version comparisons and use of stripos for PHP5,
397 * but it is dog slow compared to these simplified non-capturing
Pascal Krietec9c045a2011-04-05 14:50:41 -0400398 * preg_match(), especially if the pattern exists in the string
Derek Jonese701d762010-03-02 18:17:01 -0600399 */
400 do
401 {
402 $original = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200403
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200404 if (preg_match('/<a/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600405 {
vlakoffa81f60c2012-07-02 15:20:11 +0200406 $str = preg_replace_callback('#<a\s+([^>]*?)(?:>|$)#si', array($this, '_js_link_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200409 if (preg_match('/<img/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600410 {
vlakoffa81f60c2012-07-02 15:20:11 +0200411 $str = preg_replace_callback('#<img\s+([^>]*?)(?:\s?/?>|$)#si', array($this, '_js_img_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
vlakoffa81f60c2012-07-02 15:20:11 +0200414 if (preg_match('/script|xss/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600415 {
vlakoffa81f60c2012-07-02 15:20:11 +0200416 $str = preg_replace('#</*(?:script|xss).*?>#si', '[removed]', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600417 }
418 }
vlakoffa81f60c2012-07-02 15:20:11 +0200419 while ($original !== $str);
Derek Jonese701d762010-03-02 18:17:01 -0600420
421 unset($original);
422
Pascal Krietec9c045a2011-04-05 14:50:41 -0400423 // Remove evil attributes such as style, onclick and xmlns
424 $str = $this->_remove_evil_attributes($str, $is_image);
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Jonese701d762010-03-02 18:17:01 -0600426 /*
427 * Sanitize naughty HTML elements
428 *
429 * If a tag containing any of the words in the list
430 * below is found, the tag gets converted to entities.
431 *
432 * So this: <blink>
433 * Becomes: &lt;blink&gt;
Derek Jonese701d762010-03-02 18:17:01 -0600434 */
435 $naughty = 'alert|applet|audio|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|isindex|layer|link|meta|object|plaintext|style|script|textarea|title|video|xml|xss';
436 $str = preg_replace_callback('#<(/*\s*)('.$naughty.')([^><]*)([><]*)#is', array($this, '_sanitize_naughty_html'), $str);
437
438 /*
439 * Sanitize naughty scripting elements
440 *
441 * Similar to above, only instead of looking for
442 * tags it looks for PHP and JavaScript commands
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200443 * that are disallowed. Rather than removing the
Derek Jonese701d762010-03-02 18:17:01 -0600444 * code, it simply converts the parenthesis to entities
445 * rendering the code un-executable.
446 *
447 * For example: eval('some code')
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200448 * Becomes: eval&#40;'some code'&#41;
Derek Jonese701d762010-03-02 18:17:01 -0600449 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200450 $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si',
451 '\\1\\2&#40;\\3&#41;',
452 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200453
Pascal Krietec9c045a2011-04-05 14:50:41 -0400454 // Final clean up
455 // This adds a bit of extra precaution in case
456 // something got through the above filters
457 $str = $this->_do_never_allowed($str);
Derek Jonese701d762010-03-02 18:17:01 -0600458
459 /*
Pascal Krietec9c045a2011-04-05 14:50:41 -0400460 * Images are Handled in a Special Way
David Behler07b53422011-08-15 00:25:06 +0200461 * - Essentially, we want to know that after all of the character
462 * conversion is done whether any unwanted, likely XSS, code was found.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400463 * If not, we return TRUE, as the image is clean.
David Behler07b53422011-08-15 00:25:06 +0200464 * However, if the string post-conversion does not matched the
465 * string post-removal of XSS, then it fails, as there was unwanted XSS
Pascal Krietec9c045a2011-04-05 14:50:41 -0400466 * code found and removed/changed during processing.
Derek Jonese701d762010-03-02 18:17:01 -0600467 */
Derek Jonese701d762010-03-02 18:17:01 -0600468 if ($is_image === TRUE)
469 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200470 return ($str === $converted_string);
Derek Jonese701d762010-03-02 18:17:01 -0600471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200473 log_message('debug', 'XSS Filtering completed');
Derek Jonese701d762010-03-02 18:17:01 -0600474 return $str;
475 }
476
477 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Jonese701d762010-03-02 18:17:01 -0600479 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200480 * XSS Hash
Derek Jonese701d762010-03-02 18:17:01 -0600481 *
Andrey Andreev64354102012-10-28 14:16:02 +0200482 * Generates the XSS hash if needed and returns it.
483 *
484 * @see CI_Security::$_xss_hash
485 * @return string XSS hash
Derek Jonese701d762010-03-02 18:17:01 -0600486 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500487 public function xss_hash()
Barry Mienydd671972010-10-04 16:33:58 +0200488 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100489 if ($this->_xss_hash === '')
Derek Jonese701d762010-03-02 18:17:01 -0600490 {
Pascal Krietec38e3b62011-11-14 13:55:00 -0500491 mt_srand();
Pascal Krietec9c045a2011-04-05 14:50:41 -0400492 $this->_xss_hash = md5(time() + mt_rand(0, 1999999999));
Derek Jonese701d762010-03-02 18:17:01 -0600493 }
494
Pascal Krietec9c045a2011-04-05 14:50:41 -0400495 return $this->_xss_hash;
Derek Jonese701d762010-03-02 18:17:01 -0600496 }
497
498 // --------------------------------------------------------------------
499
500 /**
Derek Jonesa0911472010-03-30 10:33:09 -0500501 * HTML Entities Decode
502 *
Andrey Andreev64354102012-10-28 14:16:02 +0200503 * A replacement for html_entity_decode()
Derek Jonesa0911472010-03-30 10:33:09 -0500504 *
Pascal Krietec38e3b62011-11-14 13:55:00 -0500505 * The reason we are not using html_entity_decode() by itself is because
506 * while it is not technically correct to leave out the semicolon
507 * at the end of an entity most browsers will still interpret the entity
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200508 * correctly. html_entity_decode() does not convert entities without
Pascal Krietec38e3b62011-11-14 13:55:00 -0500509 * semicolons, so we are left with our own little solution here. Bummer.
Derek Jonesa0911472010-03-30 10:33:09 -0500510 *
Andrey Andreev64354102012-10-28 14:16:02 +0200511 * @link http://php.net/html-entity-decode
512 *
513 * @param string $str Input
514 * @param string $charset Character set
Derek Jonesa0911472010-03-30 10:33:09 -0500515 * @return string
516 */
freewil8cc0cfe2011-08-27 21:53:00 -0400517 public function entity_decode($str, $charset = NULL)
Derek Jonesa0911472010-03-30 10:33:09 -0500518 {
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300519 if (strpos($str, '&') === FALSE)
freewil5c9b0d12011-08-28 12:15:23 -0400520 {
521 return $str;
522 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300523
freewil5c9b0d12011-08-28 12:15:23 -0400524 if (empty($charset))
525 {
526 $charset = config_item('charset');
527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300529 $str = html_entity_decode($str, ENT_COMPAT, $charset);
530 $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str);
531 return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str);
Derek Jonesa0911472010-03-30 10:33:09 -0500532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Jonesa0911472010-03-30 10:33:09 -0500534 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Jonesa0911472010-03-30 10:33:09 -0500536 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200537 * Sanitize Filename
Derek Jonese701d762010-03-02 18:17:01 -0600538 *
Andrey Andreev64354102012-10-28 14:16:02 +0200539 * @param string $str Input file name
540 * @param bool $relative_path Whether to preserve paths
Derek Jonese701d762010-03-02 18:17:01 -0600541 * @return string
542 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500543 public function sanitize_filename($str, $relative_path = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600544 {
545 $bad = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400546 '../', '<!--', '-->', '<', '>',
547 "'", '"', '&', '$', '#',
548 '{', '}', '[', ']', '=',
549 ';', '?', '%20', '%22',
550 '%3c', // <
551 '%253c', // <
552 '%3e', // >
553 '%0e', // >
554 '%28', // (
555 '%29', // )
556 '%2528', // (
557 '%26', // &
558 '%24', // $
559 '%3f', // ?
560 '%3b', // ;
561 '%3d' // =
562 );
David Behler07b53422011-08-15 00:25:06 +0200563
Derek Jones2ef37592010-10-06 17:51:59 -0500564 if ( ! $relative_path)
565 {
566 $bad[] = './';
567 $bad[] = '/';
568 }
Derek Jonese701d762010-03-02 18:17:01 -0600569
Pascal Krietec9c045a2011-04-05 14:50:41 -0400570 $str = remove_invisible_characters($str, FALSE);
Derek Jonese701d762010-03-02 18:17:01 -0600571 return stripslashes(str_replace($bad, '', $str));
572 }
573
Pascal Krietec9c045a2011-04-05 14:50:41 -0400574 // ----------------------------------------------------------------
575
576 /**
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300577 * Strip Image Tags
578 *
Andrey Andreev64354102012-10-28 14:16:02 +0200579 * @param string $str
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300580 * @return string
581 */
582 public function strip_image_tags($str)
583 {
584 return preg_replace(array('#<img\s+.*?src\s*=\s*["\'](.+?)["\'].*?\>#', '#<img\s+.*?src\s*=\s*(.+?).*?\>#'), '\\1', $str);
585 }
586
587 // ----------------------------------------------------------------
588
589 /**
Pascal Krietec9c045a2011-04-05 14:50:41 -0400590 * Compact Exploded Words
591 *
Andrey Andreev64354102012-10-28 14:16:02 +0200592 * Callback method for xss_clean() to remove whitespace from
593 * things like 'j a v a s c r i p t'.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400594 *
Andrey Andreev64354102012-10-28 14:16:02 +0200595 * @used-by CI_Security::xss_clean()
596 * @param array $matches
Timothy Warrenad475052012-04-19 13:21:06 -0400597 * @return string
Pascal Krietec9c045a2011-04-05 14:50:41 -0400598 */
599 protected function _compact_exploded_words($matches)
600 {
601 return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
602 }
603
604 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200605
Timothy Warrenad475052012-04-19 13:21:06 -0400606 /**
607 * Remove Evil HTML Attributes (like event handlers and style)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400608 *
609 * It removes the evil attribute and either:
Pascal Krietec9c045a2011-04-05 14:50:41 -0400610 *
Andrey Andreev64354102012-10-28 14:16:02 +0200611 * - Everything up until a space. For example, everything between the pipes:
612 *
613 * <code>
614 * <a |style=document.write('hello');alert('world');| class=link>
615 * </code>
616 *
617 * - Everything inside the quotes. For example, everything between the pipes:
618 *
619 * <code>
620 * <a |style="document.write('hello'); alert('world');"| class="link">
621 * </code>
622 *
623 * @param string $str The string to check
624 * @param bool $is_image Whether the input is an image
625 * @return string The string with the evil attributes removed
Pascal Krietec9c045a2011-04-05 14:50:41 -0400626 */
627 protected function _remove_evil_attributes($str, $is_image)
628 {
629 // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns
Pascal Krietec38e3b62011-11-14 13:55:00 -0500630 $evil_attributes = array('on\w*', 'style', 'xmlns', 'formaction');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400631
632 if ($is_image === TRUE)
633 {
634 /*
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200635 * Adobe Photoshop puts XML metadata into JFIF images,
Pascal Krietec9c045a2011-04-05 14:50:41 -0400636 * including namespacing, so we have to allow this for images.
637 */
638 unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
639 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200640
Pascal Krietec9c045a2011-04-05 14:50:41 -0400641 do {
Pascal Krietec38e3b62011-11-14 13:55:00 -0500642 $count = 0;
643 $attribs = array();
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200644
Pascal Krietec38e3b62011-11-14 13:55:00 -0500645 // find occurrences of illegal attribute strings without quotes
Wes Baker5335bc32012-04-24 15:17:14 -0400646 preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*([^\s>]*)/is', $str, $matches, PREG_SET_ORDER);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200647
Pascal Krietec38e3b62011-11-14 13:55:00 -0500648 foreach ($matches as $attr)
649 {
Wes Baker5335bc32012-04-24 15:17:14 -0400650
Pascal Krietec38e3b62011-11-14 13:55:00 -0500651 $attribs[] = preg_quote($attr[0], '/');
652 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200653
Pascal Krietec38e3b62011-11-14 13:55:00 -0500654 // find occurrences of illegal attribute strings with quotes (042 and 047 are octal quotes)
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200655 preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*(\042|\047)([^\\2]*?)(\\2)/is', $str, $matches, PREG_SET_ORDER);
David Behler07b53422011-08-15 00:25:06 +0200656
Pascal Krietec38e3b62011-11-14 13:55:00 -0500657 foreach ($matches as $attr)
658 {
659 $attribs[] = preg_quote($attr[0], '/');
660 }
661
662 // replace illegal attribute strings that are inside an html tag
663 if (count($attribs) > 0)
664 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300665 $str = preg_replace('/<(\/?[^><]+?)([^A-Za-z<>\-])(.*?)('.implode('|', $attribs).')(.*?)([\s><])([><]*)/i', '<$1 $3$5$6$7', $str, -1, $count);
Pascal Krietec38e3b62011-11-14 13:55:00 -0500666 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200667
Pascal Krietec38e3b62011-11-14 13:55:00 -0500668 } while ($count);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200669
Pascal Krietec9c045a2011-04-05 14:50:41 -0400670 return $str;
671 }
David Behler07b53422011-08-15 00:25:06 +0200672
Pascal Krietec9c045a2011-04-05 14:50:41 -0400673 // --------------------------------------------------------------------
674
675 /**
676 * Sanitize Naughty HTML
677 *
Andrey Andreev64354102012-10-28 14:16:02 +0200678 * Callback method for xss_clean() to remove naughty HTML elements.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400679 *
Andrey Andreev64354102012-10-28 14:16:02 +0200680 * @used-by CI_Security::xss_clean()
681 * @param array $matches
Pascal Krietec9c045a2011-04-05 14:50:41 -0400682 * @return string
683 */
684 protected function _sanitize_naughty_html($matches)
685 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200686 return '&lt;'.$matches[1].$matches[2].$matches[3] // encode opening brace
687 // encode captured opening or closing brace to prevent recursive vectors:
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200688 .str_replace(array('>', '<'), array('&gt;', '&lt;'), $matches[4]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400689 }
690
691 // --------------------------------------------------------------------
692
693 /**
694 * JS Link Removal
695 *
Andrey Andreev64354102012-10-28 14:16:02 +0200696 * Callback method for xss_clean() to sanitize links.
697 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400698 * This limits the PCRE backtracks, making it more performance friendly
699 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200700 * PHP 5.2+ on link-heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400701 *
Andrey Andreev64354102012-10-28 14:16:02 +0200702 * @used-by CI_Security::xss_clean()
703 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400704 * @return string
705 */
706 protected function _js_link_removal($match)
707 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200708 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200709 preg_replace('#href=.*?(?:alert\(|alert&\#40;|javascript:|livescript:|mocha:|charset=|window\.|document\.|\.cookie|<script|<xss|data\s*:)#si',
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200710 '',
711 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
712 ),
713 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400714 }
715
716 // --------------------------------------------------------------------
717
718 /**
719 * JS Image Removal
720 *
Andrey Andreev64354102012-10-28 14:16:02 +0200721 * Callback method for xss_clean() to sanitize image tags.
722 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400723 * This limits the PCRE backtracks, making it more performance friendly
724 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200725 * PHP 5.2+ on image tag heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400726 *
Andrey Andreev64354102012-10-28 14:16:02 +0200727 * @used-by CI_Security::xss_clean()
728 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400729 * @return string
730 */
731 protected function _js_img_removal($match)
732 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200733 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200734 preg_replace('#src=.*?(?:alert\(|alert&\#40;|javascript:|livescript:|mocha:|charset=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si',
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200735 '',
736 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
737 ),
738 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400739 }
740
741 // --------------------------------------------------------------------
742
743 /**
744 * Attribute Conversion
745 *
Andrey Andreev64354102012-10-28 14:16:02 +0200746 * @used-by CI_Security::xss_clean()
747 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400748 * @return string
749 */
750 protected function _convert_attribute($match)
751 {
752 return str_replace(array('>', '<', '\\'), array('&gt;', '&lt;', '\\\\'), $match[0]);
753 }
754
755 // --------------------------------------------------------------------
756
757 /**
758 * Filter Attributes
759 *
Andrey Andreev64354102012-10-28 14:16:02 +0200760 * Filters tag attributes for consistency and safety.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400761 *
Andrey Andreev64354102012-10-28 14:16:02 +0200762 * @used-by CI_Security::_js_img_removal()
763 * @used-by CI_Security::_js_link_removal()
764 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400765 * @return string
766 */
767 protected function _filter_attributes($str)
768 {
769 $out = '';
Pascal Krietec9c045a2011-04-05 14:50:41 -0400770 if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches))
771 {
772 foreach ($matches[0] as $match)
773 {
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200774 $out .= preg_replace('#/\*.*?\*/#s', '', $match);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400775 }
776 }
777
778 return $out;
779 }
780
781 // --------------------------------------------------------------------
782
783 /**
784 * HTML Entity Decode Callback
785 *
Andrey Andreev64354102012-10-28 14:16:02 +0200786 * @used-by CI_Security::xss_clean()
787 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400788 * @return string
789 */
790 protected function _decode_entity($match)
791 {
792 return $this->entity_decode($match[0], strtoupper(config_item('charset')));
793 }
794
795 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200796
Pascal Krietec9c045a2011-04-05 14:50:41 -0400797 /**
798 * Validate URL entities
799 *
Andrey Andreev64354102012-10-28 14:16:02 +0200800 * @used-by CI_Security::xss_clean()
801 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400802 * @return string
803 */
804 protected function _validate_entities($str)
805 {
806 /*
807 * Protect GET variables in URLs
808 */
David Behler07b53422011-08-15 00:25:06 +0200809
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200810 // 901119URL5918AMP18930PROTECT8198
811 $str = preg_replace('|\&([a-z\_0-9\-]+)\=([a-z\_0-9\-]+)|i', $this->xss_hash().'\\1=\\2', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400812
813 /*
814 * Validate standard character entities
815 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500816 * Add a semicolon if missing. We do this to enable
Pascal Krietec9c045a2011-04-05 14:50:41 -0400817 * the conversion of entities to ASCII later.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400818 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200819 $str = preg_replace('#(&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', '\\1;\\2', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400820
821 /*
822 * Validate UTF16 two byte encoding (x00)
823 *
824 * Just as above, adds a semicolon if missing.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400825 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200826 $str = preg_replace('#(&\#x?)([0-9A-F]+);?#i', '\\1\\2;', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400827
828 /*
829 * Un-Protect GET variables in URLs
830 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200831 return str_replace($this->xss_hash(), '&', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400832 }
833
834 // ----------------------------------------------------------------------
835
836 /**
837 * Do Never Allowed
838 *
Andrey Andreev64354102012-10-28 14:16:02 +0200839 * @used-by CI_Security::xss_clean()
Pascal Krietec9c045a2011-04-05 14:50:41 -0400840 * @param string
841 * @return string
842 */
843 protected function _do_never_allowed($str)
844 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200845 $str = str_replace(array_keys($this->_never_allowed_str), $this->_never_allowed_str, $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400846
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200847 foreach ($this->_never_allowed_regex as $regex)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400848 {
Wes Baker5335bc32012-04-24 15:17:14 -0400849 $str = preg_replace('#'.$regex.'#is', '[removed]', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400850 }
David Behler07b53422011-08-15 00:25:06 +0200851
Pascal Krietec9c045a2011-04-05 14:50:41 -0400852 return $str;
853 }
854
855 // --------------------------------------------------------------------
856
857 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200858 * Set CSRF Hash and Cookie
Pascal Krietec9c045a2011-04-05 14:50:41 -0400859 *
860 * @return string
861 */
862 protected function _csrf_set_hash()
863 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100864 if ($this->_csrf_hash === '')
Pascal Krietec9c045a2011-04-05 14:50:41 -0400865 {
David Behler07b53422011-08-15 00:25:06 +0200866 // If the cookie exists we will use it's value.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400867 // We don't necessarily want to regenerate it with
David Behler07b53422011-08-15 00:25:06 +0200868 // each page load since a page could contain embedded
Pascal Krietec9c045a2011-04-05 14:50:41 -0400869 // sub-pages causing this feature to fail
David Behler07b53422011-08-15 00:25:06 +0200870 if (isset($_COOKIE[$this->_csrf_cookie_name]) &&
Alexander Hofstedee2c374f2012-05-17 00:28:08 +0200871 preg_match('#^[0-9a-f]{32}$#iS', $_COOKIE[$this->_csrf_cookie_name]) === 1)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400872 {
873 return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name];
874 }
David Behler07b53422011-08-15 00:25:06 +0200875
Chris Berthed93e6f32011-09-25 10:33:25 -0400876 $this->_csrf_hash = md5(uniqid(rand(), TRUE));
877 $this->csrf_set_cookie();
Pascal Krietec9c045a2011-04-05 14:50:41 -0400878 }
879
880 return $this->_csrf_hash;
881 }
882
Derek Jonese701d762010-03-02 18:17:01 -0600883}
Derek Jonese701d762010-03-02 18:17:01 -0600884
885/* End of file Security.php */
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300886/* Location: ./system/core/Security.php */