blob: 868112684413acda0c2c92b396a8fd3f196e1fee [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##############
2Security Class
3##############
4
5The Security Class contains methods that help you create a secure
6application, processing input data for security.
7
Andrey Andreevcc042092014-01-03 17:08:27 +02008.. contents::
9 :local:
10
11.. raw:: html
12
13 <div class="custom-index container"></div>
14
James L Parry08191be2014-12-20 02:37:13 -080015*************
Derek Jones8ede1a22011-10-05 13:34:52 -050016XSS Filtering
James L Parry08191be2014-12-20 02:37:13 -080017*************
Derek Jones8ede1a22011-10-05 13:34:52 -050018
Andrey Andreev61ac7bd2015-09-01 12:07:25 +030019CodeIgniter comes with a Cross Site Scripting prevention filter, which
20looks for commonly used techniques to trigger JavaScript or other types
21of code that attempt to hijack cookies or do other malicious things.
22If anything disallowed is encountered it is rendered safe by converting
23the data to character entities.
Derek Jones8ede1a22011-10-05 13:34:52 -050024
Andrey Andreev5363f462014-01-03 12:31:53 +020025To filter data through the XSS filter use the ``xss_clean()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -050026
27 $data = $this->security->xss_clean($data);
28
Andrey Andreev5363f462014-01-03 12:31:53 +020029An optional second parameter, *is_image*, allows this function to be used
Derek Jones8ede1a22011-10-05 13:34:52 -050030to test images for potential XSS attacks, useful for file upload
31security. When this second parameter is set to TRUE, instead of
32returning an altered string, the function returns TRUE if the image is
33safe, and FALSE if it contained potentially malicious information that a
34browser may attempt to execute.
35
36::
37
Derek Joneseb946d02011-10-05 15:47:43 -050038 if ($this->security->xss_clean($file, TRUE) === FALSE)
39 {
Andrey Andreev5363f462014-01-03 12:31:53 +020040 // file failed the XSS test
Derek Joneseb946d02011-10-05 15:47:43 -050041 }
Derek Jones8ede1a22011-10-05 13:34:52 -050042
Andrey Andreev56d1a702017-02-20 11:35:24 +020043.. important:: If you want to filter HTML attribute values, use
44 :php:func:`html_escape()` instead!
45
James L Parry08191be2014-12-20 02:37:13 -080046*********************************
Derek Jones8ede1a22011-10-05 13:34:52 -050047Cross-site request forgery (CSRF)
James L Parry08191be2014-12-20 02:37:13 -080048*********************************
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Andrey Andreev5363f462014-01-03 12:31:53 +020050You can enable CSRF protection by altering your **application/config/config.php**
51file in the following way::
Derek Jones8ede1a22011-10-05 13:34:52 -050052
53 $config['csrf_protection'] = TRUE;
54
Andrey Andreevf795ab52012-10-24 21:28:25 +030055If you use the :doc:`form helper <../helpers/form_helper>`, then
Andrey Andreev5363f462014-01-03 12:31:53 +020056:func:`form_open()` will automatically insert a hidden csrf field in
Andrey Andreev25a246c2013-12-17 13:07:26 +020057your forms. If not, then you can use ``get_csrf_token_name()``
58and ``get_csrf_hash()``
Andrey Andreevf795ab52012-10-24 21:28:25 +030059::
60
61 $csrf = array(
Andrey Andreev25a246c2013-12-17 13:07:26 +020062 'name' => $this->security->get_csrf_token_name(),
63 'hash' => $this->security->get_csrf_hash()
Andrey Andreevf795ab52012-10-24 21:28:25 +030064 );
65
66 ...
67
68 <input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />
69
70Tokens may be either regenerated on every submission (default) or
71kept the same throughout the life of the CSRF cookie. The default
72regeneration of tokens provides stricter security, but may result
73in usability concerns as other tokens become invalid (back/forward
74navigation, multiple tabs/windows, asynchronous actions, etc). You
75may alter this behavior by editing the following config parameter
76
77::
RS7123ea93b2012-01-03 12:43:16 -020078
Master Yoda12617cf2015-04-30 02:32:59 -070079 $config['csrf_regenerate'] = TRUE;
RS7123ea93b2012-01-03 12:43:16 -020080
Derek Jones8ede1a22011-10-05 13:34:52 -050081Select URIs can be whitelisted from csrf protection (for example API
82endpoints expecting externally POSTed content). You can add these URIs
83by editing the 'csrf_exclude_uris' config parameter::
84
85 $config['csrf_exclude_uris'] = array('api/person/add');
86
Andrey Andreev6c520962014-08-18 12:24:42 +030087Regular expressions are also supported (case-insensitive)::
Casey Hancock2f4c3bc2014-08-11 12:52:20 -040088
Andrey Andreev6c520962014-08-18 12:24:42 +030089 $config['csrf_exclude_uris'] = array(
90 'api/record/[0-9]+',
91 'api/title/[a-z]+'
92 );
Casey Hancock2f4c3bc2014-08-11 12:52:20 -040093
Andrey Andreev5363f462014-01-03 12:31:53 +020094***************
95Class Reference
96***************
Andrey Andreevf795ab52012-10-24 21:28:25 +030097
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020098.. php:class:: CI_Security
Andrey Andreevf795ab52012-10-24 21:28:25 +030099
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200100 .. php:method:: xss_clean($str[, $is_image = FALSE])
Andrey Andreevf795ab52012-10-24 21:28:25 +0300101
Andrey Andreev28c2c972014-02-08 04:27:48 +0200102 :param mixed $str: Input string or an array of strings
103 :returns: XSS-clean data
104 :rtype: mixed
Andrey Andreev5363f462014-01-03 12:31:53 +0200105
106 Tries to remove XSS exploits from the input data and returns the cleaned string.
Andrey Andreev56d1a702017-02-20 11:35:24 +0200107 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 Andreev763b7702017-06-26 10:26:33 +0300110 .. important:: This method is not suitable for filtering HTML attribute values!
Andrey Andreev56d1a702017-02-20 11:35:24 +0200111 Use :php:func:`html_escape()` for that instead.
Andrey Andreev5363f462014-01-03 12:31:53 +0200112
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200113 .. php:method:: sanitize_filename($str[, $relative_path = FALSE])
Andrey Andreev5363f462014-01-03 12:31:53 +0200114
Andrey Andreev28c2c972014-02-08 04:27:48 +0200115 :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 Andreev5363f462014-01-03 12:31:53 +0200119
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 Andreevcd3d9db2015-02-02 13:41:01 +0200132 .. php:method:: get_csrf_token_name()
Andrey Andreev5363f462014-01-03 12:31:53 +0200133
Andrey Andreev28c2c972014-02-08 04:27:48 +0200134 :returns: CSRF token name
135 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200136
137 Returns the CSRF token name (the ``$config['csrf_token_name']`` value).
138
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200139 .. php:method:: get_csrf_hash()
Andrey Andreev5363f462014-01-03 12:31:53 +0200140
Andrey Andreev28c2c972014-02-08 04:27:48 +0200141 :returns: CSRF hash
142 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200143
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 Andreevcd3d9db2015-02-02 13:41:01 +0200147 .. php:method:: entity_decode($str[, $charset = NULL])
Andrey Andreev5363f462014-01-03 12:31:53 +0200148
Andrey Andreev28c2c972014-02-08 04:27:48 +0200149 :param string $str: Input string
150 :param string $charset: Character set of the input string
151 :returns: Entity-decoded string
152 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200153
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 Andreev487ccc92014-08-27 16:26:23 +0300157 If the ``$charset`` parameter is left empty, then your configured ``$config['charset']`` value will be used.
158
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200159 .. php:method:: get_random_bytes($length)
Andrey Andreev487ccc92014-08-27 16:26:23 +0300160
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 Andreev56d1a702017-02-20 11:35:24 +0200172 just the best attempt at that.