blob: b8e66c08718f9ce2ea6d29446bb4674d6e23b17e [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Jonese701d762010-03-02 18:17:01 -060023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
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 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400154 * @return object
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
Andrey Andreev64354102012-10-28 14:16:02 +0200205 * @return object
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);
Andrey Andreev4562f2c2012-01-09 23:39:50 +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 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500371 $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 Jones37f4b9c2011-07-01 17:56:50 -0500385
Derek Jonese701d762010-03-02 18:17:01 -0600386 foreach ($words as $word)
387 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200388 $word = implode('\s*', str_split($word)).'\s*';
Derek Jonese701d762010-03-02 18:17:01 -0600389
390 // We only want to do this when it is followed by a non-word character
391 // That way valid stuff like "dealer to" does not become "dealerto"
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300392 $str = preg_replace_callback('#('.substr($word, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Jonese701d762010-03-02 18:17:01 -0600395 /*
396 * Remove disallowed Javascript in links or img tags
David Behler07b53422011-08-15 00:25:06 +0200397 * We used to do some version comparisons and use of stripos for PHP5,
398 * but it is dog slow compared to these simplified non-capturing
Pascal Krietec9c045a2011-04-05 14:50:41 -0400399 * preg_match(), especially if the pattern exists in the string
Derek Jonese701d762010-03-02 18:17:01 -0600400 */
401 do
402 {
403 $original = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200404
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200405 if (preg_match('/<a/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600406 {
vlakoffa81f60c2012-07-02 15:20:11 +0200407 $str = preg_replace_callback('#<a\s+([^>]*?)(?:>|$)#si', array($this, '_js_link_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200410 if (preg_match('/<img/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600411 {
vlakoffa81f60c2012-07-02 15:20:11 +0200412 $str = preg_replace_callback('#<img\s+([^>]*?)(?:\s?/?>|$)#si', array($this, '_js_img_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600413 }
Barry Mienydd671972010-10-04 16:33:58 +0200414
vlakoffa81f60c2012-07-02 15:20:11 +0200415 if (preg_match('/script|xss/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600416 {
vlakoffa81f60c2012-07-02 15:20:11 +0200417 $str = preg_replace('#</*(?:script|xss).*?>#si', '[removed]', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600418 }
419 }
vlakoffa81f60c2012-07-02 15:20:11 +0200420 while ($original !== $str);
Derek Jonese701d762010-03-02 18:17:01 -0600421
422 unset($original);
423
Pascal Krietec9c045a2011-04-05 14:50:41 -0400424 // Remove evil attributes such as style, onclick and xmlns
425 $str = $this->_remove_evil_attributes($str, $is_image);
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Jonese701d762010-03-02 18:17:01 -0600427 /*
428 * Sanitize naughty HTML elements
429 *
430 * If a tag containing any of the words in the list
431 * below is found, the tag gets converted to entities.
432 *
433 * So this: <blink>
434 * Becomes: &lt;blink&gt;
Derek Jonese701d762010-03-02 18:17:01 -0600435 */
436 $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';
437 $str = preg_replace_callback('#<(/*\s*)('.$naughty.')([^><]*)([><]*)#is', array($this, '_sanitize_naughty_html'), $str);
438
439 /*
440 * Sanitize naughty scripting elements
441 *
442 * Similar to above, only instead of looking for
443 * tags it looks for PHP and JavaScript commands
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200444 * that are disallowed. Rather than removing the
Derek Jonese701d762010-03-02 18:17:01 -0600445 * code, it simply converts the parenthesis to entities
446 * rendering the code un-executable.
447 *
448 * For example: eval('some code')
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200449 * Becomes: eval&#40;'some code'&#41;
Derek Jonese701d762010-03-02 18:17:01 -0600450 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200451 $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si',
452 '\\1\\2&#40;\\3&#41;',
453 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200454
Pascal Krietec9c045a2011-04-05 14:50:41 -0400455 // Final clean up
456 // This adds a bit of extra precaution in case
457 // something got through the above filters
458 $str = $this->_do_never_allowed($str);
Derek Jonese701d762010-03-02 18:17:01 -0600459
460 /*
Pascal Krietec9c045a2011-04-05 14:50:41 -0400461 * Images are Handled in a Special Way
David Behler07b53422011-08-15 00:25:06 +0200462 * - Essentially, we want to know that after all of the character
463 * conversion is done whether any unwanted, likely XSS, code was found.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400464 * If not, we return TRUE, as the image is clean.
David Behler07b53422011-08-15 00:25:06 +0200465 * However, if the string post-conversion does not matched the
466 * string post-removal of XSS, then it fails, as there was unwanted XSS
Pascal Krietec9c045a2011-04-05 14:50:41 -0400467 * code found and removed/changed during processing.
Derek Jonese701d762010-03-02 18:17:01 -0600468 */
Derek Jonese701d762010-03-02 18:17:01 -0600469 if ($is_image === TRUE)
470 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200471 return ($str === $converted_string);
Derek Jonese701d762010-03-02 18:17:01 -0600472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200474 log_message('debug', 'XSS Filtering completed');
Derek Jonese701d762010-03-02 18:17:01 -0600475 return $str;
476 }
477
478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Jonese701d762010-03-02 18:17:01 -0600480 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200481 * XSS Hash
Derek Jonese701d762010-03-02 18:17:01 -0600482 *
Andrey Andreev64354102012-10-28 14:16:02 +0200483 * Generates the XSS hash if needed and returns it.
484 *
485 * @see CI_Security::$_xss_hash
486 * @return string XSS hash
Derek Jonese701d762010-03-02 18:17:01 -0600487 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500488 public function xss_hash()
Barry Mienydd671972010-10-04 16:33:58 +0200489 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100490 if ($this->_xss_hash === '')
Derek Jonese701d762010-03-02 18:17:01 -0600491 {
Pascal Krietec38e3b62011-11-14 13:55:00 -0500492 mt_srand();
Pascal Krietec9c045a2011-04-05 14:50:41 -0400493 $this->_xss_hash = md5(time() + mt_rand(0, 1999999999));
Derek Jonese701d762010-03-02 18:17:01 -0600494 }
495
Pascal Krietec9c045a2011-04-05 14:50:41 -0400496 return $this->_xss_hash;
Derek Jonese701d762010-03-02 18:17:01 -0600497 }
498
499 // --------------------------------------------------------------------
500
501 /**
Derek Jonesa0911472010-03-30 10:33:09 -0500502 * HTML Entities Decode
503 *
Andrey Andreev64354102012-10-28 14:16:02 +0200504 * A replacement for html_entity_decode()
Derek Jonesa0911472010-03-30 10:33:09 -0500505 *
Pascal Krietec38e3b62011-11-14 13:55:00 -0500506 * The reason we are not using html_entity_decode() by itself is because
507 * while it is not technically correct to leave out the semicolon
508 * at the end of an entity most browsers will still interpret the entity
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200509 * correctly. html_entity_decode() does not convert entities without
Pascal Krietec38e3b62011-11-14 13:55:00 -0500510 * semicolons, so we are left with our own little solution here. Bummer.
Derek Jonesa0911472010-03-30 10:33:09 -0500511 *
Andrey Andreev64354102012-10-28 14:16:02 +0200512 * @link http://php.net/html-entity-decode
513 *
514 * @param string $str Input
515 * @param string $charset Character set
Derek Jonesa0911472010-03-30 10:33:09 -0500516 * @return string
517 */
freewil8cc0cfe2011-08-27 21:53:00 -0400518 public function entity_decode($str, $charset = NULL)
Derek Jonesa0911472010-03-30 10:33:09 -0500519 {
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300520 if (strpos($str, '&') === FALSE)
freewil5c9b0d12011-08-28 12:15:23 -0400521 {
522 return $str;
523 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300524
freewil5c9b0d12011-08-28 12:15:23 -0400525 if (empty($charset))
526 {
527 $charset = config_item('charset');
528 }
Barry Mienydd671972010-10-04 16:33:58 +0200529
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300530 $str = html_entity_decode($str, ENT_COMPAT, $charset);
531 $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str);
532 return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str);
Derek Jonesa0911472010-03-30 10:33:09 -0500533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Jonesa0911472010-03-30 10:33:09 -0500535 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Jonesa0911472010-03-30 10:33:09 -0500537 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200538 * Sanitize Filename
Derek Jonese701d762010-03-02 18:17:01 -0600539 *
Andrey Andreev64354102012-10-28 14:16:02 +0200540 * @param string $str Input file name
541 * @param bool $relative_path Whether to preserve paths
Derek Jonese701d762010-03-02 18:17:01 -0600542 * @return string
543 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500544 public function sanitize_filename($str, $relative_path = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600545 {
546 $bad = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400547 '../', '<!--', '-->', '<', '>',
548 "'", '"', '&', '$', '#',
549 '{', '}', '[', ']', '=',
550 ';', '?', '%20', '%22',
551 '%3c', // <
552 '%253c', // <
553 '%3e', // >
554 '%0e', // >
555 '%28', // (
556 '%29', // )
557 '%2528', // (
558 '%26', // &
559 '%24', // $
560 '%3f', // ?
561 '%3b', // ;
562 '%3d' // =
563 );
David Behler07b53422011-08-15 00:25:06 +0200564
Derek Jones2ef37592010-10-06 17:51:59 -0500565 if ( ! $relative_path)
566 {
567 $bad[] = './';
568 $bad[] = '/';
569 }
Derek Jonese701d762010-03-02 18:17:01 -0600570
Pascal Krietec9c045a2011-04-05 14:50:41 -0400571 $str = remove_invisible_characters($str, FALSE);
Derek Jonese701d762010-03-02 18:17:01 -0600572 return stripslashes(str_replace($bad, '', $str));
573 }
574
Pascal Krietec9c045a2011-04-05 14:50:41 -0400575 // ----------------------------------------------------------------
576
577 /**
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300578 * Strip Image Tags
579 *
Andrey Andreev64354102012-10-28 14:16:02 +0200580 * @param string $str
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300581 * @return string
582 */
583 public function strip_image_tags($str)
584 {
585 return preg_replace(array('#<img\s+.*?src\s*=\s*["\'](.+?)["\'].*?\>#', '#<img\s+.*?src\s*=\s*(.+?).*?\>#'), '\\1', $str);
586 }
587
588 // ----------------------------------------------------------------
589
590 /**
Pascal Krietec9c045a2011-04-05 14:50:41 -0400591 * Compact Exploded Words
592 *
Andrey Andreev64354102012-10-28 14:16:02 +0200593 * Callback method for xss_clean() to remove whitespace from
594 * things like 'j a v a s c r i p t'.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400595 *
Andrey Andreev64354102012-10-28 14:16:02 +0200596 * @used-by CI_Security::xss_clean()
597 * @param array $matches
Timothy Warrenad475052012-04-19 13:21:06 -0400598 * @return string
Pascal Krietec9c045a2011-04-05 14:50:41 -0400599 */
600 protected function _compact_exploded_words($matches)
601 {
602 return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
603 }
604
605 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200606
Timothy Warrenad475052012-04-19 13:21:06 -0400607 /**
608 * Remove Evil HTML Attributes (like event handlers and style)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400609 *
610 * It removes the evil attribute and either:
Pascal Krietec9c045a2011-04-05 14:50:41 -0400611 *
Andrey Andreev64354102012-10-28 14:16:02 +0200612 * - Everything up until a space. For example, everything between the pipes:
613 *
614 * <code>
615 * <a |style=document.write('hello');alert('world');| class=link>
616 * </code>
617 *
618 * - Everything inside the quotes. For example, everything between the pipes:
619 *
620 * <code>
621 * <a |style="document.write('hello'); alert('world');"| class="link">
622 * </code>
623 *
624 * @param string $str The string to check
625 * @param bool $is_image Whether the input is an image
626 * @return string The string with the evil attributes removed
Pascal Krietec9c045a2011-04-05 14:50:41 -0400627 */
628 protected function _remove_evil_attributes($str, $is_image)
629 {
630 // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns
Pascal Krietec38e3b62011-11-14 13:55:00 -0500631 $evil_attributes = array('on\w*', 'style', 'xmlns', 'formaction');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400632
633 if ($is_image === TRUE)
634 {
635 /*
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200636 * Adobe Photoshop puts XML metadata into JFIF images,
Pascal Krietec9c045a2011-04-05 14:50:41 -0400637 * including namespacing, so we have to allow this for images.
638 */
639 unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
640 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200641
Pascal Krietec9c045a2011-04-05 14:50:41 -0400642 do {
Pascal Krietec38e3b62011-11-14 13:55:00 -0500643 $count = 0;
644 $attribs = array();
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200645
Pascal Krietec38e3b62011-11-14 13:55:00 -0500646 // find occurrences of illegal attribute strings without quotes
Wes Baker5335bc32012-04-24 15:17:14 -0400647 preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*([^\s>]*)/is', $str, $matches, PREG_SET_ORDER);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200648
Pascal Krietec38e3b62011-11-14 13:55:00 -0500649 foreach ($matches as $attr)
650 {
Wes Baker5335bc32012-04-24 15:17:14 -0400651
Pascal Krietec38e3b62011-11-14 13:55:00 -0500652 $attribs[] = preg_quote($attr[0], '/');
653 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200654
Pascal Krietec38e3b62011-11-14 13:55:00 -0500655 // find occurrences of illegal attribute strings with quotes (042 and 047 are octal quotes)
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200656 preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*(\042|\047)([^\\2]*?)(\\2)/is', $str, $matches, PREG_SET_ORDER);
David Behler07b53422011-08-15 00:25:06 +0200657
Pascal Krietec38e3b62011-11-14 13:55:00 -0500658 foreach ($matches as $attr)
659 {
660 $attribs[] = preg_quote($attr[0], '/');
661 }
662
663 // replace illegal attribute strings that are inside an html tag
664 if (count($attribs) > 0)
665 {
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300666 $str = preg_replace('/<(\/?[^><]+?)([^A-Za-z<>\-])(.*?)('.implode('|', $attribs).')(.*?)([\s><])([><]*)/i', '<$1 $3$5$6$7', $str, -1, $count);
Pascal Krietec38e3b62011-11-14 13:55:00 -0500667 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200668
Pascal Krietec38e3b62011-11-14 13:55:00 -0500669 } while ($count);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200670
Pascal Krietec9c045a2011-04-05 14:50:41 -0400671 return $str;
672 }
David Behler07b53422011-08-15 00:25:06 +0200673
Pascal Krietec9c045a2011-04-05 14:50:41 -0400674 // --------------------------------------------------------------------
675
676 /**
677 * Sanitize Naughty HTML
678 *
Andrey Andreev64354102012-10-28 14:16:02 +0200679 * Callback method for xss_clean() to remove naughty HTML elements.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400680 *
Andrey Andreev64354102012-10-28 14:16:02 +0200681 * @used-by CI_Security::xss_clean()
682 * @param array $matches
Pascal Krietec9c045a2011-04-05 14:50:41 -0400683 * @return string
684 */
685 protected function _sanitize_naughty_html($matches)
686 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200687 return '&lt;'.$matches[1].$matches[2].$matches[3] // encode opening brace
688 // encode captured opening or closing brace to prevent recursive vectors:
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200689 .str_replace(array('>', '<'), array('&gt;', '&lt;'), $matches[4]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400690 }
691
692 // --------------------------------------------------------------------
693
694 /**
695 * JS Link Removal
696 *
Andrey Andreev64354102012-10-28 14:16:02 +0200697 * Callback method for xss_clean() to sanitize links.
698 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400699 * This limits the PCRE backtracks, making it more performance friendly
700 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200701 * PHP 5.2+ on link-heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400702 *
Andrey Andreev64354102012-10-28 14:16:02 +0200703 * @used-by CI_Security::xss_clean()
704 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400705 * @return string
706 */
707 protected function _js_link_removal($match)
708 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200709 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200710 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 +0200711 '',
712 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
713 ),
714 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400715 }
716
717 // --------------------------------------------------------------------
718
719 /**
720 * JS Image Removal
721 *
Andrey Andreev64354102012-10-28 14:16:02 +0200722 * Callback method for xss_clean() to sanitize image tags.
723 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400724 * This limits the PCRE backtracks, making it more performance friendly
725 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200726 * PHP 5.2+ on image tag heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400727 *
Andrey Andreev64354102012-10-28 14:16:02 +0200728 * @used-by CI_Security::xss_clean()
729 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400730 * @return string
731 */
732 protected function _js_img_removal($match)
733 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200734 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200735 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 +0200736 '',
737 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
738 ),
739 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400740 }
741
742 // --------------------------------------------------------------------
743
744 /**
745 * Attribute Conversion
746 *
Andrey Andreev64354102012-10-28 14:16:02 +0200747 * @used-by CI_Security::xss_clean()
748 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400749 * @return string
750 */
751 protected function _convert_attribute($match)
752 {
753 return str_replace(array('>', '<', '\\'), array('&gt;', '&lt;', '\\\\'), $match[0]);
754 }
755
756 // --------------------------------------------------------------------
757
758 /**
759 * Filter Attributes
760 *
Andrey Andreev64354102012-10-28 14:16:02 +0200761 * Filters tag attributes for consistency and safety.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400762 *
Andrey Andreev64354102012-10-28 14:16:02 +0200763 * @used-by CI_Security::_js_img_removal()
764 * @used-by CI_Security::_js_link_removal()
765 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400766 * @return string
767 */
768 protected function _filter_attributes($str)
769 {
770 $out = '';
Pascal Krietec9c045a2011-04-05 14:50:41 -0400771 if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches))
772 {
773 foreach ($matches[0] as $match)
774 {
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200775 $out .= preg_replace('#/\*.*?\*/#s', '', $match);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400776 }
777 }
778
779 return $out;
780 }
781
782 // --------------------------------------------------------------------
783
784 /**
785 * HTML Entity Decode Callback
786 *
Andrey Andreev64354102012-10-28 14:16:02 +0200787 * @used-by CI_Security::xss_clean()
788 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400789 * @return string
790 */
791 protected function _decode_entity($match)
792 {
793 return $this->entity_decode($match[0], strtoupper(config_item('charset')));
794 }
795
796 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200797
Pascal Krietec9c045a2011-04-05 14:50:41 -0400798 /**
799 * Validate URL entities
800 *
Andrey Andreev64354102012-10-28 14:16:02 +0200801 * @used-by CI_Security::xss_clean()
802 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400803 * @return string
804 */
805 protected function _validate_entities($str)
806 {
807 /*
808 * Protect GET variables in URLs
809 */
David Behler07b53422011-08-15 00:25:06 +0200810
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200811 // 901119URL5918AMP18930PROTECT8198
812 $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 -0400813
814 /*
815 * Validate standard character entities
816 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500817 * Add a semicolon if missing. We do this to enable
Pascal Krietec9c045a2011-04-05 14:50:41 -0400818 * the conversion of entities to ASCII later.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400819 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200820 $str = preg_replace('#(&\#?[0-9a-z]{2,})([\x00-\x20])*;?#i', '\\1;\\2', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400821
822 /*
823 * Validate UTF16 two byte encoding (x00)
824 *
825 * Just as above, adds a semicolon if missing.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400826 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200827 $str = preg_replace('#(&\#x?)([0-9A-F]+);?#i', '\\1\\2;', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400828
829 /*
830 * Un-Protect GET variables in URLs
831 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200832 return str_replace($this->xss_hash(), '&', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400833 }
834
835 // ----------------------------------------------------------------------
836
837 /**
838 * Do Never Allowed
839 *
Andrey Andreev64354102012-10-28 14:16:02 +0200840 * @used-by CI_Security::xss_clean()
Pascal Krietec9c045a2011-04-05 14:50:41 -0400841 * @param string
842 * @return string
843 */
844 protected function _do_never_allowed($str)
845 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200846 $str = str_replace(array_keys($this->_never_allowed_str), $this->_never_allowed_str, $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400847
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200848 foreach ($this->_never_allowed_regex as $regex)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400849 {
Wes Baker5335bc32012-04-24 15:17:14 -0400850 $str = preg_replace('#'.$regex.'#is', '[removed]', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400851 }
David Behler07b53422011-08-15 00:25:06 +0200852
Pascal Krietec9c045a2011-04-05 14:50:41 -0400853 return $str;
854 }
855
856 // --------------------------------------------------------------------
857
858 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200859 * Set CSRF Hash and Cookie
Pascal Krietec9c045a2011-04-05 14:50:41 -0400860 *
861 * @return string
862 */
863 protected function _csrf_set_hash()
864 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100865 if ($this->_csrf_hash === '')
Pascal Krietec9c045a2011-04-05 14:50:41 -0400866 {
David Behler07b53422011-08-15 00:25:06 +0200867 // If the cookie exists we will use it's value.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400868 // We don't necessarily want to regenerate it with
David Behler07b53422011-08-15 00:25:06 +0200869 // each page load since a page could contain embedded
Pascal Krietec9c045a2011-04-05 14:50:41 -0400870 // sub-pages causing this feature to fail
David Behler07b53422011-08-15 00:25:06 +0200871 if (isset($_COOKIE[$this->_csrf_cookie_name]) &&
Alexander Hofstedee2c374f2012-05-17 00:28:08 +0200872 preg_match('#^[0-9a-f]{32}$#iS', $_COOKIE[$this->_csrf_cookie_name]) === 1)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400873 {
874 return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name];
875 }
David Behler07b53422011-08-15 00:25:06 +0200876
Chris Berthed93e6f32011-09-25 10:33:25 -0400877 $this->_csrf_hash = md5(uniqid(rand(), TRUE));
878 $this->csrf_set_cookie();
Pascal Krietec9c045a2011-04-05 14:50:41 -0400879 }
880
881 return $this->_csrf_hash;
882 }
883
Derek Jonese701d762010-03-02 18:17:01 -0600884}
Derek Jonese701d762010-03-02 18:17:01 -0600885
886/* End of file Security.php */
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300887/* Location: ./system/core/Security.php */