Input.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Input Class
00020  *
00021  * Pre-processes global input data for security
00022  *
00023  * @package             CodeIgniter
00024  * @subpackage  Libraries
00025  * @category    Input
00026  * @author              ExpressionEngine Dev Team
00027  * @link                http://codeigniter.com/user_guide/libraries/input.html
00028  */
00029 class CI_Input {
00030         var $use_xss_clean              = FALSE;
00031         var $xss_hash                   = '';
00032         var $ip_address                 = FALSE;
00033         var $user_agent                 = FALSE;
00034         var $allow_get_array    = FALSE;
00035         
00036         /* never allowed, string replacement */
00037         var $never_allowed_str = array(
00038                                                                         'document.cookie'       => '[removed]',
00039                                                                         'document.write'        => '[removed]',
00040                                                                         '.parentNode'           => '[removed]',
00041                                                                         '.innerHTML'            => '[removed]',
00042                                                                         'window.location'       => '[removed]',
00043                                                                         '-moz-binding'          => '[removed]',
00044                                                                         '<!--'                          => '&lt;!--',
00045                                                                         '-->'                           => '--&gt;',
00046                                                                         '<![CDATA['                     => '&lt;![CDATA['
00047                                                                         );
00048         /* never allowed, regex replacement */
00049         var $never_allowed_regex = array(
00050                                                                                 "javascript\s*:"        => '[removed]',
00051                                                                                 "expression\s*\("       => '[removed]', // CSS and IE
00052                                                                                 "Redirect\s+302"        => '[removed]'
00053                                                                         );
00054                                 
00055         /**
00056          * Constructor
00057          *
00058          * Sets whether to globally enable the XSS processing
00059          * and whether to allow the $_GET array
00060          *
00061          * @access      public
00062          */
00063         function CI_Input()
00064         {
00065                 log_message('debug', "Input Class Initialized");
00066 
00067                 $CFG =& load_class('Config');
00068                 $this->use_xss_clean    = ($CFG->item('global_xss_filtering') === TRUE) ? TRUE : FALSE;
00069                 $this->allow_get_array  = ($CFG->item('enable_query_strings') === TRUE) ? TRUE : FALSE;
00070                 $this->_sanitize_globals();
00071         }
00072 
00073         // --------------------------------------------------------------------
00074 
00075         /**
00076          * Sanitize Globals
00077          *
00078          * This function does the following:
00079          *
00080          * Unsets $_GET data (if query strings are not enabled)
00081          *
00082          * Unsets all globals if register_globals is enabled
00083          *
00084          * Standardizes newline characters to \n
00085          *
00086          * @access      private
00087          * @return      void
00088          */
00089         function _sanitize_globals()
00090         {
00091                 // Would kind of be "wrong" to unset any of these GLOBALS
00092                 $protected = array('_SERVER', '_GET', '_POST', '_FILES', '_REQUEST', '_SESSION', '_ENV', 'GLOBALS', 'HTTP_RAW_POST_DATA',
00093                                                         'system_folder', 'application_folder', 'BM', 'EXT', 'CFG', 'URI', 'RTR', 'OUT', 'IN');
00094 
00095                 // Unset globals for security. 
00096                 // This is effectively the same as register_globals = off
00097                 foreach (array($_GET, $_POST, $_COOKIE, $_SERVER, $_FILES, $_ENV, (isset($_SESSION) && is_array($_SESSION)) ? $_SESSION : array()) as $global)
00098                 {
00099                         if ( ! is_array($global))
00100                         {
00101                                 if ( ! in_array($global, $protected))
00102                                 {
00103                                         unset($GLOBALS[$global]);
00104                                 }
00105                         }
00106                         else
00107                         {
00108                                 foreach ($global as $key => $val)
00109                                 {
00110                                         if ( ! in_array($key, $protected))
00111                                         {
00112                                                 unset($GLOBALS[$key]);
00113                                         }
00114                         
00115                                         if (is_array($val))
00116                                         {
00117                                                 foreach($val as $k => $v)
00118                                                 {
00119                                                         if ( ! in_array($k, $protected))
00120                                                         {
00121                                                                 unset($GLOBALS[$k]);
00122                                                         }
00123                                                 }
00124                                         }
00125                                 }
00126                         }
00127                 }
00128 
00129                 // Is $_GET data allowed? If not we'll set the $_GET to an empty array
00130                 if ($this->allow_get_array == FALSE)
00131                 {
00132                         $_GET = array();
00133                 }
00134                 else
00135                 {
00136                         $_GET = $this->_clean_input_data($_GET);
00137                 }
00138 
00139                 // Clean $_POST Data
00140                 $_POST = $this->_clean_input_data($_POST);
00141                 
00142                 // Clean $_COOKIE Data
00143                 // Also get rid of specially treated cookies that might be set by a server
00144                 // or silly application, that are of no use to a CI application anyway
00145                 // but that when present will trip our 'Disallowed Key Characters' alarm
00146                 // http://www.ietf.org/rfc/rfc2109.txt
00147                 // note that the key names below are single quoted strings, and are not PHP variables
00148                 unset($_COOKIE['$Version']);
00149                 unset($_COOKIE['$Path']);
00150                 unset($_COOKIE['$Domain']);
00151                 $_COOKIE = $this->_clean_input_data($_COOKIE);
00152 
00153                 log_message('debug', "Global POST and COOKIE data sanitized");
00154         }
00155 
00156         // --------------------------------------------------------------------
00157 
00158         /**
00159          * Clean Input Data
00160          *
00161          * This is a helper function. It escapes data and
00162          * standardizes newline characters to \n
00163          *
00164          * @access      private
00165          * @param       string
00166          * @return      string
00167          */
00168         function _clean_input_data($str)
00169         {
00170                 if (is_array($str))
00171                 {
00172                         $new_array = array();
00173                         foreach ($str as $key => $val)
00174                         {
00175                                 $new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
00176                         }
00177                         return $new_array;
00178                 }
00179 
00180                 // We strip slashes if magic quotes is on to keep things consistent
00181                 if (get_magic_quotes_gpc())
00182                 {
00183                         $str = stripslashes($str);
00184                 }
00185 
00186                 // Should we filter the input data?
00187                 if ($this->use_xss_clean === TRUE)
00188                 {
00189                         $str = $this->xss_clean($str);
00190                 }
00191 
00192                 // Standardize newlines
00193                 if (strpos($str, "\r") !== FALSE)
00194                 {
00195                         $str = str_replace(array("\r\n", "\r"), "\n", $str);
00196                 }
00197                 
00198                 return $str;
00199         }
00200 
00201         // --------------------------------------------------------------------
00202 
00203         /**
00204          * Clean Keys
00205          *
00206          * This is a helper function. To prevent malicious users
00207          * from trying to exploit keys we make sure that keys are
00208          * only named with alpha-numeric text and a few other items.
00209          *
00210          * @access      private
00211          * @param       string
00212          * @return      string
00213          */
00214         function _clean_input_keys($str)
00215         {
00216                  if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))
00217                  {
00218                         exit('Disallowed Key Characters.');
00219                  }
00220 
00221                 return $str;
00222         }
00223 
00224         // --------------------------------------------------------------------
00225         
00226         /**
00227          * Fetch from array
00228          *
00229          * This is a helper function to retrieve values from global arrays
00230          *
00231          * @access      private
00232          * @param       array
00233          * @param       string
00234          * @param       bool
00235          * @return      string
00236          */
00237         function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
00238         {
00239                 if ( ! isset($array[$index]))
00240                 {
00241                         return FALSE;
00242                 }
00243 
00244                 if ($xss_clean === TRUE)
00245                 {
00246                         return $this->xss_clean($array[$index]);
00247                 }
00248 
00249                 return $array[$index];
00250         }
00251 
00252         // --------------------------------------------------------------------
00253         
00254         /**
00255          * Fetch an item from the GET array
00256          *
00257          * @access      public
00258          * @param       string
00259          * @param       bool
00260          * @return      string
00261          */
00262         function get($index = '', $xss_clean = FALSE)
00263         {
00264                 return $this->_fetch_from_array($_GET, $index, $xss_clean);
00265         }
00266 
00267         // --------------------------------------------------------------------
00268 
00269         /**
00270          * Fetch an item from the POST array
00271          *
00272          * @access      public
00273          * @param       string
00274          * @param       bool
00275          * @return      string
00276          */
00277         function post($index = '', $xss_clean = FALSE)
00278         {
00279                 return $this->_fetch_from_array($_POST, $index, $xss_clean);
00280         }
00281 
00282         // --------------------------------------------------------------------
00283 
00284         /**
00285          * Fetch an item from either the GET array or the POST
00286          *
00287          * @access      public
00288          * @param       string  The index key
00289          * @param       bool    XSS cleaning
00290          * @return      string
00291          */
00292         function get_post($index = '', $xss_clean = FALSE)
00293         {               
00294                 if ( ! isset($_POST[$index]) )
00295                 {
00296                         return $this->get($index, $xss_clean);
00297                 }
00298                 else
00299                 {
00300                         return $this->post($index, $xss_clean);
00301                 }               
00302         }
00303 
00304         // --------------------------------------------------------------------
00305 
00306         /**
00307          * Fetch an item from the COOKIE array
00308          *
00309          * @access      public
00310          * @param       string
00311          * @param       bool
00312          * @return      string
00313          */
00314         function cookie($index = '', $xss_clean = FALSE)
00315         {
00316                 return $this->_fetch_from_array($_COOKIE, $index, $xss_clean);
00317         }
00318 
00319         // --------------------------------------------------------------------
00320 
00321         /**
00322          * Fetch an item from the SERVER array
00323          *
00324          * @access      public
00325          * @param       string
00326          * @param       bool
00327          * @return      string
00328          */
00329         function server($index = '', $xss_clean = FALSE)
00330         {
00331                 return $this->_fetch_from_array($_SERVER, $index, $xss_clean);
00332         }
00333 
00334         // --------------------------------------------------------------------
00335 
00336         /**
00337          * Fetch the IP Address
00338          *
00339          * @access      public
00340          * @return      string
00341          */
00342         function ip_address()
00343         {
00344                 if ($this->ip_address !== FALSE)
00345                 {
00346                         return $this->ip_address;
00347                 }
00348 
00349                 if ($this->server('REMOTE_ADDR') AND $this->server('HTTP_CLIENT_IP'))
00350                 {
00351                          $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
00352                 }
00353                 elseif ($this->server('REMOTE_ADDR'))
00354                 {
00355                          $this->ip_address = $_SERVER['REMOTE_ADDR'];
00356                 }
00357                 elseif ($this->server('HTTP_CLIENT_IP'))
00358                 {
00359                          $this->ip_address = $_SERVER['HTTP_CLIENT_IP'];
00360                 }
00361                 elseif ($this->server('HTTP_X_FORWARDED_FOR'))
00362                 {
00363                          $this->ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
00364                 }
00365 
00366                 if ($this->ip_address === FALSE)
00367                 {
00368                         $this->ip_address = '0.0.0.0';
00369                         return $this->ip_address;
00370                 }
00371 
00372                 if (strstr($this->ip_address, ','))
00373                 {
00374                         $x = explode(',', $this->ip_address);
00375                         $this->ip_address = end($x);
00376                 }
00377 
00378                 if ( ! $this->valid_ip($this->ip_address))
00379                 {
00380                         $this->ip_address = '0.0.0.0';
00381                 }
00382                 
00383                 return $this->ip_address;
00384         }
00385 
00386         // --------------------------------------------------------------------
00387 
00388         /**
00389          * Validate IP Address
00390          *
00391          * Updated version suggested by Geert De Deckere
00392          * 
00393          * @access      public
00394          * @param       string
00395          * @return      string
00396          */
00397         function valid_ip($ip)
00398         {
00399                 $ip_segments = explode('.', $ip);
00400 
00401                 // Always 4 segments needed
00402                 if (count($ip_segments) != 4)
00403                 {
00404                         return FALSE;
00405                 }
00406                 // IP can not start with 0
00407                 if ($ip_segments[0][0] == '0')
00408                 {
00409                         return FALSE;
00410                 }
00411                 // Check each segment
00412                 foreach ($ip_segments as $segment)
00413                 {
00414                         // IP segments must be digits and can not be 
00415                         // longer than 3 digits or greater then 255
00416                         if ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
00417                         {
00418                                 return FALSE;
00419                         }
00420                 }
00421 
00422                 return TRUE;
00423         }
00424 
00425         // --------------------------------------------------------------------
00426 
00427         /**
00428          * User Agent
00429          *
00430          * @access      public
00431          * @return      string
00432          */
00433         function user_agent()
00434         {
00435                 if ($this->user_agent !== FALSE)
00436                 {
00437                         return $this->user_agent;
00438                 }
00439 
00440                 $this->user_agent = ( ! isset($_SERVER['HTTP_USER_AGENT'])) ? FALSE : $_SERVER['HTTP_USER_AGENT'];
00441 
00442                 return $this->user_agent;
00443         }
00444 
00445         // --------------------------------------------------------------------
00446 
00447         /**
00448          * Filename Security
00449          *
00450          * @access      public
00451          * @param       string
00452          * @return      string
00453          */
00454         function filename_security($str)
00455         {
00456                 $bad = array(
00457                                                 "../",
00458                                                 "./",
00459                                                 "<!--",
00460                                                 "-->",
00461                                                 "<",
00462                                                 ">",
00463                                                 "'",
00464                                                 '"',
00465                                                 '&',
00466                                                 '$',
00467                                                 '#',
00468                                                 '{',
00469                                                 '}',
00470                                                 '[',
00471                                                 ']',
00472                                                 '=',
00473                                                 ';',
00474                                                 '?',
00475                                                 "%20",
00476                                                 "%22",
00477                                                 "%3c",          // <
00478                                                 "%253c",        // <
00479                                                 "%3e",          // >
00480                                                 "%0e",          // >
00481                                                 "%28",          // (  
00482                                                 "%29",          // ) 
00483                                                 "%2528",        // (
00484                                                 "%26",          // &
00485                                                 "%24",          // $
00486                                                 "%3f",          // ?
00487                                                 "%3b",          // ;
00488                                                 "%3d"           // =
00489                                         );
00490 
00491                 return stripslashes(str_replace($bad, '', $str));
00492         }
00493 
00494         // --------------------------------------------------------------------
00495 
00496         /**
00497          * XSS Clean
00498          *
00499          * Sanitizes data so that Cross Site Scripting Hacks can be
00500          * prevented.  This function does a fair amount of work but
00501          * it is extremely thorough, designed to prevent even the
00502          * most obscure XSS attempts.  Nothing is ever 100% foolproof,
00503          * of course, but I haven't been able to get anything passed
00504          * the filter.
00505          *
00506          * Note: This function should only be used to deal with data
00507          * upon submission.  It's not something that should
00508          * be used for general runtime processing.
00509          *
00510          * This function was based in part on some code and ideas I
00511          * got from Bitflux: http://blog.bitflux.ch/wiki/XSS_Prevention
00512          *
00513          * To help develop this script I used this great list of
00514          * vulnerabilities along with a few other hacks I've
00515          * harvested from examining vulnerabilities in other programs:
00516          * http://ha.ckers.org/xss.html
00517          *
00518          * @access      public
00519          * @param       string
00520          * @return      string
00521          */
00522         function xss_clean($str, $is_image = FALSE)
00523         {
00524                 /*
00525                  * Is the string an array?
00526                  *
00527                  */
00528                 if (is_array($str))
00529                 {
00530                         while (list($key) = each($str))
00531                         {
00532                                 $str[$key] = $this->xss_clean($str[$key]);
00533                         }
00534         
00535                         return $str;
00536                 }
00537 
00538                 /*
00539                  * Remove Invisible Characters
00540                  */
00541                 $str = $this->_remove_invisible_characters($str);
00542 
00543                 /*
00544                  * Protect GET variables in URLs
00545                  */
00546                  
00547                  // 901119URL5918AMP18930PROTECT8198
00548                  
00549                 $str = preg_replace('|\&([a-z\_0-9]+)\=([a-z\_0-9]+)|i', $this->xss_hash()."\\1=\\2", $str);
00550 
00551                 /*
00552                  * Validate standard character entities
00553                  *
00554                  * Add a semicolon if missing.  We do this to enable
00555                  * the conversion of entities to ASCII later.
00556                  *
00557                  */
00558                 $str = preg_replace('#(&\#?[0-9a-z]{2,})[\x00-\x20]*;?#i', "\\1;", $str);
00559 
00560                 /*
00561                  * Validate UTF16 two byte encoding (x00) 
00562                  *
00563                  * Just as above, adds a semicolon if missing.
00564                  *
00565                  */
00566                 $str = preg_replace('#(&\#x?)([0-9A-F]+);?#i',"\\1\\2;",$str);
00567 
00568                 /*
00569                  * Un-Protect GET variables in URLs
00570                  */
00571                 $str = str_replace($this->xss_hash(), '&', $str);
00572 
00573                 /*
00574                  * URL Decode
00575                  *
00576                  * Just in case stuff like this is submitted:
00577                  *
00578                  * <a href="http://%77%77%77%2E%67%6F%6F%67%6C%65%2E%63%6F%6D">Google</a>
00579                  *
00580                  * Note: Use rawurldecode() so it does not remove plus signs
00581                  *
00582                  */
00583                 $str = rawurldecode($str);
00584         
00585                 /*
00586                  * Convert character entities to ASCII 
00587                  *
00588                  * This permits our tests below to work reliably.
00589                  * We only convert entities that are within tags since
00590                  * these are the ones that will pose security problems.
00591                  *
00592                  */
00593 
00594                 $str = preg_replace_callback("/[a-z]+=([\'\"]).*?\\1/si", array($this, '_convert_attribute'), $str);
00595          
00596                 $str = preg_replace_callback("/<\w+.*?(?=>|<|$)/si", array($this, '_html_entity_decode_callback'), $str);
00597 
00598                 /*
00599                  * Remove Invisible Characters Again!
00600                  */
00601                 $str = $this->_remove_invisible_characters($str);
00602                 
00603                 /*
00604                  * Convert all tabs to spaces
00605                  *
00606                  * This prevents strings like this: ja  vascript
00607                  * NOTE: we deal with spaces between characters later.
00608                  * NOTE: preg_replace was found to be amazingly slow here on large blocks of data,
00609                  * so we use str_replace.
00610                  *
00611                  */
00612                 
00613                 if (strpos($str, "\t") !== FALSE)
00614                 {
00615                         $str = str_replace("\t", ' ', $str);
00616                 }
00617                 
00618                 /*
00619                  * Capture converted string for later comparison
00620                  */
00621                 $converted_string = $str;
00622                 
00623                 /*
00624                  * Not Allowed Under Any Conditions
00625                  */
00626                 
00627                 foreach ($this->never_allowed_str as $key => $val)
00628                 {
00629                         $str = str_replace($key, $val, $str);   
00630                 }
00631         
00632                 foreach ($this->never_allowed_regex as $key => $val)
00633                 {
00634                         $str = preg_replace("#".$key."#i", $val, $str);   
00635                 }
00636 
00637                 /*
00638                  * Makes PHP tags safe
00639                  *
00640                  *  Note: XML tags are inadvertently replaced too:
00641                  *
00642                  *      <?xml
00643                  *
00644                  * But it doesn't seem to pose a problem.
00645                  *
00646                  */
00647                 if ($is_image === TRUE)
00648                 {
00649                         // Images have a tendency to have the PHP short opening and closing tags every so often
00650                         // so we skip those and only do the long opening tags.
00651                         $str = str_replace(array('<?php', '<?PHP'),  array('&lt;?php', '&lt;?PHP'), $str);
00652                 }
00653                 else
00654                 {
00655                         $str = str_replace(array('<?php', '<?PHP', '<?', '?'.'>'),  array('&lt;?php', '&lt;?PHP', '&lt;?', '?&gt;'), $str);
00656                 }
00657                 
00658                 /*
00659                  * Compact any exploded words
00660                  *
00661                  * This corrects words like:  j a v a s c r i p t
00662                  * These words are compacted back to their correct state.
00663                  *
00664                  */
00665                 $words = array('javascript', 'expression', 'vbscript', 'script', 'applet', 'alert', 'document', 'write', 'cookie', 'window');
00666                 foreach ($words as $word)
00667                 {
00668                         $temp = '';
00669                         
00670                         for ($i = 0, $wordlen = strlen($word); $i < $wordlen; $i++)
00671                         {
00672                                 $temp .= substr($word, $i, 1)."\s*";
00673                         }
00674 
00675                         // We only want to do this when it is followed by a non-word character
00676                         // That way valid stuff like "dealer to" does not become "dealerto"
00677                         $str = preg_replace_callback('#('.substr($temp, 0, -3).')(\W)#is', array($this, '_compact_exploded_words'), $str);
00678                 }
00679                 
00680                 /*
00681                  * Remove disallowed Javascript in links or img tags
00682                  * We used to do some version comparisons and use of stripos for PHP5, but it is dog slow compared
00683                  * to these simplified non-capturing preg_match(), especially if the pattern exists in the string
00684                  */
00685                 do
00686                 {
00687                         $original = $str;
00688         
00689                         if (preg_match("/<a/i", $str))
00690                         {
00691                                 $str = preg_replace_callback("#<a\s+([^>]*?)(>|$)#si", array($this, '_js_link_removal'), $str);
00692                         }
00693         
00694                         if (preg_match("/<img/i", $str))
00695                         {
00696                                 $str = preg_replace_callback("#<img\s+([^>]*?)(\s?/?>|$)#si", array($this, '_js_img_removal'), $str);
00697                         }
00698         
00699                         if (preg_match("/script/i", $str) OR preg_match("/xss/i", $str))
00700                         {
00701                                 $str = preg_replace("#<(/*)(script|xss)(.*?)>#si", '[removed]', $str);
00702                         }
00703                 }
00704                 while($original != $str);
00705 
00706                 unset($original);
00707 
00708                 /*
00709                  * Remove JavaScript Event Handlers
00710                  *
00711                  * Note: This code is a little blunt.  It removes
00712                  * the event handler and anything up to the closing >,
00713                  * but it's unlikely to be a problem.
00714                  *
00715                  */
00716                 $event_handlers = array('[^a-z_\-]on\w*','xmlns');
00717 
00718                 if ($is_image === TRUE)
00719                 {
00720                         /*
00721                          * Adobe Photoshop puts XML metadata into JFIF images, including namespacing, 
00722                          * so we have to allow this for images. -Paul
00723                          */
00724                         unset($event_handlers[array_search('xmlns', $event_handlers)]);
00725                 }
00726 
00727                 $str = preg_replace("#<([^><]+?)(".implode('|', $event_handlers).")(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str);
00728 
00729                 /*
00730                  * Sanitize naughty HTML elements
00731                  *
00732                  * If a tag containing any of the words in the list
00733                  * below is found, the tag gets converted to entities.
00734                  *
00735                  * So this: <blink>
00736                  * Becomes: &lt;blink&gt;
00737                  *
00738                  */
00739                 $naughty = 'alert|applet|audio|basefont|base|behavior|bgsound|blink|body|embed|expression|form|frameset|frame|head|html|ilayer|iframe|input|isindex|layer|link|meta|object|plaintext|style|script|textarea|title|video|xml|xss';
00740                 $str = preg_replace_callback('#<(/*\s*)('.$naughty.')([^><]*)([><]*)#is', array($this, '_sanitize_naughty_html'), $str);
00741 
00742                 /*
00743                  * Sanitize naughty scripting elements
00744                  *
00745                  * Similar to above, only instead of looking for
00746                  * tags it looks for PHP and JavaScript commands
00747                  * that are disallowed.  Rather than removing the
00748                  * code, it simply converts the parenthesis to entities
00749                  * rendering the code un-executable.
00750                  *
00751                  * For example: eval('some code')
00752                  * Becomes:             eval&#40;'some code'&#41;
00753                  *
00754                  */
00755                 $str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2&#40;\\3&#41;", $str);
00756                                         
00757                 /*
00758                  * Final clean up
00759                  *
00760                  * This adds a bit of extra precaution in case
00761                  * something got through the above filters
00762                  *
00763                  */
00764                 foreach ($this->never_allowed_str as $key => $val)
00765                 {
00766                         $str = str_replace($key, $val, $str);   
00767                 }
00768         
00769                 foreach ($this->never_allowed_regex as $key => $val)
00770                 {
00771                         $str = preg_replace("#".$key."#i", $val, $str);
00772                 }
00773 
00774                 /*
00775                  *  Images are Handled in a Special Way
00776                  *  - Essentially, we want to know that after all of the character conversion is done whether
00777                  *  any unwanted, likely XSS, code was found.  If not, we return TRUE, as the image is clean.
00778                  *  However, if the string post-conversion does not matched the string post-removal of XSS,
00779                  *  then it fails, as there was unwanted XSS code found and removed/changed during processing.
00780                  */
00781 
00782                 if ($is_image === TRUE)
00783                 {
00784                         if ($str == $converted_string)
00785                         {
00786                                 return TRUE;
00787                         }
00788                         else
00789                         {
00790                                 return FALSE;
00791                         }
00792                 }
00793                 
00794                 log_message('debug', "XSS Filtering completed");
00795                 return $str;
00796         }
00797 
00798         // --------------------------------------------------------------------
00799 
00800         /**
00801          * Random Hash for protecting URLs
00802          *
00803          * @access      public
00804          * @return      string
00805          */
00806         function xss_hash()
00807         {
00808                 if ($this->xss_hash == '')
00809                 {
00810                         if (phpversion() >= 4.2)
00811                                 mt_srand();
00812                         else
00813                                 mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
00814 
00815                         $this->xss_hash = md5(time() + mt_rand(0, 1999999999));
00816                 }
00817 
00818                 return $this->xss_hash;
00819         }
00820 
00821         // --------------------------------------------------------------------
00822         
00823         /**
00824          * Remove Invisible Characters
00825          *
00826          * This prevents sandwiching null characters
00827          * between ascii characters, like Java\0script.
00828          *
00829          * @access      public
00830          * @param       string
00831          * @return      string
00832          */
00833         function _remove_invisible_characters($str)
00834         {
00835                 static $non_displayables;
00836                 
00837                 if ( ! isset($non_displayables))
00838                 {
00839                         // every control character except newline (dec 10), carriage return (dec 13), and horizontal tab (dec 09),
00840                         $non_displayables = array(
00841                                                                                 '/%0[0-8bcef]/',                        // url encoded 00-08, 11, 12, 14, 15
00842                                                                                 '/%1[0-9a-f]/',                         // url encoded 16-31
00843                                                                                 '/[\x00-\x08]/',                        // 00-08
00844                                                                                 '/\x0b/', '/\x0c/',                     // 11, 12
00845                                                                                 '/[\x0e-\x1f]/'                         // 14-31
00846                                                                         );
00847                 }
00848 
00849                 do
00850                 {
00851                         $cleaned = $str;
00852                         $str = preg_replace($non_displayables, '', $str);
00853                 }
00854                 while ($cleaned != $str);
00855 
00856                 return $str;
00857         }
00858 
00859         // --------------------------------------------------------------------
00860         
00861         /**
00862          * Compact Exploded Words
00863          *
00864          * Callback function for xss_clean() to remove whitespace from
00865          * things like j a v a s c r i p t
00866          *
00867          * @access      public
00868          * @param       type
00869          * @return      type
00870          */
00871         function _compact_exploded_words($matches)
00872         {
00873                 return preg_replace('/\s+/s', '', $matches[1]).$matches[2];
00874         }
00875 
00876         // --------------------------------------------------------------------
00877         
00878         /**
00879          * Sanitize Naughty HTML
00880          *
00881          * Callback function for xss_clean() to remove naughty HTML elements
00882          *
00883          * @access      private
00884          * @param       array
00885          * @return      string
00886          */
00887         function _sanitize_naughty_html($matches)
00888         {
00889                 // encode opening brace
00890                 $str = '&lt;'.$matches[1].$matches[2].$matches[3];
00891                 
00892                 // encode captured opening or closing brace to prevent recursive vectors
00893                 $str .= str_replace(array('>', '<'), array('&gt;', '&lt;'), $matches[4]);
00894                 
00895                 return $str;
00896         }
00897 
00898         // --------------------------------------------------------------------
00899         
00900         /**
00901          * JS Link Removal
00902          *
00903          * Callback function for xss_clean() to sanitize links
00904          * This limits the PCRE backtracks, making it more performance friendly
00905          * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
00906          * PHP 5.2+ on link-heavy strings
00907          *
00908          * @access      private
00909          * @param       array
00910          * @return      string
00911          */
00912         function _js_link_removal($match)
00913         {
00914                 $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]));
00915                 return str_replace($match[1], preg_replace("#href=.*?(alert\(|alert&\#40;|javascript\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]);
00916         }
00917 
00918         /**
00919          * JS Image Removal
00920          *
00921          * Callback function for xss_clean() to sanitize image tags
00922          * This limits the PCRE backtracks, making it more performance friendly
00923          * and prevents PREG_BACKTRACK_LIMIT_ERROR from being triggered in
00924          * PHP 5.2+ on image tag heavy strings
00925          *
00926          * @access      private
00927          * @param       array
00928          * @return      string
00929          */
00930         function _js_img_removal($match)
00931         {
00932                 $attributes = $this->_filter_attributes(str_replace(array('<', '>'), '', $match[1]));
00933                 return str_replace($match[1], preg_replace("#src=.*?(alert\(|alert&\#40;|javascript\:|charset\=|window\.|document\.|\.cookie|<script|<xss|base64\s*,)#si", "", $attributes), $match[0]);
00934         }
00935 
00936         // --------------------------------------------------------------------
00937 
00938         /**
00939          * Attribute Conversion
00940          *
00941          * Used as a callback for XSS Clean
00942          *
00943          * @access      public
00944          * @param       array
00945          * @return      string
00946          */
00947         function _convert_attribute($match)
00948         {
00949                 return str_replace(array('>', '<'), array('&gt;', '&lt;'), $match[0]);
00950         }
00951 
00952         // --------------------------------------------------------------------
00953 
00954         /**
00955          * HTML Entity Decode Callback
00956          *
00957          * Used as a callback for XSS Clean
00958          *
00959          * @access      public
00960          * @param       array
00961          * @return      string
00962          */
00963         function _html_entity_decode_callback($match)
00964         {
00965                 $CFG =& load_class('Config');
00966                 $charset = $CFG->item('charset');
00967 
00968                 return $this->_html_entity_decode($match[0], strtoupper($charset));
00969         }
00970 
00971         // --------------------------------------------------------------------
00972 
00973         /**
00974          * HTML Entities Decode
00975          *
00976          * This function is a replacement for html_entity_decode()
00977          *
00978          * In some versions of PHP the native function does not work
00979          * when UTF-8 is the specified character set, so this gives us
00980          * a work-around.  More info here:
00981          * http://bugs.php.net/bug.php?id=25670
00982          *
00983          * @access      private
00984          * @param       string
00985          * @param       string
00986          * @return      string
00987          */
00988         /* -------------------------------------------------
00989         /*  Replacement for html_entity_decode()
00990         /* -------------------------------------------------*/
00991 
00992         /*
00993         NOTE: html_entity_decode() has a bug in some PHP versions when UTF-8 is the
00994         character set, and the PHP developers said they were not back porting the
00995         fix to versions other than PHP 5.x.
00996         */
00997         function _html_entity_decode($str, $charset='UTF-8')
00998         {
00999                 if (stristr($str, '&') === FALSE) return $str;
01000 
01001                 // The reason we are not using html_entity_decode() by itself is because
01002                 // while it is not technically correct to leave out the semicolon
01003                 // at the end of an entity most browsers will still interpret the entity
01004                 // correctly.  html_entity_decode() does not convert entities without
01005                 // semicolons, so we are left with our own little solution here. Bummer.
01006 
01007                 if (function_exists('html_entity_decode') && (strtolower($charset) != 'utf-8' OR version_compare(phpversion(), '5.0.0', '>=')))
01008                 {
01009                         $str = html_entity_decode($str, ENT_COMPAT, $charset);
01010                         $str = preg_replace('~&#x(0*[0-9a-f]{2,5})~ei', 'chr(hexdec("\\1"))', $str);
01011                         return preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $str);
01012                 }
01013 
01014                 // Numeric Entities
01015                 $str = preg_replace('~&#x(0*[0-9a-f]{2,5});{0,1}~ei', 'chr(hexdec("\\1"))', $str);
01016                 $str = preg_replace('~&#([0-9]{2,4});{0,1}~e', 'chr(\\1)', $str);
01017 
01018                 // Literal Entities - Slightly slow so we do another check
01019                 if (stristr($str, '&') === FALSE)
01020                 {
01021                         $str = strtr($str, array_flip(get_html_translation_table(HTML_ENTITIES)));
01022                 }
01023 
01024                 return $str;
01025         }
01026 
01027         // --------------------------------------------------------------------
01028         
01029         /**
01030          * Filter Attributes
01031          *
01032          * Filters tag attributes for consistency and safety
01033          *
01034          * @access      public
01035          * @param       string
01036          * @return      string
01037          */
01038         function _filter_attributes($str)
01039         {
01040                 $out = '';
01041 
01042                 if (preg_match_all('#\s*[a-z\-]+\s*=\s*(\042|\047)([^\\1]*?)\\1#is', $str, $matches))
01043                 {
01044                         foreach ($matches[0] as $match)
01045                         {
01046                                 $out .= "{$match}";
01047                         }                       
01048                 }
01049 
01050                 return $out;
01051         }
01052 
01053         // --------------------------------------------------------------------
01054 
01055 }
01056 // END Input class
01057 
01058 /* End of file Input.php */
01059 /* Location: ./system/libraries/Input.php */