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 | |
| 8 | XSS Filtering |
| 9 | ============= |
| 10 | |
| 11 | CodeIgniter comes with a Cross Site Scripting Hack prevention filter |
| 12 | which can either run automatically to filter all POST and COOKIE data |
| 13 | that is encountered, or you can run it on a per item basis. By default |
| 14 | it does **not** run globally since it requires a bit of processing |
| 15 | overhead, and since you may not need it in all cases. |
| 16 | |
| 17 | The XSS filter looks for commonly used techniques to trigger Javascript |
| 18 | or other types of code that attempt to hijack cookies or do other |
| 19 | malicious things. If anything disallowed is encountered it is rendered |
| 20 | safe by converting the data to character entities. |
| 21 | |
| 22 | Note: This function should only be used to deal with data upon |
| 23 | submission. It's not something that should be used for general runtime |
| 24 | processing since it requires a fair amount of processing overhead. |
| 25 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 26 | To filter data through the XSS filter use the ``xss_clean()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | |
| 28 | $data = $this->security->xss_clean($data); |
| 29 | |
| 30 | If you want the filter to run automatically every time it encounters |
| 31 | POST or COOKIE data you can enable it by opening your |
| 32 | application/config/config.php file and setting this:: |
| 33 | |
| 34 | $config['global_xss_filtering'] = TRUE; |
| 35 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 36 | .. note:: If you use the form validation class, it gives you the option of |
| 37 | XSS filtering as well. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 39 | An optional second parameter, *is_image*, allows this function to be used |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | to test images for potential XSS attacks, useful for file upload |
| 41 | security. When this second parameter is set to TRUE, instead of |
| 42 | returning an altered string, the function returns TRUE if the image is |
| 43 | safe, and FALSE if it contained potentially malicious information that a |
| 44 | browser may attempt to execute. |
| 45 | |
| 46 | :: |
| 47 | |
Derek Jones | eb946d0 | 2011-10-05 15:47:43 -0500 | [diff] [blame] | 48 | if ($this->security->xss_clean($file, TRUE) === FALSE) |
| 49 | { |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 50 | // file failed the XSS test |
Derek Jones | eb946d0 | 2011-10-05 15:47:43 -0500 | [diff] [blame] | 51 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | Cross-site request forgery (CSRF) |
| 54 | ================================= |
| 55 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 56 | You can enable CSRF protection by altering your **application/config/config.php** |
| 57 | file in the following way:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | |
| 59 | $config['csrf_protection'] = TRUE; |
| 60 | |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 61 | If you use the :doc:`form helper <../helpers/form_helper>`, then |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 62 | :func:`form_open()` will automatically insert a hidden csrf field in |
| 63 | your forms. If not, then you can use ``csrf_get_token_name()`` and ``csrf_get_hash()`` |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 64 | :: |
| 65 | |
| 66 | $csrf = array( |
| 67 | 'name' => $this->security->csrf_get_token_name(), |
| 68 | 'hash' => $this->security->csrf_get_hash() |
| 69 | ); |
| 70 | |
| 71 | ... |
| 72 | |
| 73 | <input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" /> |
| 74 | |
| 75 | Tokens may be either regenerated on every submission (default) or |
| 76 | kept the same throughout the life of the CSRF cookie. The default |
| 77 | regeneration of tokens provides stricter security, but may result |
| 78 | in usability concerns as other tokens become invalid (back/forward |
| 79 | navigation, multiple tabs/windows, asynchronous actions, etc). You |
| 80 | may alter this behavior by editing the following config parameter |
| 81 | |
| 82 | :: |
RS71 | 23ea93b | 2012-01-03 12:43:16 -0200 | [diff] [blame] | 83 | |
| 84 | $config['csrf_regeneration'] = TRUE; |
| 85 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | Select URIs can be whitelisted from csrf protection (for example API |
| 87 | endpoints expecting externally POSTed content). You can add these URIs |
| 88 | by editing the 'csrf_exclude_uris' config parameter:: |
| 89 | |
| 90 | $config['csrf_exclude_uris'] = array('api/person/add'); |
| 91 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 92 | *************** |
| 93 | Class Reference |
| 94 | *************** |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 95 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 96 | .. class:: CI_Security |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 97 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 98 | .. method:: xss_clean($str[, $is_image = FALSE]) |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 99 | |
Andrey Andreev | 5363f46 | 2014-01-03 12:31:53 +0200 | [diff] [blame^] | 100 | :param string $str: Input string |
| 101 | :returns: mixed |
| 102 | |
| 103 | Tries to remove XSS exploits from the input data and returns the cleaned string. |
| 104 | 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. |
| 105 | |
| 106 | .. method:: sanitize_filename($str[, $relative_path = FALSE]) |
| 107 | |
| 108 | :param string $str: File name/path |
| 109 | :param bool $relative_path: Whether to preserve any directories in the file path |
| 110 | :returns: string |
| 111 | |
| 112 | Tries to sanitize filenames in order to prevent directory traversal attempts |
| 113 | and other security threats, which is particularly useful for files that were supplied via user input. |
| 114 | :: |
| 115 | |
| 116 | $filename = $this->security->sanitize_filename($this->input->post('filename')); |
| 117 | |
| 118 | If it is acceptable for the user input to include relative paths, e.g. |
| 119 | *file/in/some/approved/folder.txt*, you can set the second optional parameter, ``$relative_path`` to TRUE. |
| 120 | :: |
| 121 | |
| 122 | $filename = $this->security->sanitize_filename($this->input->post('filename'), TRUE); |
| 123 | |
| 124 | .. method:: get_csrf_token_name() |
| 125 | |
| 126 | :returns: string |
| 127 | |
| 128 | Returns the CSRF token name (the ``$config['csrf_token_name']`` value). |
| 129 | |
| 130 | .. method:: get_csrf_hash() |
| 131 | |
| 132 | :returns: string |
| 133 | |
| 134 | Returns the CSRF hash value. Useful in combination with ``get_csrf_token_name()`` |
| 135 | for manually building forms or sending valid AJAX POST requests. |
| 136 | |
| 137 | .. method:: entity_decode($str[, $charset = NULL]) |
| 138 | |
| 139 | :param string $str: Input string |
| 140 | :param string $charset: Character set of the input string |
| 141 | |
| 142 | This method acts a lot like PHP's own native ``html_entity_decode()`` function in ENT_COMPAT mode, only |
| 143 | it tries to detect HTML entities that don't end in a semicolon because some browsers allow that. |
| 144 | |
| 145 | If the ``$charset`` parameter is left empty, then your configured ``$config['charset']`` value will be used. |