blob: 196d6114467ccb08e6cb50f70bd874f380df4730 [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 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200118 * Class constructor
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300119 *
120 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -0400121 */
Greg Akera9263282010-11-10 15:26:43 -0600122 public function __construct()
Derek Jonese701d762010-03-02 18:17:01 -0600123 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200124 // Is CSRF protection enabled?
125 if (config_item('csrf_protection') === TRUE)
patworkef1a55a2011-04-09 13:04:06 +0200126 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200127 // CSRF config
128 foreach (array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key)
patworkef1a55a2011-04-09 13:04:06 +0200129 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200130 if (FALSE !== ($val = config_item($key)))
131 {
132 $this->{'_'.$key} = $val;
133 }
patworkef1a55a2011-04-09 13:04:06 +0200134 }
patworkef1a55a2011-04-09 13:04:06 +0200135
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200136 // Append application specific cookie prefix
137 if (config_item('cookie_prefix'))
138 {
139 $this->_csrf_cookie_name = config_item('cookie_prefix').$this->_csrf_cookie_name;
140 }
Derek Jonesb3f10a22010-07-25 19:11:26 -0500141
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200142 // Set the CSRF hash
143 $this->_csrf_set_hash();
144 }
Derek Allard958543a2010-07-22 14:10:26 -0400145
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200146 log_message('debug', 'Security Class Initialized');
Derek Jonese701d762010-03-02 18:17:01 -0600147 }
148
149 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Jonese701d762010-03-02 18:17:01 -0600151 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200152 * CSRF Verify
Derek Jonese701d762010-03-02 18:17:01 -0600153 *
Andrew Podner4296a652012-12-17 07:51:15 -0500154 * @return CI_Security
Derek Jonese701d762010-03-02 18:17:01 -0600155 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500156 public function csrf_verify()
Derek Allard958543a2010-07-22 14:10:26 -0400157 {
Andrey Andreev5d27c432012-03-08 12:01:52 +0200158 // If it's not a POST request we will set the CSRF cookie
159 if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST')
Derek Jonese701d762010-03-02 18:17:01 -0600160 {
161 return $this->csrf_set_cookie();
162 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300163
Alex Bilbieaeb2c3e2011-08-21 16:14:54 +0100164 // Check if URI has been whitelisted from CSRF checks
165 if ($exclude_uris = config_item('csrf_exclude_uris'))
166 {
167 $uri = load_class('URI', 'core');
168 if (in_array($uri->uri_string(), $exclude_uris))
169 {
170 return $this;
171 }
172 }
Derek Jonese701d762010-03-02 18:17:01 -0600173
174 // Do the tokens exist in both the _POST and _COOKIE arrays?
Andrey Andreevf795ab52012-10-24 21:28:25 +0300175 if ( ! isset($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name])
Alex Bilbieed944a32012-06-02 11:07:47 +0100176 OR $_POST[$this->_csrf_token_name] !== $_COOKIE[$this->_csrf_cookie_name]) // Do the tokens match?
Derek Jonese701d762010-03-02 18:17:01 -0600177 {
178 $this->csrf_show_error();
179 }
180
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200181 // We kill this since we're done and we don't want to polute the _POST array
Pascal Krietec9c045a2011-04-05 14:50:41 -0400182 unset($_POST[$this->_csrf_token_name]);
Barry Mienydd671972010-10-04 16:33:58 +0200183
RS712be25a62011-12-31 16:02:04 -0200184 // Regenerate on every submission?
185 if (config_item('csrf_regenerate'))
186 {
187 // Nothing should last forever
188 unset($_COOKIE[$this->_csrf_cookie_name]);
189 $this->_csrf_hash = '';
190 }
Andrey Andreev8a7d0782012-01-08 05:43:42 +0200191
Derek Jonesb3f10a22010-07-25 19:11:26 -0500192 $this->_csrf_set_hash();
193 $this->csrf_set_cookie();
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300194
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200195 log_message('debug', 'CSRF token verified');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400196 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Jonese701d762010-03-02 18:17:01 -0600199 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Jonese701d762010-03-02 18:17:01 -0600201 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200202 * CSRF Set Cookie
Derek Jonese701d762010-03-02 18:17:01 -0600203 *
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700204 * @codeCoverageIgnore
Andrew Podner4296a652012-12-17 07:51:15 -0500205 * @return CI_Security
Derek Jonese701d762010-03-02 18:17:01 -0600206 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500207 public function csrf_set_cookie()
Derek Jonese701d762010-03-02 18:17:01 -0600208 {
Pascal Krietec9c045a2011-04-05 14:50:41 -0400209 $expire = time() + $this->_csrf_expire;
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300210 $secure_cookie = (bool) config_item('cookie_secure');
Derek Jonese701d762010-03-02 18:17:01 -0600211
Andrey Andreev3fb02672012-10-22 16:48:01 +0300212 if ($secure_cookie && ! is_https())
Derek Jonese701d762010-03-02 18:17:01 -0600213 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200214 return FALSE;
Derek Jonese701d762010-03-02 18:17:01 -0600215 }
Derek Allard958543a2010-07-22 14:10:26 -0400216
freewil4ad0fd82012-03-13 22:37:42 -0400217 setcookie(
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300218 $this->_csrf_cookie_name,
219 $this->_csrf_hash,
220 $expire,
221 config_item('cookie_path'),
222 config_item('cookie_domain'),
freewil4ad0fd82012-03-13 22:37:42 -0400223 $secure_cookie,
224 config_item('cookie_httponly')
225 );
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200226 log_message('debug', 'CRSF cookie Set');
David Behler07b53422011-08-15 00:25:06 +0200227
Pascal Krietec9c045a2011-04-05 14:50:41 -0400228 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600229 }
230
231 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Jonese701d762010-03-02 18:17:01 -0600233 /**
234 * Show CSRF Error
235 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400236 * @return void
Derek Jonese701d762010-03-02 18:17:01 -0600237 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500238 public function csrf_show_error()
Derek Jonese701d762010-03-02 18:17:01 -0600239 {
240 show_error('The action you have requested is not allowed.');
241 }
242
243 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Jonese701d762010-03-02 18:17:01 -0600245 /**
David Behler07b53422011-08-15 00:25:06 +0200246 * Get CSRF Hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400247 *
Andrey Andreev64354102012-10-28 14:16:02 +0200248 * @see CI_Security::$_csrf_hash
249 * @return string CSRF hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400250 */
251 public function get_csrf_hash()
252 {
253 return $this->_csrf_hash;
254 }
255
256 // --------------------------------------------------------------------
257
258 /**
259 * Get CSRF Token Name
260 *
Andrey Andreev64354102012-10-28 14:16:02 +0200261 * @see CI_Security::$_csrf_token_name
262 * @return string CSRF token name
Pascal Krietec9c045a2011-04-05 14:50:41 -0400263 */
264 public function get_csrf_token_name()
265 {
266 return $this->_csrf_token_name;
267 }
268
269 // --------------------------------------------------------------------
270
271 /**
Derek Jonese701d762010-03-02 18:17:01 -0600272 * XSS Clean
273 *
274 * Sanitizes data so that Cross Site Scripting Hacks can be
Andrey Andreev64354102012-10-28 14:16:02 +0200275 * prevented. This method does a fair amount of work but
Derek Jonese701d762010-03-02 18:17:01 -0600276 * it is extremely thorough, designed to prevent even the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500277 * most obscure XSS attempts. Nothing is ever 100% foolproof,
Derek Jonese701d762010-03-02 18:17:01 -0600278 * of course, but I haven't been able to get anything passed
279 * the filter.
280 *
Andrey Andreev64354102012-10-28 14:16:02 +0200281 * Note: Should only be used to deal with data upon submission.
282 * It's not something that should be used for general
283 * runtime processing.
Derek Jonese701d762010-03-02 18:17:01 -0600284 *
Andrey Andreev64354102012-10-28 14:16:02 +0200285 * @link http://channel.bitflux.ch/wiki/XSS_Prevention
286 * Based in part on some code and ideas from Bitflux.
Derek Jonese701d762010-03-02 18:17:01 -0600287 *
Andrey Andreev64354102012-10-28 14:16:02 +0200288 * @link http://ha.ckers.org/xss.html
289 * To help develop this script I used this great list of
290 * vulnerabilities along with a few other hacks I've
291 * harvested from examining vulnerabilities in other programs.
Derek Jonese701d762010-03-02 18:17:01 -0600292 *
Andrey Andreev64354102012-10-28 14:16:02 +0200293 * @param string|string[] $str Input data
294 * @param bool $is_image Whether the input is an image
Derek Jonese701d762010-03-02 18:17:01 -0600295 * @return string
296 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500297 public function xss_clean($str, $is_image = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600298 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200299 // Is the string an array?
Derek Jonese701d762010-03-02 18:17:01 -0600300 if (is_array($str))
301 {
302 while (list($key) = each($str))
303 {
304 $str[$key] = $this->xss_clean($str[$key]);
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Jonese701d762010-03-02 18:17:01 -0600307 return $str;
308 }
309
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200310 // Remove Invisible Characters and validate entities in URLs
311 $str = $this->_validate_entities(remove_invisible_characters($str));
Derek Jonese701d762010-03-02 18:17:01 -0600312
313 /*
314 * URL Decode
315 *
316 * Just in case stuff like this is submitted:
317 *
318 * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
319 *
320 * Note: Use rawurldecode() so it does not remove plus signs
Derek Jonese701d762010-03-02 18:17:01 -0600321 */
322 $str = rawurldecode($str);
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Jonese701d762010-03-02 18:17:01 -0600324 /*
Barry Mienydd671972010-10-04 16:33:58 +0200325 * Convert character entities to ASCII
Derek Jonese701d762010-03-02 18:17:01 -0600326 *
327 * This permits our tests below to work reliably.
328 * We only convert entities that are within tags since
329 * these are the ones that will pose security problems.
Derek Jonese701d762010-03-02 18:17:01 -0600330 */
Derek Jonese701d762010-03-02 18:17:01 -0600331 $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str);
brian97807ccbe52012-12-11 20:24:12 +0200332 $str = preg_replace_callback('/<\w+.*/si', array($this, '_decode_entity'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600333
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200334 // Remove Invisible Characters Again!
Greg Aker757dda62010-04-14 19:06:19 -0500335 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Jonese701d762010-03-02 18:17:01 -0600337 /*
338 * Convert all tabs to spaces
339 *
340 * This prevents strings like this: ja vascript
341 * NOTE: we deal with spaces between characters later.
David Behler07b53422011-08-15 00:25:06 +0200342 * NOTE: preg_replace was found to be amazingly slow here on
Pascal Krietec9c045a2011-04-05 14:50:41 -0400343 * large blocks of data, so we use str_replace.
Derek Jonese701d762010-03-02 18:17:01 -0600344 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200345 $str = str_replace("\t", ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200346
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200347 // Capture converted string for later comparison
Derek Jonese701d762010-03-02 18:17:01 -0600348 $converted_string = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200349
Pascal Krietec9c045a2011-04-05 14:50:41 -0400350 // Remove Strings that are never allowed
351 $str = $this->_do_never_allowed($str);
Derek Jonese701d762010-03-02 18:17:01 -0600352
353 /*
354 * Makes PHP tags safe
355 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400356 * Note: XML tags are inadvertently replaced too:
Derek Jonese701d762010-03-02 18:17:01 -0600357 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400358 * <?xml
Derek Jonese701d762010-03-02 18:17:01 -0600359 *
360 * But it doesn't seem to pose a problem.
Derek Jonese701d762010-03-02 18:17:01 -0600361 */
362 if ($is_image === TRUE)
363 {
David Behler07b53422011-08-15 00:25:06 +0200364 // Images have a tendency to have the PHP short opening and
365 // closing tags every so often so we skip those and only
Pascal Krietec9c045a2011-04-05 14:50:41 -0400366 // do the long opening tags.
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200367 $str = preg_replace('/<\?(php)/i', '&lt;?\\1', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600368 }
369 else
370 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200371 $str = str_replace(array('<?', '?'.'>'), array('&lt;?', '?&gt;'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Jonese701d762010-03-02 18:17:01 -0600374 /*
375 * Compact any exploded words
376 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500377 * This corrects words like: j a v a s c r i p t
Derek Jonese701d762010-03-02 18:17:01 -0600378 * These words are compacted back to their correct state.
Derek Jonese701d762010-03-02 18:17:01 -0600379 */
Pascal Krietec9c045a2011-04-05 14:50:41 -0400380 $words = array(
Wes Bakerd3481352012-05-07 16:49:33 -0400381 'javascript', 'expression', 'vbscript', 'script', 'base64',
Timothy Warren40403d22012-04-19 16:38:50 -0400382 'applet', 'alert', 'document', 'write', 'cookie', 'window'
383 );
David Behler07b53422011-08-15 00:25:06 +0200384
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 {
vlakoff06127562013-03-30 00:06:39 +0100491 $this->_xss_hash = md5(uniqid(mt_rand()));
Derek Jonese701d762010-03-02 18:17:01 -0600492 }
493
Pascal Krietec9c045a2011-04-05 14:50:41 -0400494 return $this->_xss_hash;
Derek Jonese701d762010-03-02 18:17:01 -0600495 }
496
497 // --------------------------------------------------------------------
498
499 /**
Derek Jonesa0911472010-03-30 10:33:09 -0500500 * HTML Entities Decode
501 *
Andrey Andreev64354102012-10-28 14:16:02 +0200502 * A replacement for html_entity_decode()
Derek Jonesa0911472010-03-30 10:33:09 -0500503 *
Pascal Krietec38e3b62011-11-14 13:55:00 -0500504 * The reason we are not using html_entity_decode() by itself is because
505 * while it is not technically correct to leave out the semicolon
506 * at the end of an entity most browsers will still interpret the entity
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200507 * correctly. html_entity_decode() does not convert entities without
Pascal Krietec38e3b62011-11-14 13:55:00 -0500508 * semicolons, so we are left with our own little solution here. Bummer.
Derek Jonesa0911472010-03-30 10:33:09 -0500509 *
Andrey Andreev64354102012-10-28 14:16:02 +0200510 * @link http://php.net/html-entity-decode
511 *
512 * @param string $str Input
513 * @param string $charset Character set
Derek Jonesa0911472010-03-30 10:33:09 -0500514 * @return string
515 */
freewil8cc0cfe2011-08-27 21:53:00 -0400516 public function entity_decode($str, $charset = NULL)
Derek Jonesa0911472010-03-30 10:33:09 -0500517 {
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300518 if (strpos($str, '&') === FALSE)
freewil5c9b0d12011-08-28 12:15:23 -0400519 {
520 return $str;
521 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300522
freewil5c9b0d12011-08-28 12:15:23 -0400523 if (empty($charset))
524 {
525 $charset = config_item('charset');
526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
brian978638a9d22012-12-18 13:25:54 +0200528 do
529 {
530 $matches = $matches1 = 0;
brian97807ccbe52012-12-11 20:24:12 +0200531
brian978638a9d22012-12-18 13:25:54 +0200532 $str = html_entity_decode($str, ENT_COMPAT, $charset);
533 $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str, -1, $matches);
534 $str = preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str, -1, $matches1);
535 }
Andrey Andreev72ed4c32012-12-19 17:07:54 +0200536 while ($matches OR $matches1);
brian978f50fc732012-12-08 23:22:26 +0200537
brian978638a9d22012-12-18 13:25:54 +0200538 return $str;
Derek Jonesa0911472010-03-30 10:33:09 -0500539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Jonesa0911472010-03-30 10:33:09 -0500541 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Jonesa0911472010-03-30 10:33:09 -0500543 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200544 * Sanitize Filename
Derek Jonese701d762010-03-02 18:17:01 -0600545 *
Andrey Andreev64354102012-10-28 14:16:02 +0200546 * @param string $str Input file name
547 * @param bool $relative_path Whether to preserve paths
Derek Jonese701d762010-03-02 18:17:01 -0600548 * @return string
549 */
Hunter Wu8df33522013-08-03 22:36:05 +0800550 public function sanitize_filename($str, $relative_path = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600551 {
Hunter Wu8df33522013-08-03 22:36:05 +0800552 $bad = array(
553 '../', '<!--', '-->', '<', '>',
554 "'", '"', '&', '$', '#',
555 '{', '}', '[', ']', '=',
556 ';', '?', '%20', '%22',
557 '%3c', // <
558 '%253c', // <
559 '%3e', // >
560 '%0e', // >
561 '%28', // (
562 '%29', // )
563 '%2528', // (
564 '%26', // &
565 '%24', // $
566 '%3f', // ?
567 '%3b', // ;
568 '%3d' // =
569 );
David Behler07b53422011-08-15 00:25:06 +0200570
Derek Jones2ef37592010-10-06 17:51:59 -0500571 if ( ! $relative_path)
572 {
573 $bad[] = './';
574 $bad[] = '/';
575 }
Derek Jonese701d762010-03-02 18:17:01 -0600576
Pascal Krietec9c045a2011-04-05 14:50:41 -0400577 $str = remove_invisible_characters($str, FALSE);
Andrey Andreev7e559772013-01-29 15:38:33 +0200578
579 do
580 {
581 $old = $str;
582 $str = str_replace($bad, '', $str);
583 }
584 while ($old !== $str);
585
586 return stripslashes($str);
Derek Jonese701d762010-03-02 18:17:01 -0600587 }
588
Pascal Krietec9c045a2011-04-05 14:50:41 -0400589 // ----------------------------------------------------------------
590
591 /**
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300592 * Strip Image Tags
593 *
Andrey Andreev64354102012-10-28 14:16:02 +0200594 * @param string $str
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300595 * @return string
596 */
597 public function strip_image_tags($str)
598 {
599 return preg_replace(array('#<img\s+.*?src\s*=\s*["\'](.+?)["\'].*?\>#', '#<img\s+.*?src\s*=\s*(.+?).*?\>#'), '\\1', $str);
600 }
601
602 // ----------------------------------------------------------------
603
604 /**
Pascal Krietec9c045a2011-04-05 14:50:41 -0400605 * Compact Exploded Words
606 *
Andrey Andreev64354102012-10-28 14:16:02 +0200607 * Callback method for xss_clean() to remove whitespace from
608 * things like 'j a v a s c r i p t'.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400609 *
Andrey Andreev64354102012-10-28 14:16:02 +0200610 * @used-by CI_Security::xss_clean()
611 * @param array $matches
Timothy Warrenad475052012-04-19 13:21:06 -0400612 * @return string
Pascal Krietec9c045a2011-04-05 14:50:41 -0400613 */
614 protected function _compact_exploded_words($matches)
615 {
616 return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
617 }
618
619 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200620
Timothy Warrenad475052012-04-19 13:21:06 -0400621 /**
622 * Remove Evil HTML Attributes (like event handlers and style)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400623 *
624 * It removes the evil attribute and either:
Pascal Krietec9c045a2011-04-05 14:50:41 -0400625 *
Andrey Andreev64354102012-10-28 14:16:02 +0200626 * - Everything up until a space. For example, everything between the pipes:
627 *
628 * <code>
629 * <a |style=document.write('hello');alert('world');| class=link>
630 * </code>
631 *
632 * - Everything inside the quotes. For example, everything between the pipes:
633 *
634 * <code>
635 * <a |style="document.write('hello'); alert('world');"| class="link">
636 * </code>
637 *
638 * @param string $str The string to check
639 * @param bool $is_image Whether the input is an image
640 * @return string The string with the evil attributes removed
Pascal Krietec9c045a2011-04-05 14:50:41 -0400641 */
642 protected function _remove_evil_attributes($str, $is_image)
643 {
644 // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns
Pascal Krietec38e3b62011-11-14 13:55:00 -0500645 $evil_attributes = array('on\w*', 'style', 'xmlns', 'formaction');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400646
647 if ($is_image === TRUE)
648 {
649 /*
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200650 * Adobe Photoshop puts XML metadata into JFIF images,
Pascal Krietec9c045a2011-04-05 14:50:41 -0400651 * including namespacing, so we have to allow this for images.
652 */
653 unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
654 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200655
Pascal Krietec9c045a2011-04-05 14:50:41 -0400656 do {
Pascal Krietec38e3b62011-11-14 13:55:00 -0500657 $count = 0;
658 $attribs = array();
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200659
brian978160c7d12012-12-03 21:18:20 +0200660 // find occurrences of illegal attribute strings with quotes (042 and 047 are octal quotes)
661 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 +0200662
Pascal Krietec38e3b62011-11-14 13:55:00 -0500663 foreach ($matches as $attr)
664 {
665 $attribs[] = preg_quote($attr[0], '/');
666 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200667
brian978160c7d12012-12-03 21:18:20 +0200668 // find occurrences of illegal attribute strings without quotes
669 preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*([^\s>]*)/is', $str, $matches, PREG_SET_ORDER);
David Behler07b53422011-08-15 00:25:06 +0200670
Pascal Krietec38e3b62011-11-14 13:55:00 -0500671 foreach ($matches as $attr)
672 {
673 $attribs[] = preg_quote($attr[0], '/');
674 }
675
676 // replace illegal attribute strings that are inside an html tag
677 if (count($attribs) > 0)
678 {
brian978160c7d12012-12-03 21:18:20 +0200679 $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 -0500680 }
Andrey Andreev72ed4c32012-12-19 17:07:54 +0200681 }
682 while ($count);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200683
Pascal Krietec9c045a2011-04-05 14:50:41 -0400684 return $str;
685 }
David Behler07b53422011-08-15 00:25:06 +0200686
Pascal Krietec9c045a2011-04-05 14:50:41 -0400687 // --------------------------------------------------------------------
688
689 /**
690 * Sanitize Naughty HTML
691 *
Andrey Andreev64354102012-10-28 14:16:02 +0200692 * Callback method for xss_clean() to remove naughty HTML elements.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400693 *
Andrey Andreev64354102012-10-28 14:16:02 +0200694 * @used-by CI_Security::xss_clean()
695 * @param array $matches
Pascal Krietec9c045a2011-04-05 14:50:41 -0400696 * @return string
697 */
698 protected function _sanitize_naughty_html($matches)
699 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200700 return '&lt;'.$matches[1].$matches[2].$matches[3] // encode opening brace
701 // encode captured opening or closing brace to prevent recursive vectors:
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200702 .str_replace(array('>', '<'), array('&gt;', '&lt;'), $matches[4]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400703 }
704
705 // --------------------------------------------------------------------
706
707 /**
708 * JS Link Removal
709 *
Andrey Andreev64354102012-10-28 14:16:02 +0200710 * Callback method for xss_clean() to sanitize links.
711 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400712 * This limits the PCRE backtracks, making it more performance friendly
713 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200714 * PHP 5.2+ on link-heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400715 *
Andrey Andreev64354102012-10-28 14:16:02 +0200716 * @used-by CI_Security::xss_clean()
717 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400718 * @return string
719 */
720 protected function _js_link_removal($match)
721 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200722 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200723 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 +0200724 '',
725 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
726 ),
727 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400728 }
729
730 // --------------------------------------------------------------------
731
732 /**
733 * JS Image Removal
734 *
Andrey Andreev64354102012-10-28 14:16:02 +0200735 * Callback method for xss_clean() to sanitize image tags.
736 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400737 * This limits the PCRE backtracks, making it more performance friendly
738 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200739 * PHP 5.2+ on image tag heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400740 *
Andrey Andreev64354102012-10-28 14:16:02 +0200741 * @used-by CI_Security::xss_clean()
742 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400743 * @return string
744 */
745 protected function _js_img_removal($match)
746 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200747 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200748 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 +0200749 '',
750 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
751 ),
752 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400753 }
754
755 // --------------------------------------------------------------------
756
757 /**
758 * Attribute Conversion
759 *
Andrey Andreev64354102012-10-28 14:16:02 +0200760 * @used-by CI_Security::xss_clean()
761 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400762 * @return string
763 */
764 protected function _convert_attribute($match)
765 {
766 return str_replace(array('>', '<', '\\'), array('&gt;', '&lt;', '\\\\'), $match[0]);
767 }
768
769 // --------------------------------------------------------------------
770
771 /**
772 * Filter Attributes
773 *
Andrey Andreev64354102012-10-28 14:16:02 +0200774 * Filters tag attributes for consistency and safety.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400775 *
Andrey Andreev64354102012-10-28 14:16:02 +0200776 * @used-by CI_Security::_js_img_removal()
777 * @used-by CI_Security::_js_link_removal()
778 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400779 * @return string
780 */
781 protected function _filter_attributes($str)
782 {
783 $out = '';
Pascal Krietec9c045a2011-04-05 14:50:41 -0400784 if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches))
785 {
786 foreach ($matches[0] as $match)
787 {
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200788 $out .= preg_replace('#/\*.*?\*/#s', '', $match);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400789 }
790 }
791
792 return $out;
793 }
794
795 // --------------------------------------------------------------------
796
797 /**
798 * HTML Entity Decode Callback
799 *
Andrey Andreev64354102012-10-28 14:16:02 +0200800 * @used-by CI_Security::xss_clean()
801 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400802 * @return string
803 */
804 protected function _decode_entity($match)
805 {
806 return $this->entity_decode($match[0], strtoupper(config_item('charset')));
807 }
808
809 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200810
Pascal Krietec9c045a2011-04-05 14:50:41 -0400811 /**
812 * Validate URL entities
813 *
Andrey Andreev64354102012-10-28 14:16:02 +0200814 * @used-by CI_Security::xss_clean()
815 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400816 * @return string
817 */
818 protected function _validate_entities($str)
819 {
820 /*
821 * Protect GET variables in URLs
822 */
David Behler07b53422011-08-15 00:25:06 +0200823
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200824 // 901119URL5918AMP18930PROTECT8198
825 $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 -0400826
827 /*
828 * Validate standard character entities
829 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500830 * Add a semicolon if missing. We do this to enable
Pascal Krietec9c045a2011-04-05 14:50:41 -0400831 * the conversion of entities to ASCII later.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400832 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200833 $str = preg_replace('#(&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', '\\1;\\2', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400834
835 /*
836 * Validate UTF16 two byte encoding (x00)
837 *
838 * Just as above, adds a semicolon if missing.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400839 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200840 $str = preg_replace('#(&\#x?)([0-9A-F]+);?#i', '\\1\\2;', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400841
842 /*
843 * Un-Protect GET variables in URLs
844 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200845 return str_replace($this->xss_hash(), '&', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400846 }
847
848 // ----------------------------------------------------------------------
849
850 /**
851 * Do Never Allowed
852 *
Andrey Andreev64354102012-10-28 14:16:02 +0200853 * @used-by CI_Security::xss_clean()
Pascal Krietec9c045a2011-04-05 14:50:41 -0400854 * @param string
855 * @return string
856 */
857 protected function _do_never_allowed($str)
858 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200859 $str = str_replace(array_keys($this->_never_allowed_str), $this->_never_allowed_str, $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400860
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200861 foreach ($this->_never_allowed_regex as $regex)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400862 {
Wes Baker5335bc32012-04-24 15:17:14 -0400863 $str = preg_replace('#'.$regex.'#is', '[removed]', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400864 }
David Behler07b53422011-08-15 00:25:06 +0200865
Pascal Krietec9c045a2011-04-05 14:50:41 -0400866 return $str;
867 }
868
869 // --------------------------------------------------------------------
870
871 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200872 * Set CSRF Hash and Cookie
Pascal Krietec9c045a2011-04-05 14:50:41 -0400873 *
874 * @return string
875 */
876 protected function _csrf_set_hash()
877 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100878 if ($this->_csrf_hash === '')
Pascal Krietec9c045a2011-04-05 14:50:41 -0400879 {
David Behler07b53422011-08-15 00:25:06 +0200880 // If the cookie exists we will use it's value.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400881 // We don't necessarily want to regenerate it with
David Behler07b53422011-08-15 00:25:06 +0200882 // each page load since a page could contain embedded
Pascal Krietec9c045a2011-04-05 14:50:41 -0400883 // sub-pages causing this feature to fail
David Behler07b53422011-08-15 00:25:06 +0200884 if (isset($_COOKIE[$this->_csrf_cookie_name]) &&
Alexander Hofstedee2c374f2012-05-17 00:28:08 +0200885 preg_match('#^[0-9a-f]{32}$#iS', $_COOKIE[$this->_csrf_cookie_name]) === 1)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400886 {
887 return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name];
888 }
David Behler07b53422011-08-15 00:25:06 +0200889
Chris Berthed93e6f32011-09-25 10:33:25 -0400890 $this->_csrf_hash = md5(uniqid(rand(), TRUE));
891 $this->csrf_set_cookie();
Pascal Krietec9c045a2011-04-05 14:50:41 -0400892 }
893
894 return $this->_csrf_hash;
895 }
896
Derek Jonese701d762010-03-02 18:17:01 -0600897}
Derek Jonese701d762010-03-02 18:17:01 -0600898
899/* End of file Security.php */
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300900/* Location: ./system/core/Security.php */