blob: cd1cb1ab409a3f20519e5bcd4f1937410def8e21 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Jonese701d762010-03-02 18:17:01 -060028
Derek Jonese701d762010-03-02 18:17:01 -060029/**
30 * Security Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Security
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Pascal Krietec9c045a2011-04-05 14:50:41 -040036 * @link http://codeigniter.com/user_guide/libraries/security.html
Derek Jonese701d762010-03-02 18:17:01 -060037 */
38class CI_Security {
Barry Mienydd671972010-10-04 16:33:58 +020039
David Behler07b53422011-08-15 00:25:06 +020040 /**
Andrey Andreev64354102012-10-28 14:16:02 +020041 * XSS Hash
David Behler07b53422011-08-15 00:25:06 +020042 *
Andrey Andreev64354102012-10-28 14:16:02 +020043 * Random Hash for protecting URLs.
44 *
45 * @var string
David Behler07b53422011-08-15 00:25:06 +020046 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040047 protected $_xss_hash = '';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030048
David Behler07b53422011-08-15 00:25:06 +020049 /**
Andrey Andreev64354102012-10-28 14:16:02 +020050 * CSRF Hash
David Behler07b53422011-08-15 00:25:06 +020051 *
Andrey Andreev64354102012-10-28 14:16:02 +020052 * Random hash for Cross Site Request Forgery protection cookie
53 *
54 * @var string
David Behler07b53422011-08-15 00:25:06 +020055 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040056 protected $_csrf_hash = '';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030057
David Behler07b53422011-08-15 00:25:06 +020058 /**
Andrey Andreev64354102012-10-28 14:16:02 +020059 * CSRF Expire time
David Behler07b53422011-08-15 00:25:06 +020060 *
Andrey Andreev64354102012-10-28 14:16:02 +020061 * Expiration time for Cross Site Request Forgery protection cookie.
62 * Defaults to two hours (in seconds).
63 *
64 * @var int
David Behler07b53422011-08-15 00:25:06 +020065 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040066 protected $_csrf_expire = 7200;
Andrey Andreev3d113bd2011-10-05 00:03:20 +030067
David Behler07b53422011-08-15 00:25:06 +020068 /**
Andrey Andreev64354102012-10-28 14:16:02 +020069 * CSRF Token name
David Behler07b53422011-08-15 00:25:06 +020070 *
Andrey Andreev64354102012-10-28 14:16:02 +020071 * Token name for Cross Site Request Forgery protection cookie.
72 *
73 * @var string
David Behler07b53422011-08-15 00:25:06 +020074 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040075 protected $_csrf_token_name = 'ci_csrf_token';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030076
David Behler07b53422011-08-15 00:25:06 +020077 /**
Andrey Andreev64354102012-10-28 14:16:02 +020078 * CSRF Cookie name
David Behler07b53422011-08-15 00:25:06 +020079 *
Andrey Andreev64354102012-10-28 14:16:02 +020080 * Cookie name for Cross Site Request Forgery protection cookie.
81 *
82 * @var string
David Behler07b53422011-08-15 00:25:06 +020083 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040084 protected $_csrf_cookie_name = 'ci_csrf_token';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030085
David Behler07b53422011-08-15 00:25:06 +020086 /**
87 * List of never allowed strings
88 *
Andrey Andreev64354102012-10-28 14:16:02 +020089 * @var array
David Behler07b53422011-08-15 00:25:06 +020090 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040091 protected $_never_allowed_str = array(
Timothy Warren40403d22012-04-19 16:38:50 -040092 'document.cookie' => '[removed]',
93 'document.write' => '[removed]',
94 '.parentNode' => '[removed]',
95 '.innerHTML' => '[removed]',
96 'window.location' => '[removed]',
97 '-moz-binding' => '[removed]',
98 '<!--' => '&lt;!--',
99 '-->' => '--&gt;',
100 '<![CDATA[' => '&lt;![CDATA[',
101 '<comment>' => '&lt;comment&gt;'
102 );
Derek Jonese701d762010-03-02 18:17:01 -0600103
David Behler07b53422011-08-15 00:25:06 +0200104 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200105 * List of never allowed regex replacements
David Behler07b53422011-08-15 00:25:06 +0200106 *
Andrey Andreev64354102012-10-28 14:16:02 +0200107 * @var array
David Behler07b53422011-08-15 00:25:06 +0200108 */
Pascal Krietec9c045a2011-04-05 14:50:41 -0400109 protected $_never_allowed_regex = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400110 'javascript\s*:',
111 'expression\s*(\(|&\#40;)', // CSS and IE
112 'vbscript\s*:', // IE, surprise!
Wes Bakerd3481352012-05-07 16:49:33 -0400113 'Redirect\s+302',
114 "([\"'])?data\s*:[^\\1]*?base64[^\\1]*?,[^\\1]*?\\1?"
Timothy Warren40403d22012-04-19 16:38:50 -0400115 );
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300116
Timothy Warrenad475052012-04-19 13:21:06 -0400117 /**
Hunter Wu23719ab2013-08-01 23:15:13 +0800118 * List of bad chars for sanitize filename
119 *
120 * @var array
121 */
122 private $_filename_bad_str_rules = array(
123 'default' => array(
124 '../', '<!--', '-->', '<', '>',
125 "'", '"', '&', '$', '#',
126 '{', '}', '[', ']', '=',
127 ';', '?', '%20', '%22',
128 '%3c', // <
129 '%253c', // <
130 '%3e', // >
131 '%0e', // >
132 '%28', // (
133 '%29', // )
134 '%2528', // (
135 '%26', // &
136 '%24', // $
137 '%3f', // ?
138 '%3b', // ;
139 '%3d' // =
140 ),
141 'windows' => array(
142 '\\', '/', ':', '*', '?',
143 '"', '<', '>', '|',
144 ),
145 );
146
147 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200148 * Class constructor
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300149 *
150 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -0400151 */
Greg Akera9263282010-11-10 15:26:43 -0600152 public function __construct()
Derek Jonese701d762010-03-02 18:17:01 -0600153 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200154 // Is CSRF protection enabled?
155 if (config_item('csrf_protection') === TRUE)
patworkef1a55a2011-04-09 13:04:06 +0200156 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200157 // CSRF config
158 foreach (array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key)
patworkef1a55a2011-04-09 13:04:06 +0200159 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200160 if (FALSE !== ($val = config_item($key)))
161 {
162 $this->{'_'.$key} = $val;
163 }
patworkef1a55a2011-04-09 13:04:06 +0200164 }
patworkef1a55a2011-04-09 13:04:06 +0200165
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200166 // Append application specific cookie prefix
167 if (config_item('cookie_prefix'))
168 {
169 $this->_csrf_cookie_name = config_item('cookie_prefix').$this->_csrf_cookie_name;
170 }
Derek Jonesb3f10a22010-07-25 19:11:26 -0500171
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200172 // Set the CSRF hash
173 $this->_csrf_set_hash();
174 }
Derek Allard958543a2010-07-22 14:10:26 -0400175
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200176 log_message('debug', 'Security Class Initialized');
Derek Jonese701d762010-03-02 18:17:01 -0600177 }
178
179 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Jonese701d762010-03-02 18:17:01 -0600181 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200182 * CSRF Verify
Derek Jonese701d762010-03-02 18:17:01 -0600183 *
Andrew Podner4296a652012-12-17 07:51:15 -0500184 * @return CI_Security
Derek Jonese701d762010-03-02 18:17:01 -0600185 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500186 public function csrf_verify()
Derek Allard958543a2010-07-22 14:10:26 -0400187 {
Andrey Andreev5d27c432012-03-08 12:01:52 +0200188 // If it's not a POST request we will set the CSRF cookie
189 if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST')
Derek Jonese701d762010-03-02 18:17:01 -0600190 {
191 return $this->csrf_set_cookie();
192 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300193
Alex Bilbieaeb2c3e2011-08-21 16:14:54 +0100194 // Check if URI has been whitelisted from CSRF checks
195 if ($exclude_uris = config_item('csrf_exclude_uris'))
196 {
197 $uri = load_class('URI', 'core');
198 if (in_array($uri->uri_string(), $exclude_uris))
199 {
200 return $this;
201 }
202 }
Derek Jonese701d762010-03-02 18:17:01 -0600203
204 // Do the tokens exist in both the _POST and _COOKIE arrays?
Andrey Andreevf795ab52012-10-24 21:28:25 +0300205 if ( ! isset($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name])
Alex Bilbieed944a32012-06-02 11:07:47 +0100206 OR $_POST[$this->_csrf_token_name] !== $_COOKIE[$this->_csrf_cookie_name]) // Do the tokens match?
Derek Jonese701d762010-03-02 18:17:01 -0600207 {
208 $this->csrf_show_error();
209 }
210
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200211 // We kill this since we're done and we don't want to polute the _POST array
Pascal Krietec9c045a2011-04-05 14:50:41 -0400212 unset($_POST[$this->_csrf_token_name]);
Barry Mienydd671972010-10-04 16:33:58 +0200213
RS712be25a62011-12-31 16:02:04 -0200214 // Regenerate on every submission?
215 if (config_item('csrf_regenerate'))
216 {
217 // Nothing should last forever
218 unset($_COOKIE[$this->_csrf_cookie_name]);
219 $this->_csrf_hash = '';
220 }
Andrey Andreev8a7d0782012-01-08 05:43:42 +0200221
Derek Jonesb3f10a22010-07-25 19:11:26 -0500222 $this->_csrf_set_hash();
223 $this->csrf_set_cookie();
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300224
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200225 log_message('debug', 'CSRF token verified');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400226 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Jonese701d762010-03-02 18:17:01 -0600229 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Jonese701d762010-03-02 18:17:01 -0600231 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200232 * CSRF Set Cookie
Derek Jonese701d762010-03-02 18:17:01 -0600233 *
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700234 * @codeCoverageIgnore
Andrew Podner4296a652012-12-17 07:51:15 -0500235 * @return CI_Security
Derek Jonese701d762010-03-02 18:17:01 -0600236 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500237 public function csrf_set_cookie()
Derek Jonese701d762010-03-02 18:17:01 -0600238 {
Pascal Krietec9c045a2011-04-05 14:50:41 -0400239 $expire = time() + $this->_csrf_expire;
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300240 $secure_cookie = (bool) config_item('cookie_secure');
Derek Jonese701d762010-03-02 18:17:01 -0600241
Andrey Andreev3fb02672012-10-22 16:48:01 +0300242 if ($secure_cookie && ! is_https())
Derek Jonese701d762010-03-02 18:17:01 -0600243 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200244 return FALSE;
Derek Jonese701d762010-03-02 18:17:01 -0600245 }
Derek Allard958543a2010-07-22 14:10:26 -0400246
freewil4ad0fd82012-03-13 22:37:42 -0400247 setcookie(
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300248 $this->_csrf_cookie_name,
249 $this->_csrf_hash,
250 $expire,
251 config_item('cookie_path'),
252 config_item('cookie_domain'),
freewil4ad0fd82012-03-13 22:37:42 -0400253 $secure_cookie,
254 config_item('cookie_httponly')
255 );
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200256 log_message('debug', 'CRSF cookie Set');
David Behler07b53422011-08-15 00:25:06 +0200257
Pascal Krietec9c045a2011-04-05 14:50:41 -0400258 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600259 }
260
261 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Jonese701d762010-03-02 18:17:01 -0600263 /**
264 * Show CSRF Error
265 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400266 * @return void
Derek Jonese701d762010-03-02 18:17:01 -0600267 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500268 public function csrf_show_error()
Derek Jonese701d762010-03-02 18:17:01 -0600269 {
270 show_error('The action you have requested is not allowed.');
271 }
272
273 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Jonese701d762010-03-02 18:17:01 -0600275 /**
David Behler07b53422011-08-15 00:25:06 +0200276 * Get CSRF Hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400277 *
Andrey Andreev64354102012-10-28 14:16:02 +0200278 * @see CI_Security::$_csrf_hash
279 * @return string CSRF hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400280 */
281 public function get_csrf_hash()
282 {
283 return $this->_csrf_hash;
284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * Get CSRF Token Name
290 *
Andrey Andreev64354102012-10-28 14:16:02 +0200291 * @see CI_Security::$_csrf_token_name
292 * @return string CSRF token name
Pascal Krietec9c045a2011-04-05 14:50:41 -0400293 */
294 public function get_csrf_token_name()
295 {
296 return $this->_csrf_token_name;
297 }
298
299 // --------------------------------------------------------------------
300
301 /**
Derek Jonese701d762010-03-02 18:17:01 -0600302 * XSS Clean
303 *
304 * Sanitizes data so that Cross Site Scripting Hacks can be
Andrey Andreev64354102012-10-28 14:16:02 +0200305 * prevented. This method does a fair amount of work but
Derek Jonese701d762010-03-02 18:17:01 -0600306 * it is extremely thorough, designed to prevent even the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500307 * most obscure XSS attempts. Nothing is ever 100% foolproof,
Derek Jonese701d762010-03-02 18:17:01 -0600308 * of course, but I haven't been able to get anything passed
309 * the filter.
310 *
Andrey Andreev64354102012-10-28 14:16:02 +0200311 * Note: Should only be used to deal with data upon submission.
312 * It's not something that should be used for general
313 * runtime processing.
Derek Jonese701d762010-03-02 18:17:01 -0600314 *
Andrey Andreev64354102012-10-28 14:16:02 +0200315 * @link http://channel.bitflux.ch/wiki/XSS_Prevention
316 * Based in part on some code and ideas from Bitflux.
Derek Jonese701d762010-03-02 18:17:01 -0600317 *
Andrey Andreev64354102012-10-28 14:16:02 +0200318 * @link http://ha.ckers.org/xss.html
319 * To help develop this script I used this great list of
320 * vulnerabilities along with a few other hacks I've
321 * harvested from examining vulnerabilities in other programs.
Derek Jonese701d762010-03-02 18:17:01 -0600322 *
Andrey Andreev64354102012-10-28 14:16:02 +0200323 * @param string|string[] $str Input data
324 * @param bool $is_image Whether the input is an image
Derek Jonese701d762010-03-02 18:17:01 -0600325 * @return string
326 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500327 public function xss_clean($str, $is_image = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600328 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200329 // Is the string an array?
Derek Jonese701d762010-03-02 18:17:01 -0600330 if (is_array($str))
331 {
332 while (list($key) = each($str))
333 {
334 $str[$key] = $this->xss_clean($str[$key]);
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Jonese701d762010-03-02 18:17:01 -0600337 return $str;
338 }
339
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200340 // Remove Invisible Characters and validate entities in URLs
341 $str = $this->_validate_entities(remove_invisible_characters($str));
Derek Jonese701d762010-03-02 18:17:01 -0600342
343 /*
344 * URL Decode
345 *
346 * Just in case stuff like this is submitted:
347 *
348 * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
349 *
350 * Note: Use rawurldecode() so it does not remove plus signs
Derek Jonese701d762010-03-02 18:17:01 -0600351 */
352 $str = rawurldecode($str);
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Jonese701d762010-03-02 18:17:01 -0600354 /*
Barry Mienydd671972010-10-04 16:33:58 +0200355 * Convert character entities to ASCII
Derek Jonese701d762010-03-02 18:17:01 -0600356 *
357 * This permits our tests below to work reliably.
358 * We only convert entities that are within tags since
359 * these are the ones that will pose security problems.
Derek Jonese701d762010-03-02 18:17:01 -0600360 */
Derek Jonese701d762010-03-02 18:17:01 -0600361 $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str);
brian97807ccbe52012-12-11 20:24:12 +0200362 $str = preg_replace_callback('/<\w+.*/si', array($this, '_decode_entity'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600363
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200364 // Remove Invisible Characters Again!
Greg Aker757dda62010-04-14 19:06:19 -0500365 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Jonese701d762010-03-02 18:17:01 -0600367 /*
368 * Convert all tabs to spaces
369 *
370 * This prevents strings like this: ja vascript
371 * NOTE: we deal with spaces between characters later.
David Behler07b53422011-08-15 00:25:06 +0200372 * NOTE: preg_replace was found to be amazingly slow here on
Pascal Krietec9c045a2011-04-05 14:50:41 -0400373 * large blocks of data, so we use str_replace.
Derek Jonese701d762010-03-02 18:17:01 -0600374 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200375 $str = str_replace("\t", ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200376
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200377 // Capture converted string for later comparison
Derek Jonese701d762010-03-02 18:17:01 -0600378 $converted_string = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200379
Pascal Krietec9c045a2011-04-05 14:50:41 -0400380 // Remove Strings that are never allowed
381 $str = $this->_do_never_allowed($str);
Derek Jonese701d762010-03-02 18:17:01 -0600382
383 /*
384 * Makes PHP tags safe
385 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400386 * Note: XML tags are inadvertently replaced too:
Derek Jonese701d762010-03-02 18:17:01 -0600387 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400388 * <?xml
Derek Jonese701d762010-03-02 18:17:01 -0600389 *
390 * But it doesn't seem to pose a problem.
Derek Jonese701d762010-03-02 18:17:01 -0600391 */
392 if ($is_image === TRUE)
393 {
David Behler07b53422011-08-15 00:25:06 +0200394 // Images have a tendency to have the PHP short opening and
395 // closing tags every so often so we skip those and only
Pascal Krietec9c045a2011-04-05 14:50:41 -0400396 // do the long opening tags.
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200397 $str = preg_replace('/<\?(php)/i', '&lt;?\\1', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600398 }
399 else
400 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200401 $str = str_replace(array('<?', '?'.'>'), array('&lt;?', '?&gt;'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Jonese701d762010-03-02 18:17:01 -0600404 /*
405 * Compact any exploded words
406 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500407 * This corrects words like: j a v a s c r i p t
Derek Jonese701d762010-03-02 18:17:01 -0600408 * These words are compacted back to their correct state.
Derek Jonese701d762010-03-02 18:17:01 -0600409 */
Pascal Krietec9c045a2011-04-05 14:50:41 -0400410 $words = array(
Wes Bakerd3481352012-05-07 16:49:33 -0400411 'javascript', 'expression', 'vbscript', 'script', 'base64',
Timothy Warren40403d22012-04-19 16:38:50 -0400412 'applet', 'alert', 'document', 'write', 'cookie', 'window'
413 );
David Behler07b53422011-08-15 00:25:06 +0200414
Derek Jonese701d762010-03-02 18:17:01 -0600415 foreach ($words as $word)
416 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200417 $word = implode('\s*', str_split($word)).'\s*';
Derek Jonese701d762010-03-02 18:17:01 -0600418
419 // We only want to do this when it is followed by a non-word character
420 // That way valid stuff like "dealer to" does not become "dealerto"
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300421 $str = preg_replace_callback('#('.substr($word, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Jonese701d762010-03-02 18:17:01 -0600424 /*
425 * Remove disallowed Javascript in links or img tags
David Behler07b53422011-08-15 00:25:06 +0200426 * We used to do some version comparisons and use of stripos for PHP5,
427 * but it is dog slow compared to these simplified non-capturing
Pascal Krietec9c045a2011-04-05 14:50:41 -0400428 * preg_match(), especially if the pattern exists in the string
Derek Jonese701d762010-03-02 18:17:01 -0600429 */
430 do
431 {
432 $original = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200433
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200434 if (preg_match('/<a/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600435 {
vlakoffa81f60c2012-07-02 15:20:11 +0200436 $str = preg_replace_callback('#<a\s+([^>]*?)(?:>|$)#si', array($this, '_js_link_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200439 if (preg_match('/<img/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600440 {
vlakoffa81f60c2012-07-02 15:20:11 +0200441 $str = preg_replace_callback('#<img\s+([^>]*?)(?:\s?/?>|$)#si', array($this, '_js_img_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
vlakoffa81f60c2012-07-02 15:20:11 +0200444 if (preg_match('/script|xss/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600445 {
vlakoffa81f60c2012-07-02 15:20:11 +0200446 $str = preg_replace('#</*(?:script|xss).*?>#si', '[removed]', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600447 }
448 }
vlakoffa81f60c2012-07-02 15:20:11 +0200449 while ($original !== $str);
Derek Jonese701d762010-03-02 18:17:01 -0600450
451 unset($original);
452
Pascal Krietec9c045a2011-04-05 14:50:41 -0400453 // Remove evil attributes such as style, onclick and xmlns
454 $str = $this->_remove_evil_attributes($str, $is_image);
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Jonese701d762010-03-02 18:17:01 -0600456 /*
457 * Sanitize naughty HTML elements
458 *
459 * If a tag containing any of the words in the list
460 * below is found, the tag gets converted to entities.
461 *
462 * So this: <blink>
463 * Becomes: &lt;blink&gt;
Derek Jonese701d762010-03-02 18:17:01 -0600464 */
465 $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';
466 $str = preg_replace_callback('#<(/*\s*)('.$naughty.')([^><]*)([><]*)#is', array($this, '_sanitize_naughty_html'), $str);
467
468 /*
469 * Sanitize naughty scripting elements
470 *
471 * Similar to above, only instead of looking for
472 * tags it looks for PHP and JavaScript commands
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200473 * that are disallowed. Rather than removing the
Derek Jonese701d762010-03-02 18:17:01 -0600474 * code, it simply converts the parenthesis to entities
475 * rendering the code un-executable.
476 *
477 * For example: eval('some code')
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200478 * Becomes: eval&#40;'some code'&#41;
Derek Jonese701d762010-03-02 18:17:01 -0600479 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200480 $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si',
481 '\\1\\2&#40;\\3&#41;',
482 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200483
Pascal Krietec9c045a2011-04-05 14:50:41 -0400484 // Final clean up
485 // This adds a bit of extra precaution in case
486 // something got through the above filters
487 $str = $this->_do_never_allowed($str);
Derek Jonese701d762010-03-02 18:17:01 -0600488
489 /*
Pascal Krietec9c045a2011-04-05 14:50:41 -0400490 * Images are Handled in a Special Way
David Behler07b53422011-08-15 00:25:06 +0200491 * - Essentially, we want to know that after all of the character
492 * conversion is done whether any unwanted, likely XSS, code was found.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400493 * If not, we return TRUE, as the image is clean.
David Behler07b53422011-08-15 00:25:06 +0200494 * However, if the string post-conversion does not matched the
495 * string post-removal of XSS, then it fails, as there was unwanted XSS
Pascal Krietec9c045a2011-04-05 14:50:41 -0400496 * code found and removed/changed during processing.
Derek Jonese701d762010-03-02 18:17:01 -0600497 */
Derek Jonese701d762010-03-02 18:17:01 -0600498 if ($is_image === TRUE)
499 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200500 return ($str === $converted_string);
Derek Jonese701d762010-03-02 18:17:01 -0600501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200503 log_message('debug', 'XSS Filtering completed');
Derek Jonese701d762010-03-02 18:17:01 -0600504 return $str;
505 }
506
507 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Jonese701d762010-03-02 18:17:01 -0600509 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200510 * XSS Hash
Derek Jonese701d762010-03-02 18:17:01 -0600511 *
Andrey Andreev64354102012-10-28 14:16:02 +0200512 * Generates the XSS hash if needed and returns it.
513 *
514 * @see CI_Security::$_xss_hash
515 * @return string XSS hash
Derek Jonese701d762010-03-02 18:17:01 -0600516 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500517 public function xss_hash()
Barry Mienydd671972010-10-04 16:33:58 +0200518 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100519 if ($this->_xss_hash === '')
Derek Jonese701d762010-03-02 18:17:01 -0600520 {
vlakoff06127562013-03-30 00:06:39 +0100521 $this->_xss_hash = md5(uniqid(mt_rand()));
Derek Jonese701d762010-03-02 18:17:01 -0600522 }
523
Pascal Krietec9c045a2011-04-05 14:50:41 -0400524 return $this->_xss_hash;
Derek Jonese701d762010-03-02 18:17:01 -0600525 }
526
527 // --------------------------------------------------------------------
528
529 /**
Derek Jonesa0911472010-03-30 10:33:09 -0500530 * HTML Entities Decode
531 *
Andrey Andreev64354102012-10-28 14:16:02 +0200532 * A replacement for html_entity_decode()
Derek Jonesa0911472010-03-30 10:33:09 -0500533 *
Pascal Krietec38e3b62011-11-14 13:55:00 -0500534 * The reason we are not using html_entity_decode() by itself is because
535 * while it is not technically correct to leave out the semicolon
536 * at the end of an entity most browsers will still interpret the entity
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200537 * correctly. html_entity_decode() does not convert entities without
Pascal Krietec38e3b62011-11-14 13:55:00 -0500538 * semicolons, so we are left with our own little solution here. Bummer.
Derek Jonesa0911472010-03-30 10:33:09 -0500539 *
Andrey Andreev64354102012-10-28 14:16:02 +0200540 * @link http://php.net/html-entity-decode
541 *
542 * @param string $str Input
543 * @param string $charset Character set
Derek Jonesa0911472010-03-30 10:33:09 -0500544 * @return string
545 */
freewil8cc0cfe2011-08-27 21:53:00 -0400546 public function entity_decode($str, $charset = NULL)
Derek Jonesa0911472010-03-30 10:33:09 -0500547 {
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300548 if (strpos($str, '&') === FALSE)
freewil5c9b0d12011-08-28 12:15:23 -0400549 {
550 return $str;
551 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300552
freewil5c9b0d12011-08-28 12:15:23 -0400553 if (empty($charset))
554 {
555 $charset = config_item('charset');
556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
brian978638a9d22012-12-18 13:25:54 +0200558 do
559 {
560 $matches = $matches1 = 0;
brian97807ccbe52012-12-11 20:24:12 +0200561
brian978638a9d22012-12-18 13:25:54 +0200562 $str = html_entity_decode($str, ENT_COMPAT, $charset);
563 $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str, -1, $matches);
564 $str = preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str, -1, $matches1);
565 }
Andrey Andreev72ed4c32012-12-19 17:07:54 +0200566 while ($matches OR $matches1);
brian978f50fc732012-12-08 23:22:26 +0200567
brian978638a9d22012-12-18 13:25:54 +0200568 return $str;
Derek Jonesa0911472010-03-30 10:33:09 -0500569 }
Barry Mienydd671972010-10-04 16:33:58 +0200570
Derek Jonesa0911472010-03-30 10:33:09 -0500571 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Jonesa0911472010-03-30 10:33:09 -0500573 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200574 * Sanitize Filename
Derek Jonese701d762010-03-02 18:17:01 -0600575 *
Andrey Andreev64354102012-10-28 14:16:02 +0200576 * @param string $str Input file name
577 * @param bool $relative_path Whether to preserve paths
Derek Jonese701d762010-03-02 18:17:01 -0600578 * @return string
579 */
Hunter Wu23719ab2013-08-01 23:15:13 +0800580 public function sanitize_filename($str, $relative_path = FALSE, $rule = 'default')
Derek Jonese701d762010-03-02 18:17:01 -0600581 {
Hunter Wu23719ab2013-08-01 23:15:13 +0800582 $bad = $this->_filename_bad_str_rules[$rule];
David Behler07b53422011-08-15 00:25:06 +0200583
Derek Jones2ef37592010-10-06 17:51:59 -0500584 if ( ! $relative_path)
585 {
586 $bad[] = './';
587 $bad[] = '/';
588 }
Derek Jonese701d762010-03-02 18:17:01 -0600589
Pascal Krietec9c045a2011-04-05 14:50:41 -0400590 $str = remove_invisible_characters($str, FALSE);
Andrey Andreev7e559772013-01-29 15:38:33 +0200591
592 do
593 {
594 $old = $str;
595 $str = str_replace($bad, '', $str);
596 }
597 while ($old !== $str);
598
599 return stripslashes($str);
Derek Jonese701d762010-03-02 18:17:01 -0600600 }
601
Pascal Krietec9c045a2011-04-05 14:50:41 -0400602 // ----------------------------------------------------------------
603
604 /**
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300605 * Strip Image Tags
606 *
Andrey Andreev64354102012-10-28 14:16:02 +0200607 * @param string $str
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300608 * @return string
609 */
610 public function strip_image_tags($str)
611 {
612 return preg_replace(array('#<img\s+.*?src\s*=\s*["\'](.+?)["\'].*?\>#', '#<img\s+.*?src\s*=\s*(.+?).*?\>#'), '\\1', $str);
613 }
614
615 // ----------------------------------------------------------------
616
617 /**
Pascal Krietec9c045a2011-04-05 14:50:41 -0400618 * Compact Exploded Words
619 *
Andrey Andreev64354102012-10-28 14:16:02 +0200620 * Callback method for xss_clean() to remove whitespace from
621 * things like 'j a v a s c r i p t'.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400622 *
Andrey Andreev64354102012-10-28 14:16:02 +0200623 * @used-by CI_Security::xss_clean()
624 * @param array $matches
Timothy Warrenad475052012-04-19 13:21:06 -0400625 * @return string
Pascal Krietec9c045a2011-04-05 14:50:41 -0400626 */
627 protected function _compact_exploded_words($matches)
628 {
629 return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
630 }
631
632 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200633
Timothy Warrenad475052012-04-19 13:21:06 -0400634 /**
635 * Remove Evil HTML Attributes (like event handlers and style)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400636 *
637 * It removes the evil attribute and either:
Pascal Krietec9c045a2011-04-05 14:50:41 -0400638 *
Andrey Andreev64354102012-10-28 14:16:02 +0200639 * - Everything up until a space. For example, everything between the pipes:
640 *
641 * <code>
642 * <a |style=document.write('hello');alert('world');| class=link>
643 * </code>
644 *
645 * - Everything inside the quotes. For example, everything between the pipes:
646 *
647 * <code>
648 * <a |style="document.write('hello'); alert('world');"| class="link">
649 * </code>
650 *
651 * @param string $str The string to check
652 * @param bool $is_image Whether the input is an image
653 * @return string The string with the evil attributes removed
Pascal Krietec9c045a2011-04-05 14:50:41 -0400654 */
655 protected function _remove_evil_attributes($str, $is_image)
656 {
657 // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns
Pascal Krietec38e3b62011-11-14 13:55:00 -0500658 $evil_attributes = array('on\w*', 'style', 'xmlns', 'formaction');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400659
660 if ($is_image === TRUE)
661 {
662 /*
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200663 * Adobe Photoshop puts XML metadata into JFIF images,
Pascal Krietec9c045a2011-04-05 14:50:41 -0400664 * including namespacing, so we have to allow this for images.
665 */
666 unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
667 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200668
Pascal Krietec9c045a2011-04-05 14:50:41 -0400669 do {
Pascal Krietec38e3b62011-11-14 13:55:00 -0500670 $count = 0;
671 $attribs = array();
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200672
brian978160c7d12012-12-03 21:18:20 +0200673 // find occurrences of illegal attribute strings with quotes (042 and 047 are octal quotes)
674 preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*(\042|\047)([^\\2]*?)(\\2)/is', $str, $matches, PREG_SET_ORDER);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200675
Pascal Krietec38e3b62011-11-14 13:55:00 -0500676 foreach ($matches as $attr)
677 {
678 $attribs[] = preg_quote($attr[0], '/');
679 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200680
brian978160c7d12012-12-03 21:18:20 +0200681 // find occurrences of illegal attribute strings without quotes
682 preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*([^\s>]*)/is', $str, $matches, PREG_SET_ORDER);
David Behler07b53422011-08-15 00:25:06 +0200683
Pascal Krietec38e3b62011-11-14 13:55:00 -0500684 foreach ($matches as $attr)
685 {
686 $attribs[] = preg_quote($attr[0], '/');
687 }
688
689 // replace illegal attribute strings that are inside an html tag
690 if (count($attribs) > 0)
691 {
brian978160c7d12012-12-03 21:18:20 +0200692 $str = preg_replace('/(<?)(\/?[^><]+?)([^A-Za-z<>\-])(.*?)('.implode('|', $attribs).')(.*?)([\s><]?)([><]*)/i', '$1$2 $4$6$7$8', $str, -1, $count);
Pascal Krietec38e3b62011-11-14 13:55:00 -0500693 }
Andrey Andreev72ed4c32012-12-19 17:07:54 +0200694 }
695 while ($count);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200696
Pascal Krietec9c045a2011-04-05 14:50:41 -0400697 return $str;
698 }
David Behler07b53422011-08-15 00:25:06 +0200699
Pascal Krietec9c045a2011-04-05 14:50:41 -0400700 // --------------------------------------------------------------------
701
702 /**
703 * Sanitize Naughty HTML
704 *
Andrey Andreev64354102012-10-28 14:16:02 +0200705 * Callback method for xss_clean() to remove naughty HTML elements.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400706 *
Andrey Andreev64354102012-10-28 14:16:02 +0200707 * @used-by CI_Security::xss_clean()
708 * @param array $matches
Pascal Krietec9c045a2011-04-05 14:50:41 -0400709 * @return string
710 */
711 protected function _sanitize_naughty_html($matches)
712 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200713 return '&lt;'.$matches[1].$matches[2].$matches[3] // encode opening brace
714 // encode captured opening or closing brace to prevent recursive vectors:
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200715 .str_replace(array('>', '<'), array('&gt;', '&lt;'), $matches[4]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400716 }
717
718 // --------------------------------------------------------------------
719
720 /**
721 * JS Link Removal
722 *
Andrey Andreev64354102012-10-28 14:16:02 +0200723 * Callback method for xss_clean() to sanitize links.
724 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400725 * This limits the PCRE backtracks, making it more performance friendly
726 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200727 * PHP 5.2+ on link-heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400728 *
Andrey Andreev64354102012-10-28 14:16:02 +0200729 * @used-by CI_Security::xss_clean()
730 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400731 * @return string
732 */
733 protected function _js_link_removal($match)
734 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200735 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200736 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 +0200737 '',
738 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
739 ),
740 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400741 }
742
743 // --------------------------------------------------------------------
744
745 /**
746 * JS Image Removal
747 *
Andrey Andreev64354102012-10-28 14:16:02 +0200748 * Callback method for xss_clean() to sanitize image tags.
749 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400750 * This limits the PCRE backtracks, making it more performance friendly
751 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200752 * PHP 5.2+ on image tag heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400753 *
Andrey Andreev64354102012-10-28 14:16:02 +0200754 * @used-by CI_Security::xss_clean()
755 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400756 * @return string
757 */
758 protected function _js_img_removal($match)
759 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200760 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200761 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 +0200762 '',
763 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
764 ),
765 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400766 }
767
768 // --------------------------------------------------------------------
769
770 /**
771 * Attribute Conversion
772 *
Andrey Andreev64354102012-10-28 14:16:02 +0200773 * @used-by CI_Security::xss_clean()
774 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400775 * @return string
776 */
777 protected function _convert_attribute($match)
778 {
779 return str_replace(array('>', '<', '\\'), array('&gt;', '&lt;', '\\\\'), $match[0]);
780 }
781
782 // --------------------------------------------------------------------
783
784 /**
785 * Filter Attributes
786 *
Andrey Andreev64354102012-10-28 14:16:02 +0200787 * Filters tag attributes for consistency and safety.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400788 *
Andrey Andreev64354102012-10-28 14:16:02 +0200789 * @used-by CI_Security::_js_img_removal()
790 * @used-by CI_Security::_js_link_removal()
791 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400792 * @return string
793 */
794 protected function _filter_attributes($str)
795 {
796 $out = '';
Pascal Krietec9c045a2011-04-05 14:50:41 -0400797 if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches))
798 {
799 foreach ($matches[0] as $match)
800 {
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200801 $out .= preg_replace('#/\*.*?\*/#s', '', $match);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400802 }
803 }
804
805 return $out;
806 }
807
808 // --------------------------------------------------------------------
809
810 /**
811 * HTML Entity Decode Callback
812 *
Andrey Andreev64354102012-10-28 14:16:02 +0200813 * @used-by CI_Security::xss_clean()
814 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400815 * @return string
816 */
817 protected function _decode_entity($match)
818 {
819 return $this->entity_decode($match[0], strtoupper(config_item('charset')));
820 }
821
822 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200823
Pascal Krietec9c045a2011-04-05 14:50:41 -0400824 /**
825 * Validate URL entities
826 *
Andrey Andreev64354102012-10-28 14:16:02 +0200827 * @used-by CI_Security::xss_clean()
828 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400829 * @return string
830 */
831 protected function _validate_entities($str)
832 {
833 /*
834 * Protect GET variables in URLs
835 */
David Behler07b53422011-08-15 00:25:06 +0200836
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200837 // 901119URL5918AMP18930PROTECT8198
838 $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 -0400839
840 /*
841 * Validate standard character entities
842 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500843 * Add a semicolon if missing. We do this to enable
Pascal Krietec9c045a2011-04-05 14:50:41 -0400844 * the conversion of entities to ASCII later.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400845 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200846 $str = preg_replace('#(&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', '\\1;\\2', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400847
848 /*
849 * Validate UTF16 two byte encoding (x00)
850 *
851 * Just as above, adds a semicolon if missing.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400852 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200853 $str = preg_replace('#(&\#x?)([0-9A-F]+);?#i', '\\1\\2;', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400854
855 /*
856 * Un-Protect GET variables in URLs
857 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200858 return str_replace($this->xss_hash(), '&', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400859 }
860
861 // ----------------------------------------------------------------------
862
863 /**
864 * Do Never Allowed
865 *
Andrey Andreev64354102012-10-28 14:16:02 +0200866 * @used-by CI_Security::xss_clean()
Pascal Krietec9c045a2011-04-05 14:50:41 -0400867 * @param string
868 * @return string
869 */
870 protected function _do_never_allowed($str)
871 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200872 $str = str_replace(array_keys($this->_never_allowed_str), $this->_never_allowed_str, $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400873
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200874 foreach ($this->_never_allowed_regex as $regex)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400875 {
Wes Baker5335bc32012-04-24 15:17:14 -0400876 $str = preg_replace('#'.$regex.'#is', '[removed]', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400877 }
David Behler07b53422011-08-15 00:25:06 +0200878
Pascal Krietec9c045a2011-04-05 14:50:41 -0400879 return $str;
880 }
881
882 // --------------------------------------------------------------------
883
884 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200885 * Set CSRF Hash and Cookie
Pascal Krietec9c045a2011-04-05 14:50:41 -0400886 *
887 * @return string
888 */
889 protected function _csrf_set_hash()
890 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100891 if ($this->_csrf_hash === '')
Pascal Krietec9c045a2011-04-05 14:50:41 -0400892 {
David Behler07b53422011-08-15 00:25:06 +0200893 // If the cookie exists we will use it's value.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400894 // We don't necessarily want to regenerate it with
David Behler07b53422011-08-15 00:25:06 +0200895 // each page load since a page could contain embedded
Pascal Krietec9c045a2011-04-05 14:50:41 -0400896 // sub-pages causing this feature to fail
David Behler07b53422011-08-15 00:25:06 +0200897 if (isset($_COOKIE[$this->_csrf_cookie_name]) &&
Alexander Hofstedee2c374f2012-05-17 00:28:08 +0200898 preg_match('#^[0-9a-f]{32}$#iS', $_COOKIE[$this->_csrf_cookie_name]) === 1)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400899 {
900 return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name];
901 }
David Behler07b53422011-08-15 00:25:06 +0200902
Chris Berthed93e6f32011-09-25 10:33:25 -0400903 $this->_csrf_hash = md5(uniqid(rand(), TRUE));
904 $this->csrf_set_cookie();
Pascal Krietec9c045a2011-04-05 14:50:41 -0400905 }
906
907 return $this->_csrf_hash;
908 }
909
Derek Jonese701d762010-03-02 18:17:01 -0600910}
Derek Jonese701d762010-03-02 18:17:01 -0600911
912/* End of file Security.php */
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300913/* Location: ./system/core/Security.php */