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 | |
| 26 | To filter data through the XSS filter use this function: |
| 27 | |
| 28 | $this->security->xss_clean() |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 29 | ============================ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
| 31 | Here is an usage example:: |
| 32 | |
| 33 | $data = $this->security->xss_clean($data); |
| 34 | |
| 35 | If you want the filter to run automatically every time it encounters |
| 36 | POST or COOKIE data you can enable it by opening your |
| 37 | application/config/config.php file and setting this:: |
| 38 | |
| 39 | $config['global_xss_filtering'] = TRUE; |
| 40 | |
| 41 | Note: If you use the form validation class, it gives you the option of |
| 42 | XSS filtering as well. |
| 43 | |
| 44 | An optional second parameter, is_image, allows this function to be used |
| 45 | to test images for potential XSS attacks, useful for file upload |
| 46 | security. When this second parameter is set to TRUE, instead of |
| 47 | returning an altered string, the function returns TRUE if the image is |
| 48 | safe, and FALSE if it contained potentially malicious information that a |
| 49 | browser may attempt to execute. |
| 50 | |
| 51 | :: |
| 52 | |
Derek Jones | eb946d0 | 2011-10-05 15:47:43 -0500 | [diff] [blame] | 53 | if ($this->security->xss_clean($file, TRUE) === FALSE) |
| 54 | { |
| 55 | // file failed the XSS test |
| 56 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 57 | |
| 58 | $this->security->sanitize_filename() |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 59 | ==================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 60 | |
| 61 | When accepting filenames from user input, it is best to sanitize them to |
| 62 | prevent directory traversal and other security related issues. To do so, |
| 63 | use the sanitize_filename() method of the Security class. Here is an |
| 64 | example:: |
| 65 | |
| 66 | $filename = $this->security->sanitize_filename($this->input->post('filename')); |
| 67 | |
| 68 | If it is acceptable for the user input to include relative paths, e.g. |
| 69 | file/in/some/approved/folder.txt, you can set the second optional |
| 70 | parameter, $relative_path to TRUE. |
| 71 | |
| 72 | :: |
| 73 | |
| 74 | $filename = $this->security->sanitize_filename($this->input->post('filename'), TRUE); |
| 75 | |
| 76 | Cross-site request forgery (CSRF) |
| 77 | ================================= |
| 78 | |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 79 | You can enable CSRF protection by opening your |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | application/config/config.php file and setting this:: |
| 81 | |
| 82 | $config['csrf_protection'] = TRUE; |
| 83 | |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 84 | If you use the :doc:`form helper <../helpers/form_helper>`, then |
| 85 | ``form_open()`` will automatically insert a hidden csrf field in |
| 86 | your forms. If not, then you can use ``csrf_get_token_name()`` |
| 87 | and ``csrf_get_hash()`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 89 | :: |
| 90 | |
| 91 | $csrf = array( |
| 92 | 'name' => $this->security->csrf_get_token_name(), |
| 93 | 'hash' => $this->security->csrf_get_hash() |
| 94 | ); |
| 95 | |
| 96 | ... |
| 97 | |
| 98 | <input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" /> |
| 99 | |
| 100 | Tokens may be either regenerated on every submission (default) or |
| 101 | kept the same throughout the life of the CSRF cookie. The default |
| 102 | regeneration of tokens provides stricter security, but may result |
| 103 | in usability concerns as other tokens become invalid (back/forward |
| 104 | navigation, multiple tabs/windows, asynchronous actions, etc). You |
| 105 | may alter this behavior by editing the following config parameter |
| 106 | |
| 107 | :: |
RS71 | 23ea93b | 2012-01-03 12:43:16 -0200 | [diff] [blame] | 108 | |
| 109 | $config['csrf_regeneration'] = TRUE; |
| 110 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 111 | Select URIs can be whitelisted from csrf protection (for example API |
| 112 | endpoints expecting externally POSTed content). You can add these URIs |
| 113 | by editing the 'csrf_exclude_uris' config parameter:: |
| 114 | |
| 115 | $config['csrf_exclude_uris'] = array('api/person/add'); |
| 116 | |
Andrey Andreev | f795ab5 | 2012-10-24 21:28:25 +0300 | [diff] [blame] | 117 | $this->security->get_csrf_token_name() |
| 118 | ====================================== |
| 119 | |
| 120 | Returns the CSRF token name, which is set by |
| 121 | ``$config['csrf_token_name']``. |
| 122 | |
| 123 | $this->security->get_csrf_hash() |
| 124 | ================================ |
| 125 | |
| 126 | Returns the CSRF hash value. Useful in combination with |
| 127 | ``get_csrf_token_name()`` for manually building forms or |
| 128 | sending valid AJAX POST requests. |