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