Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############## |
| 2 | Security Class |
| 3 | ############## |
| 4 | |
| 5 | The Security Class contains methods that help you create a secure |
| 6 | application, processing input data for security. |
| 7 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 8 | .. contents:: |
| 9 | :local: |
| 10 | |
| 11 | .. raw:: html |
| 12 | |
| 13 | <div class="custom-index container"></div> |
| 14 | |
James L Parry | 08191be | 2014-12-20 02:37:13 -0800 | [diff] [blame] | 15 | ************* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 16 | XSS Filtering |
James L Parry | 08191be | 2014-12-20 02:37:13 -0800 | [diff] [blame] | 17 | ************* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | |
| 19 | CodeIgniter comes with a Cross Site Scripting Hack prevention filter |
| 20 | which can either run automatically to filter all POST and COOKIE data |
| 21 | that is encountered, or you can run it on a per item basis. By default |
| 22 | it does **not** run globally since it requires a bit of processing |
| 23 | overhead, and since you may not need it in all cases. |
| 24 | |
| 25 | The XSS filter looks for commonly used techniques to trigger Javascript |
| 26 | or other types of code that attempt to hijack cookies or do other |
| 27 | malicious things. If anything disallowed is encountered it is rendered |
| 28 | safe by converting the data to character entities. |
| 29 | |
| 30 | Note: This function should only be used to deal with data upon |
| 31 | submission. It's not something that should be used for general runtime |
| 32 | processing since it requires a fair amount of processing overhead. |
| 33 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 34 | To filter data through the XSS filter use the ``xss_clean()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
| 36 | $data = $this->security->xss_clean($data); |
| 37 | |
| 38 | If you want the filter to run automatically every time it encounters |
| 39 | POST or COOKIE data you can enable it by opening your |
| 40 | application/config/config.php file and setting this:: |
| 41 | |
| 42 | $config['global_xss_filtering'] = TRUE; |
| 43 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 44 | .. note:: If you use the form validation class, it gives you the option of |
| 45 | XSS filtering as well. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 47 | An optional second parameter, *is_image*, allows this function to be used |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | to test images for potential XSS attacks, useful for file upload |
| 49 | security. When this second parameter is set to TRUE, instead of |
| 50 | returning an altered string, the function returns TRUE if the image is |
| 51 | safe, and FALSE if it contained potentially malicious information that a |
| 52 | browser may attempt to execute. |
| 53 | |
| 54 | :: |
| 55 | |
Derek Jones | eb946d0 | 2011-10-05 15:47:43 -0500 | [diff] [blame] | 56 | if ($this->security->xss_clean($file, TRUE) === FALSE) |
| 57 | { |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 58 | // file failed the XSS test |
Derek Jones | eb946d0 | 2011-10-05 15:47:43 -0500 | [diff] [blame] | 59 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | |
James L Parry | 08191be | 2014-12-20 02:37:13 -0800 | [diff] [blame] | 61 | ********************************* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | Cross-site request forgery (CSRF) |
James L Parry | 08191be | 2014-12-20 02:37:13 -0800 | [diff] [blame] | 63 | ********************************* |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 65 | You can enable CSRF protection by altering your **application/config/config.php** |
| 66 | file in the following way:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 67 | |
| 68 | $config['csrf_protection'] = TRUE; |
| 69 | |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 70 | If you use the :doc:`form helper <../helpers/form_helper>`, then |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 71 | :func:`form_open()` will automatically insert a hidden csrf field in |
Andrey Andreev | 25a246c | 2013-12-17 13:07:26 +0200 | [diff] [blame] | 72 | your forms. If not, then you can use ``get_csrf_token_name()`` |
| 73 | and ``get_csrf_hash()`` |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 74 | :: |
| 75 | |
| 76 | $csrf = array( |
Andrey Andreev | 25a246c | 2013-12-17 13:07:26 +0200 | [diff] [blame] | 77 | 'name' => $this->security->get_csrf_token_name(), |
| 78 | 'hash' => $this->security->get_csrf_hash() |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 79 | ); |
| 80 | |
| 81 | ... |
| 82 | |
| 83 | <input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" /> |
| 84 | |
| 85 | Tokens may be either regenerated on every submission (default) or |
| 86 | kept the same throughout the life of the CSRF cookie. The default |
| 87 | regeneration of tokens provides stricter security, but may result |
| 88 | in usability concerns as other tokens become invalid (back/forward |
| 89 | navigation, multiple tabs/windows, asynchronous actions, etc). You |
| 90 | may alter this behavior by editing the following config parameter |
| 91 | |
| 92 | :: |
RS71 | 23ea93b | 2012-01-03 12:43:16 -0200 | [diff] [blame] | 93 | |
| 94 | $config['csrf_regeneration'] = TRUE; |
| 95 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 96 | Select URIs can be whitelisted from csrf protection (for example API |
| 97 | endpoints expecting externally POSTed content). You can add these URIs |
| 98 | by editing the 'csrf_exclude_uris' config parameter:: |
| 99 | |
| 100 | $config['csrf_exclude_uris'] = array('api/person/add'); |
| 101 | |
Andrey Andreev | 6c52096 | 2014-08-18 12:24:42 +0300 | [diff] [blame] | 102 | Regular expressions are also supported (case-insensitive):: |
Casey Hancock | 2f4c3bc | 2014-08-11 12:52:20 -0400 | [diff] [blame] | 103 | |
Andrey Andreev | 6c52096 | 2014-08-18 12:24:42 +0300 | [diff] [blame] | 104 | $config['csrf_exclude_uris'] = array( |
| 105 | 'api/record/[0-9]+', |
| 106 | 'api/title/[a-z]+' |
| 107 | ); |
Casey Hancock | 2f4c3bc | 2014-08-11 12:52:20 -0400 | [diff] [blame] | 108 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 109 | *************** |
| 110 | Class Reference |
| 111 | *************** |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 112 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 113 | .. php:class:: CI_Security |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 114 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 115 | .. php:method:: xss_clean($str[, $is_image = FALSE]) |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 116 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 117 | :param mixed $str: Input string or an array of strings |
| 118 | :returns: XSS-clean data |
| 119 | :rtype: mixed |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 120 | |
| 121 | Tries to remove XSS exploits from the input data and returns the cleaned string. |
| 122 | If the optional second parameter is set to true, it will return boolean TRUE if the image is safe to use and FALSE if malicious data was detected in it. |
| 123 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 124 | .. php:method:: sanitize_filename($str[, $relative_path = FALSE]) |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 125 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 126 | :param string $str: File name/path |
| 127 | :param bool $relative_path: Whether to preserve any directories in the file path |
| 128 | :returns: Sanitized file name/path |
| 129 | :rtype: string |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 130 | |
| 131 | Tries to sanitize filenames in order to prevent directory traversal attempts |
| 132 | and other security threats, which is particularly useful for files that were supplied via user input. |
| 133 | :: |
| 134 | |
| 135 | $filename = $this->security->sanitize_filename($this->input->post('filename')); |
| 136 | |
| 137 | If it is acceptable for the user input to include relative paths, e.g. |
| 138 | *file/in/some/approved/folder.txt*, you can set the second optional parameter, ``$relative_path`` to TRUE. |
| 139 | :: |
| 140 | |
| 141 | $filename = $this->security->sanitize_filename($this->input->post('filename'), TRUE); |
| 142 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 143 | .. php:method:: get_csrf_token_name() |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 144 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 145 | :returns: CSRF token name |
| 146 | :rtype: string |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 147 | |
| 148 | Returns the CSRF token name (the ``$config['csrf_token_name']`` value). |
| 149 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 150 | .. php:method:: get_csrf_hash() |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 151 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 152 | :returns: CSRF hash |
| 153 | :rtype: string |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 154 | |
| 155 | Returns the CSRF hash value. Useful in combination with ``get_csrf_token_name()`` |
| 156 | for manually building forms or sending valid AJAX POST requests. |
| 157 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 158 | .. php:method:: entity_decode($str[, $charset = NULL]) |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 159 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 160 | :param string $str: Input string |
| 161 | :param string $charset: Character set of the input string |
| 162 | :returns: Entity-decoded string |
| 163 | :rtype: string |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame] | 164 | |
| 165 | This method acts a lot like PHP's own native ``html_entity_decode()`` function in ENT_COMPAT mode, only |
| 166 | it tries to detect HTML entities that don't end in a semicolon because some browsers allow that. |
| 167 | |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 168 | If the ``$charset`` parameter is left empty, then your configured ``$config['charset']`` value will be used. |
| 169 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 170 | .. php:method:: get_random_bytes($length) |
Andrey Andreev | 487ccc9 | 2014-08-27 16:26:23 +0300 | [diff] [blame] | 171 | |
| 172 | :param int $length: Output length |
| 173 | :returns: A binary stream of random bytes or FALSE on failure |
| 174 | :rtype: string |
| 175 | |
| 176 | A convenience method for getting proper random bytes via ``mcrypt_create_iv()``, |
| 177 | ``/dev/urandom`` or ``openssl_random_pseudo_bytes()`` (in that order), if one |
| 178 | of them is available. |
| 179 | |
| 180 | Used for generating CSRF and XSS tokens. |
| 181 | |
| 182 | .. note:: The output is NOT guaranteed to be cryptographically secure, |
| 183 | just the best attempt at that. |