admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Input Class |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 20 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 21 | * Pre-processes global input data for security |
| 22 | * |
| 23 | * @package CodeIgniter |
| 24 | * @subpackage Libraries |
| 25 | * @category Input |
| 26 | * @author Rick Ellis |
| 27 | * @link http://www.codeigniter.com/user_guide/libraries/input.html |
| 28 | */ |
| 29 | class CI_Input { |
| 30 | var $use_xss_clean = FALSE; |
| 31 | var $ip_address = FALSE; |
| 32 | var $user_agent = FALSE; |
| 33 | var $allow_get_array = FALSE; |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * Sets whether to globally enable the XSS processing |
| 39 | * and whether to allow the $_GET array |
| 40 | * |
| 41 | * @access public |
| 42 | */ |
| 43 | function CI_Input() |
| 44 | { |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 45 | log_message('debug', "Input Class Initialized"); |
| 46 | |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 47 | $CFG =& load_class('Config'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 48 | $this->use_xss_clean = ($CFG->item('global_xss_filtering') === TRUE) ? TRUE : FALSE; |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 49 | $this->allow_get_array = ($CFG->item('enable_query_strings') === TRUE) ? TRUE : FALSE; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 50 | $this->_sanitize_globals(); |
| 51 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 52 | |
| 53 | // -------------------------------------------------------------------- |
| 54 | |
| 55 | /** |
| 56 | * Sanitize Globals |
| 57 | * |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 58 | * This function does the following: |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 59 | * |
| 60 | * Unsets $_GET data (if query strings are not enabled) |
| 61 | * |
| 62 | * Unsets all globals if register_globals is enabled |
| 63 | * |
| 64 | * Standardizes newline characters to \n |
| 65 | * |
| 66 | * @access private |
| 67 | * @return void |
| 68 | */ |
| 69 | function _sanitize_globals() |
| 70 | { |
| 71 | // Unset globals. This is effectively the same as register_globals = off |
| 72 | foreach (array($_GET, $_POST, $_COOKIE) as $global) |
| 73 | { |
| 74 | if ( ! is_array($global)) |
| 75 | { |
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 76 | global $global; |
| 77 | $$global = NULL; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 78 | } |
| 79 | else |
| 80 | { |
| 81 | foreach ($global as $key => $val) |
| 82 | { |
Rick Ellis | 325197e | 2006-11-20 17:29:05 +0000 | [diff] [blame] | 83 | global $$key; |
| 84 | $$key = NULL; |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 85 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 89 | // Is $_GET data allowed? If not we'll set the $_GET to an empty array |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 90 | if ($this->allow_get_array == FALSE) |
| 91 | { |
| 92 | $_GET = array(); |
| 93 | } |
| 94 | |
| 95 | // Clean $_POST Data |
| 96 | if (is_array($_POST) AND count($_POST) > 0) |
| 97 | { |
| 98 | foreach($_POST as $key => $val) |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 99 | { |
| 100 | $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
| 101 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Clean $_COOKIE Data |
| 105 | if (is_array($_COOKIE) AND count($_COOKIE) > 0) |
| 106 | { |
| 107 | foreach($_COOKIE as $key => $val) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 108 | { |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 109 | $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 110 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | log_message('debug', "Global POST and COOKIE data sanitized"); |
| 114 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 115 | |
| 116 | // -------------------------------------------------------------------- |
| 117 | |
| 118 | /** |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 119 | * Clean Input Data |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 120 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 121 | * This is a helper function. It escapes data and |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 122 | * standardizes newline characters to \n |
| 123 | * |
| 124 | * @access private |
| 125 | * @param string |
| 126 | * @return string |
| 127 | */ |
| 128 | function _clean_input_data($str) |
| 129 | { |
| 130 | if (is_array($str)) |
| 131 | { |
| 132 | $new_array = array(); |
| 133 | foreach ($str as $key => $val) |
| 134 | { |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 135 | $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 136 | } |
| 137 | return $new_array; |
| 138 | } |
| 139 | |
| 140 | if ($this->use_xss_clean === TRUE) |
| 141 | { |
| 142 | $str = $this->xss_clean($str); |
| 143 | } |
| 144 | |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 145 | // Standardize newlines |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 146 | return preg_replace("/\015\012|\015|\012/", "\n", $str); |
| 147 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 148 | |
| 149 | // -------------------------------------------------------------------- |
| 150 | |
| 151 | /** |
| 152 | * Clean Keys |
| 153 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 154 | * This is a helper function. To prevent malicious users |
| 155 | * from trying to exploit keys we make sure that keys are |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 156 | * only named with alpha-numeric text and a few other items. |
| 157 | * |
| 158 | * @access private |
| 159 | * @param string |
| 160 | * @return string |
| 161 | */ |
| 162 | function _clean_input_keys($str) |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 163 | { |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 164 | if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str)) |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 165 | { |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 166 | exit('Disallowed Key Characters: '.$str); |
| 167 | } |
| 168 | |
| 169 | if ( ! get_magic_quotes_gpc()) |
| 170 | { |
| 171 | return addslashes($str); |
| 172 | } |
| 173 | |
| 174 | return $str; |
| 175 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 176 | |
| 177 | // -------------------------------------------------------------------- |
| 178 | |
| 179 | /** |
| 180 | * Fetch an item from the POST array |
| 181 | * |
| 182 | * @access public |
| 183 | * @param string |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 184 | * @param bool |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 185 | * @return string |
| 186 | */ |
| 187 | function post($index = '', $xss_clean = FALSE) |
| 188 | { |
| 189 | if ( ! isset($_POST[$index])) |
| 190 | { |
| 191 | return FALSE; |
| 192 | } |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 193 | |
| 194 | if ($xss_clean === TRUE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 195 | { |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 196 | if (is_array($_POST[$index])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 197 | { |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 198 | foreach($_POST[$index] as $key => $val) |
| 199 | { |
| 200 | $_POST[$index][$key] = $this->xss_clean($val); |
| 201 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 202 | } |
| 203 | else |
| 204 | { |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 205 | return $this->xss_clean($_POST[$index]); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 208 | |
| 209 | return $_POST[$index]; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 210 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 211 | |
| 212 | // -------------------------------------------------------------------- |
| 213 | |
| 214 | /** |
| 215 | * Fetch an item from the COOKIE array |
| 216 | * |
| 217 | * @access public |
| 218 | * @param string |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 219 | * @param bool |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 220 | * @return string |
| 221 | */ |
| 222 | function cookie($index = '', $xss_clean = FALSE) |
| 223 | { |
| 224 | if ( ! isset($_COOKIE[$index])) |
| 225 | { |
| 226 | return FALSE; |
| 227 | } |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 228 | |
| 229 | if ($xss_clean === TRUE) |
| 230 | { |
| 231 | if (is_array($_COOKIE[$index])) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 232 | { |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 233 | $cookie = array(); |
| 234 | foreach($_COOKIE[$index] as $key => $val) |
admin | 2fcd16b | 2006-10-03 16:41:54 +0000 | [diff] [blame] | 235 | { |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 236 | $cookie[$key] = $this->xss_clean($val); |
admin | 2fcd16b | 2006-10-03 16:41:54 +0000 | [diff] [blame] | 237 | } |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 238 | |
| 239 | return $cookie; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 240 | } |
| 241 | else |
| 242 | { |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 243 | return $this->xss_clean($_COOKIE[$index]); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 246 | else |
| 247 | { |
| 248 | return $_COOKIE[$index]; |
| 249 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 250 | } |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 251 | |
| 252 | // -------------------------------------------------------------------- |
| 253 | |
| 254 | /** |
| 255 | * Fetch an item from the SERVER array |
| 256 | * |
| 257 | * @access public |
| 258 | * @param string |
| 259 | * @param bool |
| 260 | * @return string |
| 261 | */ |
| 262 | function server($index = '', $xss_clean = FALSE) |
| 263 | { |
| 264 | if ( ! isset($_SERVER[$index])) |
| 265 | { |
| 266 | return FALSE; |
| 267 | } |
| 268 | |
| 269 | if ($xss_clean === TRUE) |
| 270 | { |
| 271 | return $this->xss_clean($_SERVER[$index]); |
| 272 | } |
| 273 | |
| 274 | return $_SERVER[$index]; |
| 275 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 276 | |
| 277 | // -------------------------------------------------------------------- |
| 278 | |
| 279 | /** |
| 280 | * Fetch the IP Address |
| 281 | * |
| 282 | * @access public |
| 283 | * @return string |
| 284 | */ |
| 285 | function ip_address() |
| 286 | { |
| 287 | if ($this->ip_address !== FALSE) |
| 288 | { |
| 289 | return $this->ip_address; |
| 290 | } |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 291 | |
| 292 | if ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP')) |
| 293 | { |
| 294 | $this->ip_address = $_SERVER['HTTP_CLIENT_IP']; |
| 295 | } |
| 296 | elseif ($this->server('REMOTE_ADDR')) |
| 297 | { |
| 298 | $this->ip_address = $_SERVER['REMOTE_ADDR']; |
| 299 | } |
| 300 | elseif ($this->server('HTTP_CLIENT_IP')) |
| 301 | { |
| 302 | $this->ip_address = $_SERVER['HTTP_CLIENT_IP']; |
| 303 | } |
| 304 | elseif ($this->server('HTTP_X_FORWARDED_FOR')) |
| 305 | { |
| 306 | $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 307 | } |
| 308 | |
| 309 | if ($this->ip_address === FALSE) |
| 310 | { |
admin | 7099a58 | 2006-10-10 17:47:59 +0000 | [diff] [blame] | 311 | $this->ip_address = '0.0.0.0'; |
| 312 | return $this->ip_address; |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 313 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 314 | |
| 315 | if (strstr($this->ip_address, ',')) |
| 316 | { |
| 317 | $x = explode(',', $this->ip_address); |
| 318 | $this->ip_address = end($x); |
| 319 | } |
| 320 | |
| 321 | if ( ! $this->valid_ip($this->ip_address)) |
| 322 | { |
| 323 | $this->ip_address = '0.0.0.0'; |
| 324 | } |
admin | 10c3f41 | 2006-10-08 07:21:12 +0000 | [diff] [blame] | 325 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 326 | return $this->ip_address; |
| 327 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 328 | |
| 329 | // -------------------------------------------------------------------- |
| 330 | |
| 331 | /** |
| 332 | * Validate IP Address |
| 333 | * |
| 334 | * @access public |
| 335 | * @param string |
| 336 | * @return string |
| 337 | */ |
| 338 | function valid_ip($ip) |
| 339 | { |
| 340 | return ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip)) ? FALSE : TRUE; |
| 341 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 342 | |
| 343 | // -------------------------------------------------------------------- |
| 344 | |
| 345 | /** |
| 346 | * User Agent |
| 347 | * |
| 348 | * @access public |
| 349 | * @return string |
| 350 | */ |
| 351 | function user_agent() |
| 352 | { |
| 353 | if ($this->user_agent !== FALSE) |
| 354 | { |
| 355 | return $this->user_agent; |
| 356 | } |
| 357 | |
| 358 | $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT']; |
| 359 | |
| 360 | return $this->user_agent; |
| 361 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 362 | |
| 363 | // -------------------------------------------------------------------- |
| 364 | |
| 365 | /** |
| 366 | * XSS Clean |
| 367 | * |
| 368 | * Sanitizes data so that Cross Site Scripting Hacks can be |
| 369 | * prevented.Ê This function does a fair amount of work but |
| 370 | * it is extremely thorough, designed to prevent even the |
| 371 | * most obscure XSS attempts.Ê Nothing is ever 100% foolproof, |
| 372 | * of course, but I haven't been able to get anything passed |
| 373 | * the filter. |
| 374 | * |
| 375 | * Note: This function should only be used to deal with data |
| 376 | * upon submission.Ê It's not something that should |
| 377 | * be used for general runtime processing. |
| 378 | * |
| 379 | * This function was based in part on some code and ideas I |
| 380 | * got from Bitflux: http://blog.bitflux.ch/wiki/XSS_Prevention |
| 381 | * |
| 382 | * To help develop this script I used this great list of |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 383 | * vulnerabilities along with a few other hacks I've |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 384 | * harvested from examining vulnerabilities in other programs: |
| 385 | * http://ha.ckers.org/xss.html |
| 386 | * |
| 387 | * @access public |
| 388 | * @param string |
| 389 | * @return string |
| 390 | */ |
| 391 | function xss_clean($str, $charset = 'ISO-8859-1') |
| 392 | { |
| 393 | /* |
| 394 | * Remove Null Characters |
| 395 | * |
| 396 | * This prevents sandwiching null characters |
| 397 | * between ascii characters, like Java\0script. |
| 398 | * |
| 399 | */ |
| 400 | $str = preg_replace('/\0+/', '', $str); |
| 401 | $str = preg_replace('/(\\\\0)+/', '', $str); |
| 402 | |
| 403 | /* |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 404 | * Validate standard character entities |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 405 | * |
| 406 | * Add a semicolon if missing. We do this to enable |
| 407 | * the conversion of entities to ASCII later. |
| 408 | * |
| 409 | */ |
| 410 | $str = preg_replace('#(&\#*\w+)[\x00-\x20]+;#u',"\\1;",$str); |
| 411 | |
| 412 | /* |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 413 | * Validate UTF16 two byte encoding (x00) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 414 | * |
| 415 | * Just as above, adds a semicolon if missing. |
| 416 | * |
| 417 | */ |
| 418 | $str = preg_replace('#(&\#x*)([0-9A-F]+);*#iu',"\\1\\2;",$str); |
| 419 | |
| 420 | /* |
| 421 | * URL Decode |
| 422 | * |
| 423 | * Just in case stuff like this is submitted: |
| 424 | * |
| 425 | * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a> |
| 426 | * |
| 427 | * Note: Normally urldecode() would be easier but it removes plus signs |
| 428 | * |
| 429 | */ |
| 430 | $str = preg_replace("/%u0([a-z0-9]{3})/i", "&#x\\1;", $str); |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 431 | $str = preg_replace("/%([a-z0-9]{2})/i", "&#x\\1;", $str); |
| 432 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 433 | /* |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 434 | * Convert character entities to ASCII |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 435 | * |
| 436 | * This permits our tests below to work reliably. |
| 437 | * We only convert entities that are within tags since |
| 438 | * these are the ones that will pose security problems. |
| 439 | * |
| 440 | */ |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 441 | if (preg_match_all("/<(.+?)>/si", $str, $matches)) |
| 442 | { |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 443 | for ($i = 0; $i < count($matches['0']); $i++) |
| 444 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 445 | $str = str_replace($matches['1'][$i], |
| 446 | $this->_html_entity_decode($matches['1'][$i], $charset), |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 447 | $str); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Convert all tabs to spaces |
| 453 | * |
| 454 | * This prevents strings like this: ja vascript |
| 455 | * Note: we deal with spaces between characters later. |
| 456 | * |
| 457 | */ |
| 458 | $str = preg_replace("#\t+#", " ", $str); |
| 459 | |
| 460 | /* |
| 461 | * Makes PHP tags safe |
| 462 | * |
| 463 | * Note: XML tags are inadvertently replaced too: |
| 464 | * |
| 465 | * <?xml |
| 466 | * |
| 467 | * But it doesn't seem to pose a problem. |
| 468 | * |
| 469 | */ |
admin | bc042dd | 2006-09-21 02:46:59 +0000 | [diff] [blame] | 470 | $str = str_replace(array('<?php', '<?PHP', '<?', '?>'), array('<?php', '<?PHP', '<?', '?>'), $str); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 471 | |
| 472 | /* |
| 473 | * Compact any exploded words |
| 474 | * |
| 475 | * This corrects words like: j a v a s c r i p t |
| 476 | * These words are compacted back to their correct state. |
| 477 | * |
| 478 | */ |
| 479 | $words = array('javascript', 'vbscript', 'script', 'applet', 'alert', 'document', 'write', 'cookie', 'window'); |
| 480 | foreach ($words as $word) |
| 481 | { |
| 482 | $temp = ''; |
| 483 | for ($i = 0; $i < strlen($word); $i++) |
| 484 | { |
| 485 | $temp .= substr($word, $i, 1)."\s*"; |
| 486 | } |
| 487 | |
| 488 | $temp = substr($temp, 0, -3); |
| 489 | $str = preg_replace('#'.$temp.'#s', $word, $str); |
| 490 | $str = preg_replace('#'.ucfirst($temp).'#s', ucfirst($word), $str); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Remove disallowed Javascript in links or img tags |
| 495 | */ |
| 496 | $str = preg_replace("#<a.+?href=.*?(alert\(|alert&\#40;|javascript\:|window\.|document\.|\.cookie|<script|<xss).*?\>.*?</a>#si", "", $str); |
| 497 | $str = preg_replace("#<img.+?src=.*?(alert\(|alert&\#40;|javascript\:|window\.|document\.|\.cookie|<script|<xss).*?\>#si", "", $str); |
| 498 | $str = preg_replace("#<(script|xss).*?\>#si", "", $str); |
| 499 | |
| 500 | /* |
| 501 | * Remove JavaScript Event Handlers |
| 502 | * |
| 503 | * Note: This code is a little blunt. It removes |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 504 | * the event handler and anything up to the closing >, |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 505 | * but it's unlikely to be a problem. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 506 | * |
| 507 | */ |
| 508 | $str = preg_replace('#(<[^>]+.*?)(onblur|onchange|onclick|onfocus|onload|onmouseover|onmouseup|onmousedown|onselect|onsubmit|onunload|onkeypress|onkeydown|onkeyup|onresize)[^>]*>#iU',"\\1>",$str); |
| 509 | |
| 510 | /* |
| 511 | * Sanitize naughty HTML elements |
| 512 | * |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 513 | * If a tag containing any of the words in the list |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 514 | * below is found, the tag gets converted to entities. |
| 515 | * |
| 516 | * So this: <blink> |
| 517 | * Becomes: <blink> |
| 518 | * |
| 519 | */ |
| 520 | $str = preg_replace('#<(/*\s*)(alert|applet|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|layer|link|meta|object|plaintext|style|script|textarea|title|xml|xss)([^>]*)>#is', "<\\1\\2\\3>", $str); |
| 521 | |
| 522 | /* |
| 523 | * Sanitize naughty scripting elements |
| 524 | * |
| 525 | * Similar to above, only instead of looking for |
| 526 | * tags it looks for PHP and JavaScript commands |
| 527 | * that are disallowed. Rather than removing the |
| 528 | * code, it simply converts the parenthesis to entities |
admin | bd6bee7 | 2006-10-21 19:39:00 +0000 | [diff] [blame] | 529 | * rendering the code un-executable. |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 530 | * |
| 531 | * For example: eval('some code') |
| 532 | * Becomes: eval('some code') |
| 533 | * |
| 534 | */ |
| 535 | $str = preg_replace('#(alert|cmd|passthru|eval|exec|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2(\\3)", $str); |
| 536 | |
| 537 | /* |
| 538 | * Final clean up |
| 539 | * |
| 540 | * This adds a bit of extra precaution in case |
| 541 | * something got through the above filters |
| 542 | * |
| 543 | */ |
| 544 | $bad = array( |
| 545 | 'document.cookie' => '', |
| 546 | 'document.write' => '', |
| 547 | 'window.location' => '', |
| 548 | "javascript\s*:" => '', |
| 549 | "Redirect\s+302" => '', |
| 550 | '<!--' => '<!--', |
| 551 | '-->' => '-->' |
| 552 | ); |
| 553 | |
| 554 | foreach ($bad as $key => $val) |
| 555 | { |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 556 | $str = preg_replace("#".$key."#i", $val, $str); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 557 | } |
| 558 | |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 559 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 560 | log_message('debug', "XSS Filtering completed"); |
| 561 | return $str; |
| 562 | } |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 563 | |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 564 | // -------------------------------------------------------------------- |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 565 | |
| 566 | /** |
| 567 | * HTML Entities Decode |
| 568 | * |
| 569 | * This function is a replacement for html_entity_decode() |
| 570 | * |
| 571 | * In some versions of PHP the native function does not work |
| 572 | * when UTF-8 is the specified character set, so this gives us |
| 573 | * a work-around. More info here: |
| 574 | * http://bugs.php.net/bug.php?id=25670 |
| 575 | * |
| 576 | * @access private |
| 577 | * @param string |
| 578 | * @param string |
| 579 | * @return string |
| 580 | */ |
| 581 | /* ------------------------------------------------- |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 582 | /* Replacement for html_entity_decode() |
| 583 | /* -------------------------------------------------*/ |
| 584 | |
| 585 | /* |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 586 | NOTE: html_entity_decode() has a bug in some PHP versions when UTF-8 is the |
admin | 04ea44e | 2006-10-03 19:17:59 +0000 | [diff] [blame] | 587 | character set, and the PHP developers said they were not back porting the |
| 588 | fix to versions other than PHP 5.x. |
| 589 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 590 | function _html_entity_decode($str, $charset='ISO-8859-1') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 591 | { |
| 592 | if (stristr($str, '&') === FALSE) return $str; |
| 593 | |
| 594 | // The reason we are not using html_entity_decode() by itself is because |
| 595 | // while it is not technically correct to leave out the semicolon |
| 596 | // at the end of an entity most browsers will still interpret the entity |
| 597 | // correctly. html_entity_decode() does not convert entities without |
| 598 | // semicolons, so we are left with our own little solution here. Bummer. |
| 599 | |
| 600 | if (function_exists('html_entity_decode') && (strtolower($charset) != 'utf-8' OR version_compare(phpversion(), '5.0.0', '>='))) |
| 601 | { |
| 602 | $str = html_entity_decode($str, ENT_COMPAT, $charset); |
| 603 | $str = preg_replace('~&#x([0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str); |
| 604 | return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str); |
| 605 | } |
| 606 | |
| 607 | // Numeric Entities |
| 608 | $str = preg_replace('~&#x([0-9a-f]{2,5});{0,1}~ei', 'chr(hexdec("\\1"))', $str); |
| 609 | $str = preg_replace('~&#([0-9]{2,4});{0,1}~e', 'chr(\\1)', $str); |
| 610 | |
| 611 | // Literal Entities - Slightly slow so we do another check |
| 612 | if (stristr($str, '&') === FALSE) |
| 613 | { |
| 614 | $str = strtr($str, array_flip(get_html_translation_table(HTML_ENTITIES))); |
| 615 | } |
| 616 | |
| 617 | return $str; |
| 618 | } |
| 619 | |
| 620 | } |
| 621 | // END Input class |
| 622 | ?> |