blob: f7604ef007b0ef432c34cb1b6aef16f71b959610 [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
James L Parry08191be2014-12-20 02:37:13 -080043*********************************
Derek Jones8ede1a22011-10-05 13:34:52 -050044Cross-site request forgery (CSRF)
James L Parry08191be2014-12-20 02:37:13 -080045*********************************
Derek Jones8ede1a22011-10-05 13:34:52 -050046
Andrey Andreev5363f462014-01-03 12:31:53 +020047You can enable CSRF protection by altering your **application/config/config.php**
48file in the following way::
Derek Jones8ede1a22011-10-05 13:34:52 -050049
50 $config['csrf_protection'] = TRUE;
51
Andrey Andreevf795ab52012-10-24 21:28:25 +030052If you use the :doc:`form helper <../helpers/form_helper>`, then
Andrey Andreev5363f462014-01-03 12:31:53 +020053:func:`form_open()` will automatically insert a hidden csrf field in
Andrey Andreev25a246c2013-12-17 13:07:26 +020054your forms. If not, then you can use ``get_csrf_token_name()``
55and ``get_csrf_hash()``
Andrey Andreevf795ab52012-10-24 21:28:25 +030056::
57
58 $csrf = array(
Andrey Andreev25a246c2013-12-17 13:07:26 +020059 'name' => $this->security->get_csrf_token_name(),
60 'hash' => $this->security->get_csrf_hash()
Andrey Andreevf795ab52012-10-24 21:28:25 +030061 );
62
63 ...
64
65 <input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />
66
67Tokens may be either regenerated on every submission (default) or
68kept the same throughout the life of the CSRF cookie. The default
69regeneration of tokens provides stricter security, but may result
70in usability concerns as other tokens become invalid (back/forward
71navigation, multiple tabs/windows, asynchronous actions, etc). You
72may alter this behavior by editing the following config parameter
73
74::
RS7123ea93b2012-01-03 12:43:16 -020075
Master Yoda12617cf2015-04-30 02:32:59 -070076 $config['csrf_regenerate'] = TRUE;
RS7123ea93b2012-01-03 12:43:16 -020077
Derek Jones8ede1a22011-10-05 13:34:52 -050078Select URIs can be whitelisted from csrf protection (for example API
79endpoints expecting externally POSTed content). You can add these URIs
80by editing the 'csrf_exclude_uris' config parameter::
81
82 $config['csrf_exclude_uris'] = array('api/person/add');
83
Andrey Andreev6c520962014-08-18 12:24:42 +030084Regular expressions are also supported (case-insensitive)::
Casey Hancock2f4c3bc2014-08-11 12:52:20 -040085
Andrey Andreev6c520962014-08-18 12:24:42 +030086 $config['csrf_exclude_uris'] = array(
87 'api/record/[0-9]+',
88 'api/title/[a-z]+'
89 );
Casey Hancock2f4c3bc2014-08-11 12:52:20 -040090
Andrey Andreev5363f462014-01-03 12:31:53 +020091***************
92Class Reference
93***************
Andrey Andreevf795ab52012-10-24 21:28:25 +030094
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020095.. php:class:: CI_Security
Andrey Andreevf795ab52012-10-24 21:28:25 +030096
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020097 .. php:method:: xss_clean($str[, $is_image = FALSE])
Andrey Andreevf795ab52012-10-24 21:28:25 +030098
Andrey Andreev28c2c972014-02-08 04:27:48 +020099 :param mixed $str: Input string or an array of strings
100 :returns: XSS-clean data
101 :rtype: mixed
Andrey Andreev5363f462014-01-03 12:31:53 +0200102
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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200106 .. php:method:: sanitize_filename($str[, $relative_path = FALSE])
Andrey Andreev5363f462014-01-03 12:31:53 +0200107
Andrey Andreev28c2c972014-02-08 04:27:48 +0200108 :param string $str: File name/path
109 :param bool $relative_path: Whether to preserve any directories in the file path
110 :returns: Sanitized file name/path
111 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200112
113 Tries to sanitize filenames in order to prevent directory traversal attempts
114 and other security threats, which is particularly useful for files that were supplied via user input.
115 ::
116
117 $filename = $this->security->sanitize_filename($this->input->post('filename'));
118
119 If it is acceptable for the user input to include relative paths, e.g.
120 *file/in/some/approved/folder.txt*, you can set the second optional parameter, ``$relative_path`` to TRUE.
121 ::
122
123 $filename = $this->security->sanitize_filename($this->input->post('filename'), TRUE);
124
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200125 .. php:method:: get_csrf_token_name()
Andrey Andreev5363f462014-01-03 12:31:53 +0200126
Andrey Andreev28c2c972014-02-08 04:27:48 +0200127 :returns: CSRF token name
128 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200129
130 Returns the CSRF token name (the ``$config['csrf_token_name']`` value).
131
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200132 .. php:method:: get_csrf_hash()
Andrey Andreev5363f462014-01-03 12:31:53 +0200133
Andrey Andreev28c2c972014-02-08 04:27:48 +0200134 :returns: CSRF hash
135 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200136
137 Returns the CSRF hash value. Useful in combination with ``get_csrf_token_name()``
138 for manually building forms or sending valid AJAX POST requests.
139
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200140 .. php:method:: entity_decode($str[, $charset = NULL])
Andrey Andreev5363f462014-01-03 12:31:53 +0200141
Andrey Andreev28c2c972014-02-08 04:27:48 +0200142 :param string $str: Input string
143 :param string $charset: Character set of the input string
144 :returns: Entity-decoded string
145 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200146
147 This method acts a lot like PHP's own native ``html_entity_decode()`` function in ENT_COMPAT mode, only
148 it tries to detect HTML entities that don't end in a semicolon because some browsers allow that.
149
Andrey Andreev487ccc92014-08-27 16:26:23 +0300150 If the ``$charset`` parameter is left empty, then your configured ``$config['charset']`` value will be used.
151
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200152 .. php:method:: get_random_bytes($length)
Andrey Andreev487ccc92014-08-27 16:26:23 +0300153
154 :param int $length: Output length
155 :returns: A binary stream of random bytes or FALSE on failure
156 :rtype: string
157
158 A convenience method for getting proper random bytes via ``mcrypt_create_iv()``,
159 ``/dev/urandom`` or ``openssl_random_pseudo_bytes()`` (in that order), if one
160 of them is available.
161
162 Used for generating CSRF and XSS tokens.
163
164 .. note:: The output is NOT guaranteed to be cryptographically secure,
165 just the best attempt at that.