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