blob: 49e5ab411b9139e91d316f7485966048174add53 [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 /**
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 Andreevc67c3fb2014-01-22 13:26:00 +020065 * HTML5 entities
66 *
67 * @var array
68 */
69 public $html5_entities = array(
70 '&colon;' => ':',
71 '&lpar;' => '(',
Andrey Andreevd98cbb82014-01-24 18:34:27 +020072 '&rpar;' => ')',
Andrey Andreevee7633c2014-01-24 18:47:20 +020073 '&newline;' => "\n",
74 '&tab;' => "\t"
Andrey Andreevc67c3fb2014-01-22 13:26:00 +020075 );
76
77 /**
Andrey Andreev64354102012-10-28 14:16:02 +020078 * XSS Hash
David Behler07b53422011-08-15 00:25:06 +020079 *
Andrey Andreev64354102012-10-28 14:16:02 +020080 * Random Hash for protecting URLs.
81 *
82 * @var string
David Behler07b53422011-08-15 00:25:06 +020083 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040084 protected $_xss_hash = '';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030085
David Behler07b53422011-08-15 00:25:06 +020086 /**
Andrey Andreev64354102012-10-28 14:16:02 +020087 * CSRF Hash
David Behler07b53422011-08-15 00:25:06 +020088 *
Andrey Andreev64354102012-10-28 14:16:02 +020089 * Random hash for Cross Site Request Forgery protection cookie
90 *
91 * @var string
David Behler07b53422011-08-15 00:25:06 +020092 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040093 protected $_csrf_hash = '';
Andrey Andreev3d113bd2011-10-05 00:03:20 +030094
David Behler07b53422011-08-15 00:25:06 +020095 /**
Andrey Andreev64354102012-10-28 14:16:02 +020096 * CSRF Expire time
David Behler07b53422011-08-15 00:25:06 +020097 *
Andrey Andreev64354102012-10-28 14:16:02 +020098 * Expiration time for Cross Site Request Forgery protection cookie.
99 * Defaults to two hours (in seconds).
100 *
101 * @var int
David Behler07b53422011-08-15 00:25:06 +0200102 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400103 protected $_csrf_expire = 7200;
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300104
David Behler07b53422011-08-15 00:25:06 +0200105 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200106 * CSRF Token name
David Behler07b53422011-08-15 00:25:06 +0200107 *
Andrey Andreev64354102012-10-28 14:16:02 +0200108 * Token name for Cross Site Request Forgery protection cookie.
109 *
110 * @var string
David Behler07b53422011-08-15 00:25:06 +0200111 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400112 protected $_csrf_token_name = 'ci_csrf_token';
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300113
David Behler07b53422011-08-15 00:25:06 +0200114 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200115 * CSRF Cookie name
David Behler07b53422011-08-15 00:25:06 +0200116 *
Andrey Andreev64354102012-10-28 14:16:02 +0200117 * Cookie name for Cross Site Request Forgery protection cookie.
118 *
119 * @var string
David Behler07b53422011-08-15 00:25:06 +0200120 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400121 protected $_csrf_cookie_name = 'ci_csrf_token';
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300122
David Behler07b53422011-08-15 00:25:06 +0200123 /**
124 * List of never allowed strings
125 *
Andrey Andreev64354102012-10-28 14:16:02 +0200126 * @var array
David Behler07b53422011-08-15 00:25:06 +0200127 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -0400128 protected $_never_allowed_str = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400129 'document.cookie' => '[removed]',
130 'document.write' => '[removed]',
131 '.parentNode' => '[removed]',
132 '.innerHTML' => '[removed]',
Timothy Warren40403d22012-04-19 16:38:50 -0400133 '-moz-binding' => '[removed]',
134 '<!--' => '&lt;!--',
135 '-->' => '--&gt;',
136 '<![CDATA[' => '&lt;![CDATA[',
137 '<comment>' => '&lt;comment&gt;'
138 );
Derek Jonese701d762010-03-02 18:17:01 -0600139
David Behler07b53422011-08-15 00:25:06 +0200140 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200141 * List of never allowed regex replacements
David Behler07b53422011-08-15 00:25:06 +0200142 *
Andrey Andreev64354102012-10-28 14:16:02 +0200143 * @var array
David Behler07b53422011-08-15 00:25:06 +0200144 */
Pascal Krietec9c045a2011-04-05 14:50:41 -0400145 protected $_never_allowed_regex = array(
Timothy Warren40403d22012-04-19 16:38:50 -0400146 'javascript\s*:',
Andrey Andreev1bbc5642014-01-07 12:45:27 +0200147 '(document|(document\.)?window)\.(location|on\w*)',
Timothy Warren40403d22012-04-19 16:38:50 -0400148 'expression\s*(\(|&\#40;)', // CSS and IE
149 'vbscript\s*:', // IE, surprise!
Andrey Andreev43568062014-01-21 23:52:31 +0200150 'Redirect\s+30\d',
Wes Bakerd3481352012-05-07 16:49:33 -0400151 "([\"'])?data\s*:[^\\1]*?base64[^\\1]*?,[^\\1]*?\\1?"
Timothy Warren40403d22012-04-19 16:38:50 -0400152 );
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300153
Timothy Warrenad475052012-04-19 13:21:06 -0400154 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200155 * Class constructor
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300156 *
157 * @return void
Timothy Warrenad475052012-04-19 13:21:06 -0400158 */
Greg Akera9263282010-11-10 15:26:43 -0600159 public function __construct()
Derek Jonese701d762010-03-02 18:17:01 -0600160 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200161 // Is CSRF protection enabled?
162 if (config_item('csrf_protection') === TRUE)
patworkef1a55a2011-04-09 13:04:06 +0200163 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200164 // CSRF config
165 foreach (array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key)
patworkef1a55a2011-04-09 13:04:06 +0200166 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200167 if (FALSE !== ($val = config_item($key)))
168 {
169 $this->{'_'.$key} = $val;
170 }
patworkef1a55a2011-04-09 13:04:06 +0200171 }
patworkef1a55a2011-04-09 13:04:06 +0200172
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200173 // Append application specific cookie prefix
174 if (config_item('cookie_prefix'))
175 {
176 $this->_csrf_cookie_name = config_item('cookie_prefix').$this->_csrf_cookie_name;
177 }
Derek Jonesb3f10a22010-07-25 19:11:26 -0500178
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200179 // Set the CSRF hash
180 $this->_csrf_set_hash();
181 }
Derek Allard958543a2010-07-22 14:10:26 -0400182
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200183 log_message('debug', 'Security Class Initialized');
Derek Jonese701d762010-03-02 18:17:01 -0600184 }
185
186 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Jonese701d762010-03-02 18:17:01 -0600188 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200189 * CSRF Verify
Derek Jonese701d762010-03-02 18:17:01 -0600190 *
Andrew Podner4296a652012-12-17 07:51:15 -0500191 * @return CI_Security
Derek Jonese701d762010-03-02 18:17:01 -0600192 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500193 public function csrf_verify()
Derek Allard958543a2010-07-22 14:10:26 -0400194 {
Andrey Andreev5d27c432012-03-08 12:01:52 +0200195 // If it's not a POST request we will set the CSRF cookie
196 if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST')
Derek Jonese701d762010-03-02 18:17:01 -0600197 {
198 return $this->csrf_set_cookie();
199 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300200
Alex Bilbieaeb2c3e2011-08-21 16:14:54 +0100201 // Check if URI has been whitelisted from CSRF checks
202 if ($exclude_uris = config_item('csrf_exclude_uris'))
203 {
204 $uri = load_class('URI', 'core');
205 if (in_array($uri->uri_string(), $exclude_uris))
206 {
207 return $this;
208 }
209 }
Derek Jonese701d762010-03-02 18:17:01 -0600210
211 // Do the tokens exist in both the _POST and _COOKIE arrays?
Andrey Andreevf795ab52012-10-24 21:28:25 +0300212 if ( ! isset($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name])
Alex Bilbieed944a32012-06-02 11:07:47 +0100213 OR $_POST[$this->_csrf_token_name] !== $_COOKIE[$this->_csrf_cookie_name]) // Do the tokens match?
Derek Jonese701d762010-03-02 18:17:01 -0600214 {
215 $this->csrf_show_error();
216 }
217
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200218 // We kill this since we're done and we don't want to polute the _POST array
Pascal Krietec9c045a2011-04-05 14:50:41 -0400219 unset($_POST[$this->_csrf_token_name]);
Barry Mienydd671972010-10-04 16:33:58 +0200220
RS712be25a62011-12-31 16:02:04 -0200221 // Regenerate on every submission?
222 if (config_item('csrf_regenerate'))
223 {
224 // Nothing should last forever
225 unset($_COOKIE[$this->_csrf_cookie_name]);
226 $this->_csrf_hash = '';
227 }
Andrey Andreev8a7d0782012-01-08 05:43:42 +0200228
Derek Jonesb3f10a22010-07-25 19:11:26 -0500229 $this->_csrf_set_hash();
230 $this->csrf_set_cookie();
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300231
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200232 log_message('debug', 'CSRF token verified');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400233 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Jonese701d762010-03-02 18:17:01 -0600236 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Jonese701d762010-03-02 18:17:01 -0600238 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200239 * CSRF Set Cookie
Derek Jonese701d762010-03-02 18:17:01 -0600240 *
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700241 * @codeCoverageIgnore
Andrew Podner4296a652012-12-17 07:51:15 -0500242 * @return CI_Security
Derek Jonese701d762010-03-02 18:17:01 -0600243 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500244 public function csrf_set_cookie()
Derek Jonese701d762010-03-02 18:17:01 -0600245 {
Pascal Krietec9c045a2011-04-05 14:50:41 -0400246 $expire = time() + $this->_csrf_expire;
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300247 $secure_cookie = (bool) config_item('cookie_secure');
Derek Jonese701d762010-03-02 18:17:01 -0600248
Andrey Andreev3fb02672012-10-22 16:48:01 +0300249 if ($secure_cookie && ! is_https())
Derek Jonese701d762010-03-02 18:17:01 -0600250 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200251 return FALSE;
Derek Jonese701d762010-03-02 18:17:01 -0600252 }
Derek Allard958543a2010-07-22 14:10:26 -0400253
freewil4ad0fd82012-03-13 22:37:42 -0400254 setcookie(
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300255 $this->_csrf_cookie_name,
256 $this->_csrf_hash,
257 $expire,
258 config_item('cookie_path'),
259 config_item('cookie_domain'),
freewil4ad0fd82012-03-13 22:37:42 -0400260 $secure_cookie,
261 config_item('cookie_httponly')
262 );
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200263 log_message('debug', 'CRSF cookie Set');
David Behler07b53422011-08-15 00:25:06 +0200264
Pascal Krietec9c045a2011-04-05 14:50:41 -0400265 return $this;
Derek Jonese701d762010-03-02 18:17:01 -0600266 }
267
268 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Jonese701d762010-03-02 18:17:01 -0600270 /**
271 * Show CSRF Error
272 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400273 * @return void
Derek Jonese701d762010-03-02 18:17:01 -0600274 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500275 public function csrf_show_error()
Derek Jonese701d762010-03-02 18:17:01 -0600276 {
277 show_error('The action you have requested is not allowed.');
278 }
279
280 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Jonese701d762010-03-02 18:17:01 -0600282 /**
David Behler07b53422011-08-15 00:25:06 +0200283 * Get CSRF Hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400284 *
Andrey Andreev64354102012-10-28 14:16:02 +0200285 * @see CI_Security::$_csrf_hash
286 * @return string CSRF hash
Pascal Krietec9c045a2011-04-05 14:50:41 -0400287 */
288 public function get_csrf_hash()
289 {
290 return $this->_csrf_hash;
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Get CSRF Token Name
297 *
Andrey Andreev64354102012-10-28 14:16:02 +0200298 * @see CI_Security::$_csrf_token_name
299 * @return string CSRF token name
Pascal Krietec9c045a2011-04-05 14:50:41 -0400300 */
301 public function get_csrf_token_name()
302 {
303 return $this->_csrf_token_name;
304 }
305
306 // --------------------------------------------------------------------
307
308 /**
Derek Jonese701d762010-03-02 18:17:01 -0600309 * XSS Clean
310 *
311 * Sanitizes data so that Cross Site Scripting Hacks can be
Andrey Andreev64354102012-10-28 14:16:02 +0200312 * prevented. This method does a fair amount of work but
Derek Jonese701d762010-03-02 18:17:01 -0600313 * it is extremely thorough, designed to prevent even the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500314 * most obscure XSS attempts. Nothing is ever 100% foolproof,
Derek Jonese701d762010-03-02 18:17:01 -0600315 * of course, but I haven't been able to get anything passed
316 * the filter.
317 *
Andrey Andreev64354102012-10-28 14:16:02 +0200318 * Note: Should only be used to deal with data upon submission.
319 * It's not something that should be used for general
320 * runtime processing.
Derek Jonese701d762010-03-02 18:17:01 -0600321 *
Andrey Andreev64354102012-10-28 14:16:02 +0200322 * @link http://channel.bitflux.ch/wiki/XSS_Prevention
323 * Based in part on some code and ideas from Bitflux.
Derek Jonese701d762010-03-02 18:17:01 -0600324 *
Andrey Andreev64354102012-10-28 14:16:02 +0200325 * @link http://ha.ckers.org/xss.html
326 * To help develop this script I used this great list of
327 * vulnerabilities along with a few other hacks I've
328 * harvested from examining vulnerabilities in other programs.
Derek Jonese701d762010-03-02 18:17:01 -0600329 *
Andrey Andreev64354102012-10-28 14:16:02 +0200330 * @param string|string[] $str Input data
331 * @param bool $is_image Whether the input is an image
Derek Jonese701d762010-03-02 18:17:01 -0600332 * @return string
333 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500334 public function xss_clean($str, $is_image = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600335 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200336 // Is the string an array?
Derek Jonese701d762010-03-02 18:17:01 -0600337 if (is_array($str))
338 {
339 while (list($key) = each($str))
340 {
341 $str[$key] = $this->xss_clean($str[$key]);
342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Jonese701d762010-03-02 18:17:01 -0600344 return $str;
345 }
346
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200347 // Remove Invisible Characters and validate entities in URLs
348 $str = $this->_validate_entities(remove_invisible_characters($str));
Derek Jonese701d762010-03-02 18:17:01 -0600349
350 /*
351 * URL Decode
352 *
353 * Just in case stuff like this is submitted:
354 *
355 * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
356 *
357 * Note: Use rawurldecode() so it does not remove plus signs
Derek Jonese701d762010-03-02 18:17:01 -0600358 */
359 $str = rawurldecode($str);
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Jonese701d762010-03-02 18:17:01 -0600361 /*
Barry Mienydd671972010-10-04 16:33:58 +0200362 * Convert character entities to ASCII
Derek Jonese701d762010-03-02 18:17:01 -0600363 *
364 * This permits our tests below to work reliably.
365 * We only convert entities that are within tags since
366 * these are the ones that will pose security problems.
Derek Jonese701d762010-03-02 18:17:01 -0600367 */
Derek Jonese701d762010-03-02 18:17:01 -0600368 $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str);
brian97807ccbe52012-12-11 20:24:12 +0200369 $str = preg_replace_callback('/<\w+.*/si', array($this, '_decode_entity'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600370
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200371 // Remove Invisible Characters Again!
Greg Aker757dda62010-04-14 19:06:19 -0500372 $str = remove_invisible_characters($str);
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Jonese701d762010-03-02 18:17:01 -0600374 /*
375 * Convert all tabs to spaces
376 *
377 * This prevents strings like this: ja vascript
378 * NOTE: we deal with spaces between characters later.
David Behler07b53422011-08-15 00:25:06 +0200379 * NOTE: preg_replace was found to be amazingly slow here on
Pascal Krietec9c045a2011-04-05 14:50:41 -0400380 * large blocks of data, so we use str_replace.
Derek Jonese701d762010-03-02 18:17:01 -0600381 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200382 $str = str_replace("\t", ' ', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200383
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200384 // Capture converted string for later comparison
Derek Jonese701d762010-03-02 18:17:01 -0600385 $converted_string = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200386
Pascal Krietec9c045a2011-04-05 14:50:41 -0400387 // Remove Strings that are never allowed
388 $str = $this->_do_never_allowed($str);
Derek Jonese701d762010-03-02 18:17:01 -0600389
390 /*
391 * Makes PHP tags safe
392 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400393 * Note: XML tags are inadvertently replaced too:
Derek Jonese701d762010-03-02 18:17:01 -0600394 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400395 * <?xml
Derek Jonese701d762010-03-02 18:17:01 -0600396 *
397 * But it doesn't seem to pose a problem.
Derek Jonese701d762010-03-02 18:17:01 -0600398 */
399 if ($is_image === TRUE)
400 {
David Behler07b53422011-08-15 00:25:06 +0200401 // Images have a tendency to have the PHP short opening and
402 // closing tags every so often so we skip those and only
Pascal Krietec9c045a2011-04-05 14:50:41 -0400403 // do the long opening tags.
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200404 $str = preg_replace('/<\?(php)/i', '&lt;?\\1', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600405 }
406 else
407 {
Andrey Andreev838a9d62012-12-03 14:37:47 +0200408 $str = str_replace(array('<?', '?'.'>'), array('&lt;?', '?&gt;'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Jonese701d762010-03-02 18:17:01 -0600411 /*
412 * Compact any exploded words
413 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500414 * This corrects words like: j a v a s c r i p t
Derek Jonese701d762010-03-02 18:17:01 -0600415 * These words are compacted back to their correct state.
Derek Jonese701d762010-03-02 18:17:01 -0600416 */
Pascal Krietec9c045a2011-04-05 14:50:41 -0400417 $words = array(
Wes Bakerd3481352012-05-07 16:49:33 -0400418 'javascript', 'expression', 'vbscript', 'script', 'base64',
Timothy Warren40403d22012-04-19 16:38:50 -0400419 'applet', 'alert', 'document', 'write', 'cookie', 'window'
420 );
David Behler07b53422011-08-15 00:25:06 +0200421
Derek Jonese701d762010-03-02 18:17:01 -0600422 foreach ($words as $word)
423 {
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200424 $word = implode('\s*', str_split($word)).'\s*';
Derek Jonese701d762010-03-02 18:17:01 -0600425
426 // We only want to do this when it is followed by a non-word character
427 // That way valid stuff like "dealer to" does not become "dealerto"
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300428 $str = preg_replace_callback('#('.substr($word, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Jonese701d762010-03-02 18:17:01 -0600431 /*
432 * Remove disallowed Javascript in links or img tags
David Behler07b53422011-08-15 00:25:06 +0200433 * We used to do some version comparisons and use of stripos for PHP5,
434 * but it is dog slow compared to these simplified non-capturing
Pascal Krietec9c045a2011-04-05 14:50:41 -0400435 * preg_match(), especially if the pattern exists in the string
Derek Jonese701d762010-03-02 18:17:01 -0600436 */
437 do
438 {
439 $original = $str;
Barry Mienydd671972010-10-04 16:33:58 +0200440
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200441 if (preg_match('/<a/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600442 {
vlakoffa81f60c2012-07-02 15:20:11 +0200443 $str = preg_replace_callback('#<a\s+([^>]*?)(?:>|$)#si', array($this, '_js_link_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600444 }
Barry Mienydd671972010-10-04 16:33:58 +0200445
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200446 if (preg_match('/<img/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600447 {
vlakoffa81f60c2012-07-02 15:20:11 +0200448 $str = preg_replace_callback('#<img\s+([^>]*?)(?:\s?/?>|$)#si', array($this, '_js_img_removal'), $str);
Derek Jonese701d762010-03-02 18:17:01 -0600449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
vlakoffa81f60c2012-07-02 15:20:11 +0200451 if (preg_match('/script|xss/i', $str))
Derek Jonese701d762010-03-02 18:17:01 -0600452 {
vlakoffa81f60c2012-07-02 15:20:11 +0200453 $str = preg_replace('#</*(?:script|xss).*?>#si', '[removed]', $str);
Derek Jonese701d762010-03-02 18:17:01 -0600454 }
455 }
vlakoffa81f60c2012-07-02 15:20:11 +0200456 while ($original !== $str);
Derek Jonese701d762010-03-02 18:17:01 -0600457
458 unset($original);
459
Pascal Krietec9c045a2011-04-05 14:50:41 -0400460 // Remove evil attributes such as style, onclick and xmlns
461 $str = $this->_remove_evil_attributes($str, $is_image);
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Jonese701d762010-03-02 18:17:01 -0600463 /*
464 * Sanitize naughty HTML elements
465 *
466 * If a tag containing any of the words in the list
467 * below is found, the tag gets converted to entities.
468 *
469 * So this: <blink>
470 * Becomes: &lt;blink&gt;
Derek Jonese701d762010-03-02 18:17:01 -0600471 */
Andrey Andreevc53a1782014-01-24 19:32:48 +0200472 $naughty = 'alert|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|video|svg|xml|xss';
Derek Jonese701d762010-03-02 18:17:01 -0600473 $str = preg_replace_callback('#<(/*\s*)('.$naughty.')([^><]*)([><]*)#is', array($this, '_sanitize_naughty_html'), $str);
474
475 /*
476 * Sanitize naughty scripting elements
477 *
478 * Similar to above, only instead of looking for
479 * tags it looks for PHP and JavaScript commands
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200480 * that are disallowed. Rather than removing the
Derek Jonese701d762010-03-02 18:17:01 -0600481 * code, it simply converts the parenthesis to entities
482 * rendering the code un-executable.
483 *
484 * For example: eval('some code')
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200485 * Becomes: eval&#40;'some code'&#41;
Derek Jonese701d762010-03-02 18:17:01 -0600486 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200487 $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si',
488 '\\1\\2&#40;\\3&#41;',
489 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200490
Pascal Krietec9c045a2011-04-05 14:50:41 -0400491 // Final clean up
492 // This adds a bit of extra precaution in case
493 // something got through the above filters
494 $str = $this->_do_never_allowed($str);
Derek Jonese701d762010-03-02 18:17:01 -0600495
496 /*
Pascal Krietec9c045a2011-04-05 14:50:41 -0400497 * Images are Handled in a Special Way
David Behler07b53422011-08-15 00:25:06 +0200498 * - Essentially, we want to know that after all of the character
499 * conversion is done whether any unwanted, likely XSS, code was found.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400500 * If not, we return TRUE, as the image is clean.
David Behler07b53422011-08-15 00:25:06 +0200501 * However, if the string post-conversion does not matched the
502 * string post-removal of XSS, then it fails, as there was unwanted XSS
Pascal Krietec9c045a2011-04-05 14:50:41 -0400503 * code found and removed/changed during processing.
Derek Jonese701d762010-03-02 18:17:01 -0600504 */
Derek Jonese701d762010-03-02 18:17:01 -0600505 if ($is_image === TRUE)
506 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200507 return ($str === $converted_string);
Derek Jonese701d762010-03-02 18:17:01 -0600508 }
Barry Mienydd671972010-10-04 16:33:58 +0200509
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200510 log_message('debug', 'XSS Filtering completed');
Derek Jonese701d762010-03-02 18:17:01 -0600511 return $str;
512 }
513
514 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Jonese701d762010-03-02 18:17:01 -0600516 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200517 * XSS Hash
Derek Jonese701d762010-03-02 18:17:01 -0600518 *
Andrey Andreev64354102012-10-28 14:16:02 +0200519 * Generates the XSS hash if needed and returns it.
520 *
521 * @see CI_Security::$_xss_hash
522 * @return string XSS hash
Derek Jonese701d762010-03-02 18:17:01 -0600523 */
Eric Barnes9805ecc2011-01-16 23:35:16 -0500524 public function xss_hash()
Barry Mienydd671972010-10-04 16:33:58 +0200525 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100526 if ($this->_xss_hash === '')
Derek Jonese701d762010-03-02 18:17:01 -0600527 {
vlakoff06127562013-03-30 00:06:39 +0100528 $this->_xss_hash = md5(uniqid(mt_rand()));
Derek Jonese701d762010-03-02 18:17:01 -0600529 }
530
Pascal Krietec9c045a2011-04-05 14:50:41 -0400531 return $this->_xss_hash;
Derek Jonese701d762010-03-02 18:17:01 -0600532 }
533
534 // --------------------------------------------------------------------
535
536 /**
Derek Jonesa0911472010-03-30 10:33:09 -0500537 * HTML Entities Decode
538 *
Andrey Andreev64354102012-10-28 14:16:02 +0200539 * A replacement for html_entity_decode()
Derek Jonesa0911472010-03-30 10:33:09 -0500540 *
Pascal Krietec38e3b62011-11-14 13:55:00 -0500541 * The reason we are not using html_entity_decode() by itself is because
542 * while it is not technically correct to leave out the semicolon
543 * at the end of an entity most browsers will still interpret the entity
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200544 * correctly. html_entity_decode() does not convert entities without
Pascal Krietec38e3b62011-11-14 13:55:00 -0500545 * semicolons, so we are left with our own little solution here. Bummer.
Derek Jonesa0911472010-03-30 10:33:09 -0500546 *
Andrey Andreev64354102012-10-28 14:16:02 +0200547 * @link http://php.net/html-entity-decode
548 *
549 * @param string $str Input
550 * @param string $charset Character set
Derek Jonesa0911472010-03-30 10:33:09 -0500551 * @return string
552 */
freewil8cc0cfe2011-08-27 21:53:00 -0400553 public function entity_decode($str, $charset = NULL)
Derek Jonesa0911472010-03-30 10:33:09 -0500554 {
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300555 if (strpos($str, '&') === FALSE)
freewil5c9b0d12011-08-28 12:15:23 -0400556 {
557 return $str;
558 }
Andrey Andreev3d113bd2011-10-05 00:03:20 +0300559
freewil5c9b0d12011-08-28 12:15:23 -0400560 if (empty($charset))
561 {
562 $charset = config_item('charset');
563 }
Barry Mienydd671972010-10-04 16:33:58 +0200564
brian978638a9d22012-12-18 13:25:54 +0200565 do
566 {
Andrey Andreev99e2f8e2014-01-19 00:04:44 +0200567 $m1 = $m2 = 0;
brian97807ccbe52012-12-11 20:24:12 +0200568
Andrey Andreev99e2f8e2014-01-19 00:04:44 +0200569 $str = preg_replace('/(&#x0*[0-9a-f]{2,5})(?![0-9a-f;])/iS', '$1;', $str, -1, $m1);
570 $str = preg_replace('/(&#\d{2,4})(?![0-9;])/S', '$1;', $str, -1, $m2);
brian978638a9d22012-12-18 13:25:54 +0200571 $str = html_entity_decode($str, ENT_COMPAT, $charset);
brian978638a9d22012-12-18 13:25:54 +0200572 }
Andrey Andreev99e2f8e2014-01-19 00:04:44 +0200573 while ($m1 OR $m2);
brian978f50fc732012-12-08 23:22:26 +0200574
brian978638a9d22012-12-18 13:25:54 +0200575 return $str;
Derek Jonesa0911472010-03-30 10:33:09 -0500576 }
Barry Mienydd671972010-10-04 16:33:58 +0200577
Derek Jonesa0911472010-03-30 10:33:09 -0500578 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Jonesa0911472010-03-30 10:33:09 -0500580 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200581 * Sanitize Filename
Derek Jonese701d762010-03-02 18:17:01 -0600582 *
Andrey Andreev64354102012-10-28 14:16:02 +0200583 * @param string $str Input file name
584 * @param bool $relative_path Whether to preserve paths
Derek Jonese701d762010-03-02 18:17:01 -0600585 * @return string
586 */
Hunter Wu8df33522013-08-03 22:36:05 +0800587 public function sanitize_filename($str, $relative_path = FALSE)
Derek Jonese701d762010-03-02 18:17:01 -0600588 {
Hunter Wu4495cc72013-08-04 12:31:52 +0800589 $bad = $this->filename_bad_chars;
David Behler07b53422011-08-15 00:25:06 +0200590
Derek Jones2ef37592010-10-06 17:51:59 -0500591 if ( ! $relative_path)
592 {
593 $bad[] = './';
594 $bad[] = '/';
595 }
Derek Jonese701d762010-03-02 18:17:01 -0600596
Pascal Krietec9c045a2011-04-05 14:50:41 -0400597 $str = remove_invisible_characters($str, FALSE);
Andrey Andreev7e559772013-01-29 15:38:33 +0200598
599 do
600 {
601 $old = $str;
602 $str = str_replace($bad, '', $str);
603 }
604 while ($old !== $str);
605
606 return stripslashes($str);
Derek Jonese701d762010-03-02 18:17:01 -0600607 }
608
Pascal Krietec9c045a2011-04-05 14:50:41 -0400609 // ----------------------------------------------------------------
610
611 /**
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300612 * Strip Image Tags
613 *
Andrey Andreev64354102012-10-28 14:16:02 +0200614 * @param string $str
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300615 * @return string
616 */
617 public function strip_image_tags($str)
618 {
David Cox Jr46e77e02013-10-03 16:56:04 -0400619 return preg_replace(array('#<img[\s/]+.*?src\s*=\s*["\'](.+?)["\'].*?\>#', '#<img[\s/]+.*?src\s*=\s*(.+?).*?\>#'), '\\1', $str);
Andrey Andreev1a24a9d2012-06-27 00:52:47 +0300620 }
621
622 // ----------------------------------------------------------------
623
624 /**
Pascal Krietec9c045a2011-04-05 14:50:41 -0400625 * Compact Exploded Words
626 *
Andrey Andreev64354102012-10-28 14:16:02 +0200627 * Callback method for xss_clean() to remove whitespace from
628 * things like 'j a v a s c r i p t'.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400629 *
Andrey Andreev64354102012-10-28 14:16:02 +0200630 * @used-by CI_Security::xss_clean()
631 * @param array $matches
Timothy Warrenad475052012-04-19 13:21:06 -0400632 * @return string
Pascal Krietec9c045a2011-04-05 14:50:41 -0400633 */
634 protected function _compact_exploded_words($matches)
635 {
636 return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
637 }
638
639 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200640
Timothy Warrenad475052012-04-19 13:21:06 -0400641 /**
642 * Remove Evil HTML Attributes (like event handlers and style)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400643 *
644 * It removes the evil attribute and either:
Pascal Krietec9c045a2011-04-05 14:50:41 -0400645 *
Andrey Andreev64354102012-10-28 14:16:02 +0200646 * - Everything up until a space. For example, everything between the pipes:
647 *
648 * <code>
649 * <a |style=document.write('hello');alert('world');| class=link>
650 * </code>
651 *
652 * - Everything inside the quotes. For example, everything between the pipes:
653 *
654 * <code>
655 * <a |style="document.write('hello'); alert('world');"| class="link">
656 * </code>
657 *
658 * @param string $str The string to check
659 * @param bool $is_image Whether the input is an image
660 * @return string The string with the evil attributes removed
Pascal Krietec9c045a2011-04-05 14:50:41 -0400661 */
662 protected function _remove_evil_attributes($str, $is_image)
663 {
Andrey Andreev1bbc5642014-01-07 12:45:27 +0200664 // Formaction, style, and xmlns
Andrey Andreev25ca2352014-01-24 18:46:29 +0200665 $evil_attributes = array('style', 'xmlns', 'formaction', 'form', 'xlink:href');
Pascal Krietec9c045a2011-04-05 14:50:41 -0400666
667 if ($is_image === TRUE)
668 {
669 /*
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200670 * Adobe Photoshop puts XML metadata into JFIF images,
Pascal Krietec9c045a2011-04-05 14:50:41 -0400671 * including namespacing, so we have to allow this for images.
672 */
673 unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
674 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200675
Pascal Krietec9c045a2011-04-05 14:50:41 -0400676 do {
Pascal Krietec38e3b62011-11-14 13:55:00 -0500677 $count = 0;
678 $attribs = array();
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200679
brian978160c7d12012-12-03 21:18:20 +0200680 // find occurrences of illegal attribute strings with quotes (042 and 047 are octal quotes)
681 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 +0200682
Pascal Krietec38e3b62011-11-14 13:55:00 -0500683 foreach ($matches as $attr)
684 {
685 $attribs[] = preg_quote($attr[0], '/');
686 }
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200687
brian978160c7d12012-12-03 21:18:20 +0200688 // find occurrences of illegal attribute strings without quotes
689 preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*([^\s>]*)/is', $str, $matches, PREG_SET_ORDER);
David Behler07b53422011-08-15 00:25:06 +0200690
Pascal Krietec38e3b62011-11-14 13:55:00 -0500691 foreach ($matches as $attr)
692 {
693 $attribs[] = preg_quote($attr[0], '/');
694 }
695
696 // replace illegal attribute strings that are inside an html tag
697 if (count($attribs) > 0)
698 {
brian978160c7d12012-12-03 21:18:20 +0200699 $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 -0500700 }
Andrey Andreev72ed4c32012-12-19 17:07:54 +0200701 }
702 while ($count);
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200703
Pascal Krietec9c045a2011-04-05 14:50:41 -0400704 return $str;
705 }
David Behler07b53422011-08-15 00:25:06 +0200706
Pascal Krietec9c045a2011-04-05 14:50:41 -0400707 // --------------------------------------------------------------------
708
709 /**
710 * Sanitize Naughty HTML
711 *
Andrey Andreev64354102012-10-28 14:16:02 +0200712 * Callback method for xss_clean() to remove naughty HTML elements.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400713 *
Andrey Andreev64354102012-10-28 14:16:02 +0200714 * @used-by CI_Security::xss_clean()
715 * @param array $matches
Pascal Krietec9c045a2011-04-05 14:50:41 -0400716 * @return string
717 */
718 protected function _sanitize_naughty_html($matches)
719 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200720 return '&lt;'.$matches[1].$matches[2].$matches[3] // encode opening brace
721 // encode captured opening or closing brace to prevent recursive vectors:
Andrey Andreev67ccdc02012-02-27 23:57:58 +0200722 .str_replace(array('>', '<'), array('&gt;', '&lt;'), $matches[4]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400723 }
724
725 // --------------------------------------------------------------------
726
727 /**
728 * JS Link Removal
729 *
Andrey Andreev64354102012-10-28 14:16:02 +0200730 * Callback method for xss_clean() to sanitize links.
731 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400732 * This limits the PCRE backtracks, making it more performance friendly
733 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200734 * PHP 5.2+ on link-heavy strings.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400735 *
Andrey Andreev64354102012-10-28 14:16:02 +0200736 * @used-by CI_Security::xss_clean()
737 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400738 * @return string
739 */
740 protected function _js_link_removal($match)
741 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200742 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200743 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 +0200744 '',
745 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
746 ),
747 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400748 }
749
750 // --------------------------------------------------------------------
751
752 /**
753 * JS Image Removal
754 *
Andrey Andreev64354102012-10-28 14:16:02 +0200755 * Callback method for xss_clean() to sanitize image tags.
756 *
Pascal Krietec9c045a2011-04-05 14:50:41 -0400757 * This limits the PCRE backtracks, making it more performance friendly
758 * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
Andrey Andreev64354102012-10-28 14:16:02 +0200759 * PHP 5.2+ on image tag heavy strings.
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 $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400763 * @return string
764 */
765 protected function _js_img_removal($match)
766 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200767 return str_replace($match[1],
vlakoffa81f60c2012-07-02 15:20:11 +0200768 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 +0200769 '',
770 $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]))
771 ),
772 $match[0]);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400773 }
774
775 // --------------------------------------------------------------------
776
777 /**
778 * Attribute Conversion
779 *
Andrey Andreev64354102012-10-28 14:16:02 +0200780 * @used-by CI_Security::xss_clean()
781 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400782 * @return string
783 */
784 protected function _convert_attribute($match)
785 {
786 return str_replace(array('>', '<', '\\'), array('&gt;', '&lt;', '\\\\'), $match[0]);
787 }
788
789 // --------------------------------------------------------------------
790
791 /**
792 * Filter Attributes
793 *
Andrey Andreev64354102012-10-28 14:16:02 +0200794 * Filters tag attributes for consistency and safety.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400795 *
Andrey Andreev64354102012-10-28 14:16:02 +0200796 * @used-by CI_Security::_js_img_removal()
797 * @used-by CI_Security::_js_link_removal()
798 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400799 * @return string
800 */
801 protected function _filter_attributes($str)
802 {
803 $out = '';
Pascal Krietec9c045a2011-04-05 14:50:41 -0400804 if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches))
805 {
806 foreach ($matches[0] as $match)
807 {
Andrey Andreev4562f2c2012-01-09 23:39:50 +0200808 $out .= preg_replace('#/\*.*?\*/#s', '', $match);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400809 }
810 }
811
812 return $out;
813 }
814
815 // --------------------------------------------------------------------
816
817 /**
818 * HTML Entity Decode Callback
819 *
Andrey Andreev64354102012-10-28 14:16:02 +0200820 * @used-by CI_Security::xss_clean()
821 * @param array $match
Pascal Krietec9c045a2011-04-05 14:50:41 -0400822 * @return string
823 */
824 protected function _decode_entity($match)
825 {
Andrey Andreevc67c3fb2014-01-22 13:26:00 +0200826 // entity_decode() won't convert dangerous HTML5 entities
827 // (it could, but ENT_HTML5 is only available since PHP 5.4),
828 // so we'll do that here
829 return str_ireplace(
830 array_keys($this->html5_entities),
831 array_values($this->html5_entities),
832 $this->entity_decode($match[0], strtoupper(config_item('charset')))
833 );
Pascal Krietec9c045a2011-04-05 14:50:41 -0400834 }
835
836 // --------------------------------------------------------------------
David Behler07b53422011-08-15 00:25:06 +0200837
Pascal Krietec9c045a2011-04-05 14:50:41 -0400838 /**
839 * Validate URL entities
840 *
Andrey Andreev64354102012-10-28 14:16:02 +0200841 * @used-by CI_Security::xss_clean()
842 * @param string $str
Pascal Krietec9c045a2011-04-05 14:50:41 -0400843 * @return string
844 */
845 protected function _validate_entities($str)
846 {
847 /*
848 * Protect GET variables in URLs
849 */
David Behler07b53422011-08-15 00:25:06 +0200850
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200851 // 901119URL5918AMP18930PROTECT8198
852 $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 -0400853
854 /*
855 * Validate standard character entities
856 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500857 * Add a semicolon if missing. We do this to enable
Pascal Krietec9c045a2011-04-05 14:50:41 -0400858 * the conversion of entities to ASCII later.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400859 */
Andrey Andreev4d057162014-01-20 11:17:34 +0200860 $str = preg_replace('/(&#\d{2,4})(?![0-9;])/', '$1;', $str);
861 $str = preg_replace('/(&[a-z]{2,})(?![a-z;])/i', '$1;', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400862
863 /*
864 * Validate UTF16 two byte encoding (x00)
865 *
866 * Just as above, adds a semicolon if missing.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400867 */
Andrey Andreev4d057162014-01-20 11:17:34 +0200868 $str = preg_replace('/(&#x0*[0-9a-f]{2,5})(?![0-9a-f;])/i', '$1;', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400869
870 /*
871 * Un-Protect GET variables in URLs
872 */
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200873 return str_replace($this->xss_hash(), '&', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400874 }
875
876 // ----------------------------------------------------------------------
877
878 /**
879 * Do Never Allowed
880 *
Andrey Andreev64354102012-10-28 14:16:02 +0200881 * @used-by CI_Security::xss_clean()
Pascal Krietec9c045a2011-04-05 14:50:41 -0400882 * @param string
883 * @return string
884 */
885 protected function _do_never_allowed($str)
886 {
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200887 $str = str_replace(array_keys($this->_never_allowed_str), $this->_never_allowed_str, $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400888
Andrey Andreevbb488dc2012-01-07 23:35:16 +0200889 foreach ($this->_never_allowed_regex as $regex)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400890 {
Wes Baker5335bc32012-04-24 15:17:14 -0400891 $str = preg_replace('#'.$regex.'#is', '[removed]', $str);
Pascal Krietec9c045a2011-04-05 14:50:41 -0400892 }
David Behler07b53422011-08-15 00:25:06 +0200893
Pascal Krietec9c045a2011-04-05 14:50:41 -0400894 return $str;
895 }
896
897 // --------------------------------------------------------------------
898
899 /**
Andrey Andreev64354102012-10-28 14:16:02 +0200900 * Set CSRF Hash and Cookie
Pascal Krietec9c045a2011-04-05 14:50:41 -0400901 *
902 * @return string
903 */
904 protected function _csrf_set_hash()
905 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100906 if ($this->_csrf_hash === '')
Pascal Krietec9c045a2011-04-05 14:50:41 -0400907 {
vlakoff3a3d5f62013-10-17 22:22:16 +0200908 // If the cookie exists we will use its value.
Pascal Krietec9c045a2011-04-05 14:50:41 -0400909 // We don't necessarily want to regenerate it with
David Behler07b53422011-08-15 00:25:06 +0200910 // each page load since a page could contain embedded
Pascal Krietec9c045a2011-04-05 14:50:41 -0400911 // sub-pages causing this feature to fail
David Behler07b53422011-08-15 00:25:06 +0200912 if (isset($_COOKIE[$this->_csrf_cookie_name]) &&
Alexander Hofstedee2c374f2012-05-17 00:28:08 +0200913 preg_match('#^[0-9a-f]{32}$#iS', $_COOKIE[$this->_csrf_cookie_name]) === 1)
Pascal Krietec9c045a2011-04-05 14:50:41 -0400914 {
915 return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name];
916 }
David Behler07b53422011-08-15 00:25:06 +0200917
vlakoff3a3d5f62013-10-17 22:22:16 +0200918 $this->_csrf_hash = md5(uniqid(mt_rand(), TRUE));
Chris Berthed93e6f32011-09-25 10:33:25 -0400919 $this->csrf_set_cookie();
Pascal Krietec9c045a2011-04-05 14:50:41 -0400920 }
921
922 return $this->_csrf_hash;
923 }
924
Derek Jonese701d762010-03-02 18:17:01 -0600925}
Derek Jonese701d762010-03-02 18:17:01 -0600926
927/* End of file Security.php */
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300928/* Location: ./system/core/Security.php */