blob: 2cf214b181921b01f2370cd052ecc3ec7d696635 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 /**
Hunter Wua8d6d3b2013-08-03 23:17:45 +080041 * List of sanitize filename strings
42 *
43 * @var array
44 */
Hunter Wu4495cc72013-08-04 12:31:52 +080045 public $filename_bad_chars = array(
Hunter Wua8d6d3b2013-08-03 23:17:45 +080046 '../', '<!--', '-->', '<', '>',
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 Andreev487d1ae2014-05-23 14:41:32 +030065 * Character set
Andrey Andreevc67c3fb2014-01-22 13:26:00 +020066 *
Andrey Andreev487d1ae2014-05-23 14:41:32 +030067 * Will be overriden by the constructor.
68 *
69 * @var string
Andrey Andreevc67c3fb2014-01-22 13:26:00 +020070 */
Andrey Andreev487d1ae2014-05-23 14:41:32 +030071 public $charset = 'UTF-8';
Andrey Andreevc67c3fb2014-01-22 13:26:00 +020072
73 /**
Andrey Andreev64354102012-10-28 14:16:02 +020074 * XSS Hash
David Behler07b53422011-08-15 00:25:06 +020075 *
Andrey Andreev64354102012-10-28 14:16:02 +020076 * Random Hash for protecting URLs.
77 *
78 * @var string
David Behler07b53422011-08-15 00:25:06 +020079 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040080 protected $_xss_hash = '';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030081
David Behler07b53422011-08-15 00:25:06 +020082 /**
Andrey Andreev64354102012-10-28 14:16:02 +020083 * CSRF Hash
David Behler07b53422011-08-15 00:25:06 +020084 *
Andrey Andreev64354102012-10-28 14:16:02 +020085 * Random hash for Cross Site Request Forgery protection cookie
86 *
87 * @var string
David Behler07b53422011-08-15 00:25:06 +020088 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040089 protected $_csrf_hash = '';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030090
David Behler07b53422011-08-15 00:25:06 +020091 /**
Andrey Andreev64354102012-10-28 14:16:02 +020092 * CSRF Expire time
David Behler07b53422011-08-15 00:25:06 +020093 *
Andrey Andreev64354102012-10-28 14:16:02 +020094 * Expiration time for Cross Site Request Forgery protection cookie.
95 * Defaults to two hours (in seconds).
96 *
97 * @var int
David Behler07b53422011-08-15 00:25:06 +020098 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040099 protected $_csrf_expire = 7200;
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300100
David Behler07b53422011-08-15 00:25:06 +0200101 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200102 * CSRF Token name
David Behler07b53422011-08-15 00:25:06 +0200103 *
Andrey Andreev64354102012-10-28 14:16:02 +0200104 * Token name for Cross Site Request Forgery protection cookie.
105 *
106 * @var string
David Behler07b53422011-08-15 00:25:06 +0200107 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400108 protected $_csrf_token_name = 'ci_csrf_token';
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300109
David Behler07b53422011-08-15 00:25:06 +0200110 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200111 * CSRF Cookie name
David Behler07b53422011-08-15 00:25:06 +0200112 *
Andrey Andreev64354102012-10-28 14:16:02 +0200113 * Cookie name for Cross Site Request Forgery protection cookie.
114 *
115 * @var string
David Behler07b53422011-08-15 00:25:06 +0200116 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400117 protected $_csrf_cookie_name = 'ci_csrf_token';
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300118
David Behler07b53422011-08-15 00:25:06 +0200119 /**
120 * List of never allowed strings
121 *
Andrey Andreev64354102012-10-28 14:16:02 +0200122 * @var array
David Behler07b53422011-08-15 00:25:06 +0200123 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400124 protected $_never_allowed_str = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400125 'document.cookie' => '[removed]',
126 'document.write' => '[removed]',
127 '.parentNode' => '[removed]',
128 '.innerHTML' => '[removed]',
Timothy Warren40403d22012-04-19 16:38:50 -0400129 '-moz-binding' => '[removed]',
130 '<!--' => '&lt;!--',
131 '-->' => '--&gt;',
132 '<![CDATA[' => '&lt;![CDATA[',
133 '<comment>' => '&lt;comment&gt;'
134 );
Derek Jonese701d762010-03-02 18:17:01 -0600135
David Behler07b53422011-08-15 00:25:06 +0200136 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200137 * List of never allowed regex replacements
David Behler07b53422011-08-15 00:25:06 +0200138 *
Andrey Andreev64354102012-10-28 14:16:02 +0200139 * @var array
David Behler07b53422011-08-15 00:25:06 +0200140 */
Pascal Krietec9c045a2011-04-05 14:50:41 -0400141 protected $_never_allowed_regex = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400142 'javascript\s*:',
Andrey Andreev1bbc5642014-01-07 12:45:27 +0200143 '(document|(document\.)?window)\.(location|on\w*)',
Timothy Warren40403d22012-04-19 16:38:50 -0400144 'expression\s*(\(|&\#40;)', // CSS and IE
145 'vbscript\s*:', // IE, surprise!
Andrey Andreeva30a7172014-02-10 09:17:25 +0200146 'wscript\s*:', // IE
Andrey Andreevf7f9dca2014-02-10 12:41:00 +0200147 'jscript\s*:', // IE
Andrey Andreeva30a7172014-02-10 09:17:25 +0200148 'vbs\s*:', // IE
Andrey Andreev43568062014-01-21 23:52:31 +0200149 'Redirect\s+30\d',
Wes Bakerd3481352012-05-07 16:49:33 -0400150 "([\"'])?data\s*:[^\\1]*?base64[^\\1]*?,[^\\1]*?\\1?"
Timothy Warren40403d22012-04-19 16:38:50 -0400151 );
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300152
Timothy Warrenad475052012-04-19 13:21:06 -0400153 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200154 * Class constructor
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300155 *
156 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -0400157 */
Greg Akera9263282010-11-10 15:26:43 -0600158 public function __construct()
Derek Jonese701d762010-03-02 18:17:01 -0600159 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200160 // Is CSRF protection enabled?
161 if (config_item('csrf_protection') === TRUE)
patworkef1a55a2011-04-09 13:04:06 +0200162 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200163 // CSRF config
164 foreach (array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key)
patworkef1a55a2011-04-09 13:04:06 +0200165 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200166 if (FALSE !== ($val = config_item($key)))
167 {
168 $this->{'_'.$key} = $val;
169 }
patworkef1a55a2011-04-09 13:04:06 +0200170 }
patworkef1a55a2011-04-09 13:04:06 +0200171
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200172 // 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 Jonesb3f10a22010-07-25 19:11:26 -0500177
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200178 // Set the CSRF hash
179 $this->_csrf_set_hash();
180 }
Derek Allard958543a2010-07-22 14:10:26 -0400181
Andrey Andreev487d1ae2014-05-23 14:41:32 +0300182 $this->charset = strtoupper(config_item('charset'));
183
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200184 log_message('debug', 'Security Class Initialized');
Derek Jonese701d762010-03-02 18:17:01 -0600185 }
186
187 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200188
Derek Jonese701d762010-03-02 18:17:01 -0600189 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200190 * CSRF Verify
Derek Jonese701d762010-03-02 18:17:01 -0600191 *
Andrew Podner4296a652012-12-17 07:51:15 -0500192 * @return CI_Security
Derek Jonese701d762010-03-02 18:17:01 -0600193 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500194 public function csrf_verify()
Derek Allard958543a2010-07-22 14:10:26 -0400195 {
Andrey Andreev5d27c432012-03-08 12:01:52 +0200196 // If it's not a POST request we will set the CSRF cookie
197 if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST')
Derek Jonese701d762010-03-02 18:17:01 -0600198 {
199 return $this->csrf_set_cookie();
200 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300201
Alex Bilbieaeb2c3e2011-08-21 16:14:54 +0100202 // 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 Jonese701d762010-03-02 18:17:01 -0600211
212 // Do the tokens exist in both the _POST and _COOKIE arrays?
Andrey Andreevf795ab52012-10-24 21:28:25 +0300213 if ( ! isset($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name])
Alex Bilbieed944a32012-06-02 11:07:47 +0100214 OR $_POST[$this->_csrf_token_name] !== $_COOKIE[$this->_csrf_cookie_name]) // Do the tokens match?
Derek Jonese701d762010-03-02 18:17:01 -0600215 {
216 $this->csrf_show_error();
217 }
218
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200219 // We kill this since we're done and we don't want to polute the _POST array
Pascal Krietec9c045a2011-04-05 14:50:41 -0400220 unset($_POST[$this->_csrf_token_name]);
Barry Mienydd671972010-10-04 16:33:58 +0200221
RS712be25a62011-12-31 16:02:04 -0200222 // 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 Andreev8a7d0782012-01-08 05:43:42 +0200229
Derek Jonesb3f10a22010-07-25 19:11:26 -0500230 $this->_csrf_set_hash();
231 $this->csrf_set_cookie();
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300232
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200233 log_message('debug', 'CSRF token verified');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400234 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Jonese701d762010-03-02 18:17:01 -0600237 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Jonese701d762010-03-02 18:17:01 -0600239 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200240 * CSRF Set Cookie
Derek Jonese701d762010-03-02 18:17:01 -0600241 *
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700242 * @codeCoverageIgnore
Andrew Podner4296a652012-12-17 07:51:15 -0500243 * @return CI_Security
Derek Jonese701d762010-03-02 18:17:01 -0600244 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500245 public function csrf_set_cookie()
Derek Jonese701d762010-03-02 18:17:01 -0600246 {
Pascal Krietec9c045a2011-04-05 14:50:41 -0400247 $expire = time() + $this->_csrf_expire;
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300248 $secure_cookie = (bool) config_item('cookie_secure');
Derek Jonese701d762010-03-02 18:17:01 -0600249
Andrey Andreev3fb02672012-10-22 16:48:01 +0300250 if ($secure_cookie && ! is_https())
Derek Jonese701d762010-03-02 18:17:01 -0600251 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200252 return FALSE;
Derek Jonese701d762010-03-02 18:17:01 -0600253 }
Derek Allard958543a2010-07-22 14:10:26 -0400254
freewil4ad0fd82012-03-13 22:37:42 -0400255 setcookie(
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300256 $this->_csrf_cookie_name,
257 $this->_csrf_hash,
258 $expire,
259 config_item('cookie_path'),
260 config_item('cookie_domain'),
freewil4ad0fd82012-03-13 22:37:42 -0400261 $secure_cookie,
262 config_item('cookie_httponly')
263 );
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200264 log_message('debug', 'CRSF cookie Set');
David Behler07b53422011-08-15 00:25:06 +0200265
Pascal Krietec9c045a2011-04-05 14:50:41 -0400266 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600267 }
268
269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Jonese701d762010-03-02 18:17:01 -0600271 /**
272 * Show CSRF Error
273 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400274 * @return void
Derek Jonese701d762010-03-02 18:17:01 -0600275 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500276 public function csrf_show_error()
Derek Jonese701d762010-03-02 18:17:01 -0600277 {
278 show_error('The action you have requested is not allowed.');
279 }
280
281 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Jonese701d762010-03-02 18:17:01 -0600283 /**
David Behler07b53422011-08-15 00:25:06 +0200284 * Get CSRF Hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400285 *
Andrey Andreev64354102012-10-28 14:16:02 +0200286 * @see CI_Security::$_csrf_hash
287 * @return string CSRF hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400288 */
289 public function get_csrf_hash()
290 {
291 return $this->_csrf_hash;
292 }
293
294 // --------------------------------------------------------------------
295
296 /**
297 * Get CSRF Token Name
298 *
Andrey Andreev64354102012-10-28 14:16:02 +0200299 * @see CI_Security::$_csrf_token_name
300 * @return string CSRF token name
Pascal Krietec9c045a2011-04-05 14:50:41 -0400301 */
302 public function get_csrf_token_name()
303 {
304 return $this->_csrf_token_name;
305 }
306
307 // --------------------------------------------------------------------
308
309 /**
Derek Jonese701d762010-03-02 18:17:01 -0600310 * XSS Clean
311 *
312 * Sanitizes data so that Cross Site Scripting Hacks can be
Andrey Andreev64354102012-10-28 14:16:02 +0200313 * prevented. This method does a fair amount of work but
Derek Jonese701d762010-03-02 18:17:01 -0600314 * it is extremely thorough, designed to prevent even the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500315 * most obscure XSS attempts. Nothing is ever 100% foolproof,
Derek Jonese701d762010-03-02 18:17:01 -0600316 * of course, but I haven't been able to get anything passed
317 * the filter.
318 *
Andrey Andreev64354102012-10-28 14:16:02 +0200319 * 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 Jonese701d762010-03-02 18:17:01 -0600322 *
Andrey Andreev64354102012-10-28 14:16:02 +0200323 * @link http://channel.bitflux.ch/wiki/XSS_Prevention
324 * Based in part on some code and ideas from Bitflux.
Derek Jonese701d762010-03-02 18:17:01 -0600325 *
Andrey Andreev64354102012-10-28 14:16:02 +0200326 * @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 Jonese701d762010-03-02 18:17:01 -0600330 *
Andrey Andreev64354102012-10-28 14:16:02 +0200331 * @param string|string[] $str Input data
332 * @param bool $is_image Whether the input is an image
Derek Jonese701d762010-03-02 18:17:01 -0600333 * @return string
334 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500335 public function xss_clean($str, $is_image = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600336 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200337 // Is the string an array?
Derek Jonese701d762010-03-02 18:17:01 -0600338 if (is_array($str))
339 {
340 while (list($key) = each($str))
341 {
342 $str[$key] = $this->xss_clean($str[$key]);
343 }
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Jonese701d762010-03-02 18:17:01 -0600345 return $str;
346 }
347
Andrey Andreev487d1ae2014-05-23 14:41:32 +0300348 // Remove Invisible Characters
349 $str = remove_invisible_characters($str);
Derek Jonese701d762010-03-02 18:17:01 -0600350
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 Jonese701d762010-03-02 18:17:01 -0600359 */
Andrey Andreev29e12642014-02-10 13:24:44 +0200360 do
361 {
362 $str = rawurldecode($str);
363 }
364 while (preg_match('/%[0-9a-f]{2,}/i', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Jonese701d762010-03-02 18:17:01 -0600366 /*
Barry Mienydd671972010-10-04 16:33:58 +0200367 * Convert character entities to ASCII
Derek Jonese701d762010-03-02 18:17:01 -0600368 *
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 Jonese701d762010-03-02 18:17:01 -0600372 */
Derek Jonese701d762010-03-02 18:17:01 -0600373 $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str);
brian97807ccbe52012-12-11 20:24:12 +0200374 $str = preg_replace_callback('/<\w+.*/si', array($this, '_decode_entity'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600375
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200376 // Remove Invisible Characters Again!
Greg Aker757dda62010-04-14 19:06:19 -0500377 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Jonese701d762010-03-02 18:17:01 -0600379 /*
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 Behler07b53422011-08-15 00:25:06 +0200384 * NOTE: preg_replace was found to be amazingly slow here on
Pascal Krietec9c045a2011-04-05 14:50:41 -0400385 * large blocks of data, so we use str_replace.
Derek Jonese701d762010-03-02 18:17:01 -0600386 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200387 $str = str_replace("\t", ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200388
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200389 // Capture converted string for later comparison
Derek Jonese701d762010-03-02 18:17:01 -0600390 $converted_string = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200391
Pascal Krietec9c045a2011-04-05 14:50:41 -0400392 // Remove Strings that are never allowed
393 $str = $this->_do_never_allowed($str);
Derek Jonese701d762010-03-02 18:17:01 -0600394
395 /*
396 * Makes PHP tags safe
397 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400398 * Note: XML tags are inadvertently replaced too:
Derek Jonese701d762010-03-02 18:17:01 -0600399 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400400 * <?xml
Derek Jonese701d762010-03-02 18:17:01 -0600401 *
402 * But it doesn't seem to pose a problem.
Derek Jonese701d762010-03-02 18:17:01 -0600403 */
404 if ($is_image === TRUE)
405 {
David Behler07b53422011-08-15 00:25:06 +0200406 // Images have a tendency to have the PHP short opening and
407 // closing tags every so often so we skip those and only
Pascal Krietec9c045a2011-04-05 14:50:41 -0400408 // do the long opening tags.
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200409 $str = preg_replace('/<\?(php)/i', '&lt;?\\1', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600410 }
411 else
412 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200413 $str = str_replace(array('<?', '?'.'>'), array('&lt;?', '?&gt;'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Jonese701d762010-03-02 18:17:01 -0600416 /*
417 * Compact any exploded words
418 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500419 * This corrects words like: j a v a s c r i p t
Derek Jonese701d762010-03-02 18:17:01 -0600420 * These words are compacted back to their correct state.
Derek Jonese701d762010-03-02 18:17:01 -0600421 */
Pascal Krietec9c045a2011-04-05 14:50:41 -0400422 $words = array(
Andrey Andreeva30a7172014-02-10 09:17:25 +0200423 'javascript', 'expression', 'vbscript', 'jscript', 'wscript',
424 'vbs', 'script', 'base64', 'applet', 'alert', 'document',
425 'write', 'cookie', 'window', 'confirm', 'prompt'
Timothy Warren40403d22012-04-19 16:38:50 -0400426 );
David Behler07b53422011-08-15 00:25:06 +0200427
Derek Jonese701d762010-03-02 18:17:01 -0600428 foreach ($words as $word)
429 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200430 $word = implode('\s*', str_split($word)).'\s*';
Derek Jonese701d762010-03-02 18:17:01 -0600431
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 Andreev3d113bd2011-10-05 00:03:20 +0300434 $str = preg_replace_callback('#('.substr($word, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Jonese701d762010-03-02 18:17:01 -0600437 /*
438 * Remove disallowed Javascript in links or img tags
David Behler07b53422011-08-15 00:25:06 +0200439 * 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 Krietec9c045a2011-04-05 14:50:41 -0400441 * preg_match(), especially if the pattern exists in the string
Andrey Andreev12445ca2014-01-25 01:55:52 +0200442 *
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 Jonese701d762010-03-02 18:17:01 -0600448 */
449 do
450 {
451 $original = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200452
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200453 if (preg_match('/<a/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600454 {
Andrey Andreev46d20722014-03-18 23:08:59 +0200455 $str = preg_replace_callback('#<a[^a-z0-9>]+([^>]*?)(?:>|$)#si', array($this, '_js_link_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200458 if (preg_match('/<img/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600459 {
Andrey Andreevebb3aa02014-03-18 19:18:19 +0200460 $str = preg_replace_callback('#<img[^a-z0-9]+([^>]*?)(?:\s?/?>|$)#si', array($this, '_js_img_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
vlakoffa81f60c2012-07-02 15:20:11 +0200463 if (preg_match('/script|xss/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600464 {
vlakoffa81f60c2012-07-02 15:20:11 +0200465 $str = preg_replace('#</*(?:script|xss).*?>#si', '[removed]', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600466 }
467 }
vlakoffa81f60c2012-07-02 15:20:11 +0200468 while ($original !== $str);
Derek Jonese701d762010-03-02 18:17:01 -0600469
470 unset($original);
471
Pascal Krietec9c045a2011-04-05 14:50:41 -0400472 // Remove evil attributes such as style, onclick and xmlns
473 $str = $this->_remove_evil_attributes($str, $is_image);
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Jonese701d762010-03-02 18:17:01 -0600475 /*
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: &lt;blink&gt;
Derek Jonese701d762010-03-02 18:17:01 -0600483 */
Andrey Andreeva30a7172014-02-10 09:17:25 +0200484 $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 Jonese701d762010-03-02 18:17:01 -0600485 $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 Andreevbb488dc2012-01-07 23:35:16 +0200492 * that are disallowed. Rather than removing the
Derek Jonese701d762010-03-02 18:17:01 -0600493 * code, it simply converts the parenthesis to entities
494 * rendering the code un-executable.
495 *
496 * For example: eval('some code')
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200497 * Becomes: eval&#40;'some code'&#41;
Derek Jonese701d762010-03-02 18:17:01 -0600498 */
Andrey Andreeva30a7172014-02-10 09:17:25 +0200499 $str = preg_replace('#(alert|prompt|confirm|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si',
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200500 '\\1\\2&#40;\\3&#41;',
501 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200502
Pascal Krietec9c045a2011-04-05 14:50:41 -0400503 // 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 Jonese701d762010-03-02 18:17:01 -0600507
508 /*
Pascal Krietec9c045a2011-04-05 14:50:41 -0400509 * Images are Handled in a Special Way
David Behler07b53422011-08-15 00:25:06 +0200510 * - Essentially, we want to know that after all of the character
511 * conversion is done whether any unwanted, likely XSS, code was found.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400512 * If not, we return TRUE, as the image is clean.
David Behler07b53422011-08-15 00:25:06 +0200513 * 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 Krietec9c045a2011-04-05 14:50:41 -0400515 * code found and removed/changed during processing.
Derek Jonese701d762010-03-02 18:17:01 -0600516 */
Derek Jonese701d762010-03-02 18:17:01 -0600517 if ($is_image === TRUE)
518 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200519 return ($str === $converted_string);
Derek Jonese701d762010-03-02 18:17:01 -0600520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200522 log_message('debug', 'XSS Filtering completed');
Derek Jonese701d762010-03-02 18:17:01 -0600523 return $str;
524 }
525
526 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Jonese701d762010-03-02 18:17:01 -0600528 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200529 * XSS Hash
Derek Jonese701d762010-03-02 18:17:01 -0600530 *
Andrey Andreev64354102012-10-28 14:16:02 +0200531 * Generates the XSS hash if needed and returns it.
532 *
533 * @see CI_Security::$_xss_hash
534 * @return string XSS hash
Derek Jonese701d762010-03-02 18:17:01 -0600535 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500536 public function xss_hash()
Barry Mienydd671972010-10-04 16:33:58 +0200537 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100538 if ($this->_xss_hash === '')
Derek Jonese701d762010-03-02 18:17:01 -0600539 {
vlakoff06127562013-03-30 00:06:39 +0100540 $this->_xss_hash = md5(uniqid(mt_rand()));
Derek Jonese701d762010-03-02 18:17:01 -0600541 }
542
Pascal Krietec9c045a2011-04-05 14:50:41 -0400543 return $this->_xss_hash;
Derek Jonese701d762010-03-02 18:17:01 -0600544 }
545
546 // --------------------------------------------------------------------
547
548 /**
Derek Jonesa0911472010-03-30 10:33:09 -0500549 * HTML Entities Decode
550 *
Andrey Andreev64354102012-10-28 14:16:02 +0200551 * A replacement for html_entity_decode()
Derek Jonesa0911472010-03-30 10:33:09 -0500552 *
Pascal Krietec38e3b62011-11-14 13:55:00 -0500553 * 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 Andreevbb488dc2012-01-07 23:35:16 +0200556 * correctly. html_entity_decode() does not convert entities without
Pascal Krietec38e3b62011-11-14 13:55:00 -0500557 * semicolons, so we are left with our own little solution here. Bummer.
Derek Jonesa0911472010-03-30 10:33:09 -0500558 *
Andrey Andreev64354102012-10-28 14:16:02 +0200559 * @link http://php.net/html-entity-decode
560 *
561 * @param string $str Input
562 * @param string $charset Character set
Derek Jonesa0911472010-03-30 10:33:09 -0500563 * @return string
564 */
freewil8cc0cfe2011-08-27 21:53:00 -0400565 public function entity_decode($str, $charset = NULL)
Derek Jonesa0911472010-03-30 10:33:09 -0500566 {
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300567 if (strpos($str, '&') === FALSE)
freewil5c9b0d12011-08-28 12:15:23 -0400568 {
569 return $str;
570 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300571
Andrey Andreev487d1ae2014-05-23 14:41:32 +0300572 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 Mienydd671972010-10-04 16:33:58 +0200578
brian978638a9d22012-12-18 13:25:54 +0200579 do
580 {
Andrey Andreeve7a2aa02014-03-18 18:44:53 +0200581 $str_compare = $str;
brian97807ccbe52012-12-11 20:24:12 +0200582
Andrey Andreev487d1ae2014-05-23 14:41:32 +0300583 // 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[':'] = '&colon;';
595 $_entities['('] = '&lpar;';
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 );
brian978638a9d22012-12-18 13:25:54 +0200621 }
Andrey Andreeve7a2aa02014-03-18 18:44:53 +0200622 while ($str_compare !== $str);
brian978638a9d22012-12-18 13:25:54 +0200623 return $str;
Derek Jonesa0911472010-03-30 10:33:09 -0500624 }
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Jonesa0911472010-03-30 10:33:09 -0500626 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Jonesa0911472010-03-30 10:33:09 -0500628 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200629 * Sanitize Filename
Derek Jonese701d762010-03-02 18:17:01 -0600630 *
Andrey Andreev64354102012-10-28 14:16:02 +0200631 * @param string $str Input file name
632 * @param bool $relative_path Whether to preserve paths
Derek Jonese701d762010-03-02 18:17:01 -0600633 * @return string
634 */
Hunter Wu8df33522013-08-03 22:36:05 +0800635 public function sanitize_filename($str, $relative_path = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600636 {
Hunter Wu4495cc72013-08-04 12:31:52 +0800637 $bad = $this->filename_bad_chars;
David Behler07b53422011-08-15 00:25:06 +0200638
Derek Jones2ef37592010-10-06 17:51:59 -0500639 if ( ! $relative_path)
640 {
641 $bad[] = './';
642 $bad[] = '/';
643 }
Derek Jonese701d762010-03-02 18:17:01 -0600644
Pascal Krietec9c045a2011-04-05 14:50:41 -0400645 $str = remove_invisible_characters($str, FALSE);
Andrey Andreev7e559772013-01-29 15:38:33 +0200646
647 do
648 {
649 $old = $str;
650 $str = str_replace($bad, '', $str);
651 }
652 while ($old !== $str);
653
654 return stripslashes($str);
Derek Jonese701d762010-03-02 18:17:01 -0600655 }
656
Pascal Krietec9c045a2011-04-05 14:50:41 -0400657 // ----------------------------------------------------------------
658
659 /**
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300660 * Strip Image Tags
661 *
Andrey Andreev64354102012-10-28 14:16:02 +0200662 * @param string $str
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300663 * @return string
664 */
665 public function strip_image_tags($str)
666 {
David Cox Jr46e77e02013-10-03 16:56:04 -0400667 return preg_replace(array('#<img[\s/]+.*?src\s*=\s*["\'](.+?)["\'].*?\>#', '#<img[\s/]+.*?src\s*=\s*(.+?).*?\>#'), '\\1', $str);
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300668 }
669
670 // ----------------------------------------------------------------
671
672 /**
Pascal Krietec9c045a2011-04-05 14:50:41 -0400673 * Compact Exploded Words
674 *
Andrey Andreev64354102012-10-28 14:16:02 +0200675 * Callback method for xss_clean() to remove whitespace from
676 * things like 'j a v a s c r i p t'.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400677 *
Andrey Andreev64354102012-10-28 14:16:02 +0200678 * @used-by CI_Security::xss_clean()
679 * @param array $matches
Timothy Warrenad475052012-04-19 13:21:06 -0400680 * @return string
Pascal Krietec9c045a2011-04-05 14:50:41 -0400681 */
682 protected function _compact_exploded_words($matches)
683 {
684 return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
685 }
686
687 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200688
Timothy Warrenad475052012-04-19 13:21:06 -0400689 /**
690 * Remove Evil HTML Attributes (like event handlers and style)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400691 *
692 * It removes the evil attribute and either:
Pascal Krietec9c045a2011-04-05 14:50:41 -0400693 *
Andrey Andreev64354102012-10-28 14:16:02 +0200694 * - 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 Krietec9c045a2011-04-05 14:50:41 -0400709 */
710 protected function _remove_evil_attributes($str, $is_image)
711 {
Andrey Andreevadf3bde2014-01-25 16:59:17 +0200712 $evil_attributes = array('on\w*', 'style', 'xmlns', 'formaction', 'form', 'xlink:href');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400713
714 if ($is_image === TRUE)
715 {
716 /*
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200717 * Adobe Photoshop puts XML metadata into JFIF images,
Pascal Krietec9c045a2011-04-05 14:50:41 -0400718 * including namespacing, so we have to allow this for images.
719 */
720 unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
721 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200722
Pascal Krietec9c045a2011-04-05 14:50:41 -0400723 do {
Pascal Krietec38e3b62011-11-14 13:55:00 -0500724 $count = 0;
725 $attribs = array();
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200726
brian978160c7d12012-12-03 21:18:20 +0200727 // find occurrences of illegal attribute strings with quotes (042 and 047 are octal quotes)
Andrey Andreevdbd999f2014-01-25 22:55:21 +0200728 preg_match_all('/(?<!\w)('.implode('|', $evil_attributes).')\s*=\s*(\042|\047)([^\\2]*?)(\\2)/is', $str, $matches, PREG_SET_ORDER);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200729
Pascal Krietec38e3b62011-11-14 13:55:00 -0500730 foreach ($matches as $attr)
731 {
732 $attribs[] = preg_quote($attr[0], '/');
733 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200734
brian978160c7d12012-12-03 21:18:20 +0200735 // find occurrences of illegal attribute strings without quotes
Andrey Andreevdbd999f2014-01-25 22:55:21 +0200736 preg_match_all('/(?<!\w)('.implode('|', $evil_attributes).')\s*=\s*([^\s>]*)/is', $str, $matches, PREG_SET_ORDER);
David Behler07b53422011-08-15 00:25:06 +0200737
Pascal Krietec38e3b62011-11-14 13:55:00 -0500738 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 {
brian978160c7d12012-12-03 21:18:20 +0200746 $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 -0500747 }
Andrey Andreev72ed4c32012-12-19 17:07:54 +0200748 }
749 while ($count);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200750
Pascal Krietec9c045a2011-04-05 14:50:41 -0400751 return $str;
752 }
David Behler07b53422011-08-15 00:25:06 +0200753
Pascal Krietec9c045a2011-04-05 14:50:41 -0400754 // --------------------------------------------------------------------
755
756 /**
757 * Sanitize Naughty HTML
758 *
Andrey Andreev64354102012-10-28 14:16:02 +0200759 * Callback method for xss_clean() to remove naughty HTML elements.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400760 *
Andrey Andreev64354102012-10-28 14:16:02 +0200761 * @used-by CI_Security::xss_clean()
762 * @param array $matches
Pascal Krietec9c045a2011-04-05 14:50:41 -0400763 * @return string
764 */
765 protected function _sanitize_naughty_html($matches)
766 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200767 return '&lt;'.$matches[1].$matches[2].$matches[3] // encode opening brace
768 // encode captured opening or closing brace to prevent recursive vectors:
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200769 .str_replace(array('>', '<'), array('&gt;', '&lt;'), $matches[4]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400770 }
771
772 // --------------------------------------------------------------------
773
774 /**
775 * JS Link Removal
776 *
Andrey Andreev64354102012-10-28 14:16:02 +0200777 * Callback method for xss_clean() to sanitize links.
778 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400779 * This limits the PCRE backtracks, making it more performance friendly
780 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200781 * PHP 5.2+ on link-heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400782 *
Andrey Andreev64354102012-10-28 14:16:02 +0200783 * @used-by CI_Security::xss_clean()
784 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400785 * @return string
786 */
787 protected function _js_link_removal($match)
788 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200789 return str_replace($match[1],
Andrey Andreeva30a7172014-02-10 09:17:25 +0200790 preg_replace('#href=.*?(?:(?:alert|prompt|confirm)(?:\(|&\#40;)|javascript:|livescript:|mocha:|charset=|window\.|document\.|\.cookie|<script|<xss|data\s*:)#si',
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200791 '',
792 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
793 ),
794 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400795 }
796
797 // --------------------------------------------------------------------
798
799 /**
800 * JS Image Removal
801 *
Andrey Andreev64354102012-10-28 14:16:02 +0200802 * Callback method for xss_clean() to sanitize image tags.
803 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400804 * This limits the PCRE backtracks, making it more performance friendly
805 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200806 * PHP 5.2+ on image tag heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400807 *
Andrey Andreev64354102012-10-28 14:16:02 +0200808 * @used-by CI_Security::xss_clean()
809 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400810 * @return string
811 */
812 protected function _js_img_removal($match)
813 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200814 return str_replace($match[1],
Andrey Andreeva30a7172014-02-10 09:17:25 +0200815 preg_replace('#src=.*?(?:(?:alert|prompt|confirm)(?:\(|&\#40;)|javascript:|livescript:|mocha:|charset=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si',
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200816 '',
817 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
818 ),
819 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400820 }
821
822 // --------------------------------------------------------------------
823
824 /**
825 * Attribute Conversion
826 *
Andrey Andreev64354102012-10-28 14:16:02 +0200827 * @used-by CI_Security::xss_clean()
828 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400829 * @return string
830 */
831 protected function _convert_attribute($match)
832 {
833 return str_replace(array('>', '<', '\\'), array('&gt;', '&lt;', '\\\\'), $match[0]);
834 }
835
836 // --------------------------------------------------------------------
837
838 /**
839 * Filter Attributes
840 *
Andrey Andreev64354102012-10-28 14:16:02 +0200841 * Filters tag attributes for consistency and safety.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400842 *
Andrey Andreev64354102012-10-28 14:16:02 +0200843 * @used-by CI_Security::_js_img_removal()
844 * @used-by CI_Security::_js_link_removal()
845 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400846 * @return string
847 */
848 protected function _filter_attributes($str)
849 {
850 $out = '';
Pascal Krietec9c045a2011-04-05 14:50:41 -0400851 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 Andreev4562f2c2012-01-09 23:39:50 +0200855 $out .= preg_replace('#/\*.*?\*/#s', '', $match);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400856 }
857 }
858
859 return $out;
860 }
861
862 // --------------------------------------------------------------------
863
864 /**
865 * HTML Entity Decode Callback
866 *
Andrey Andreev64354102012-10-28 14:16:02 +0200867 * @used-by CI_Security::xss_clean()
868 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400869 * @return string
870 */
871 protected function _decode_entity($match)
872 {
Andrey Andreev487d1ae2014-05-23 14:41:32 +0300873 // 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 Andreevc67c3fb2014-01-22 13:26:00 +0200882 );
Pascal Krietec9c045a2011-04-05 14:50:41 -0400883 }
884
885 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200886
Pascal Krietec9c045a2011-04-05 14:50:41 -0400887 /**
Pascal Krietec9c045a2011-04-05 14:50:41 -0400888 * Do Never Allowed
889 *
Andrey Andreev64354102012-10-28 14:16:02 +0200890 * @used-by CI_Security::xss_clean()
Pascal Krietec9c045a2011-04-05 14:50:41 -0400891 * @param string
892 * @return string
893 */
894 protected function _do_never_allowed($str)
895 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200896 $str = str_replace(array_keys($this->_never_allowed_str), $this->_never_allowed_str, $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400897
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200898 foreach ($this->_never_allowed_regex as $regex)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400899 {
Wes Baker5335bc32012-04-24 15:17:14 -0400900 $str = preg_replace('#'.$regex.'#is', '[removed]', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400901 }
David Behler07b53422011-08-15 00:25:06 +0200902
Pascal Krietec9c045a2011-04-05 14:50:41 -0400903 return $str;
904 }
905
906 // --------------------------------------------------------------------
907
908 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200909 * Set CSRF Hash and Cookie
Pascal Krietec9c045a2011-04-05 14:50:41 -0400910 *
911 * @return string
912 */
913 protected function _csrf_set_hash()
914 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100915 if ($this->_csrf_hash === '')
Pascal Krietec9c045a2011-04-05 14:50:41 -0400916 {
vlakoff3a3d5f62013-10-17 22:22:16 +0200917 // If the cookie exists we will use its value.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400918 // We don't necessarily want to regenerate it with
David Behler07b53422011-08-15 00:25:06 +0200919 // each page load since a page could contain embedded
Pascal Krietec9c045a2011-04-05 14:50:41 -0400920 // sub-pages causing this feature to fail
David Behler07b53422011-08-15 00:25:06 +0200921 if (isset($_COOKIE[$this->_csrf_cookie_name]) &&
Alexander Hofstedee2c374f2012-05-17 00:28:08 +0200922 preg_match('#^[0-9a-f]{32}$#iS', $_COOKIE[$this->_csrf_cookie_name]) === 1)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400923 {
924 return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name];
925 }
David Behler07b53422011-08-15 00:25:06 +0200926
vlakoff3a3d5f62013-10-17 22:22:16 +0200927 $this->_csrf_hash = md5(uniqid(mt_rand(), TRUE));
Chris Berthed93e6f32011-09-25 10:33:25 -0400928 $this->csrf_set_cookie();
Pascal Krietec9c045a2011-04-05 14:50:41 -0400929 }
930
931 return $this->_csrf_hash;
932 }
933
Derek Jonese701d762010-03-02 18:17:01 -0600934}
Derek Jonese701d762010-03-02 18:17:01 -0600935
936/* End of file Security.php */
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300937/* Location: ./system/core/Security.php */