blob: ac56fc589c0ec790435b30ec17d31b2906e2459a [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
19CodeIgniter comes with a Cross Site Scripting Hack prevention filter
20which can either run automatically to filter all POST and COOKIE data
21that is encountered, or you can run it on a per item basis. By default
22it does **not** run globally since it requires a bit of processing
23overhead, and since you may not need it in all cases.
24
25The XSS filter looks for commonly used techniques to trigger Javascript
26or other types of code that attempt to hijack cookies or do other
27malicious things. If anything disallowed is encountered it is rendered
28safe by converting the data to character entities.
29
30Note: This function should only be used to deal with data upon
31submission. It's not something that should be used for general runtime
32processing since it requires a fair amount of processing overhead.
33
Andrey Andreev5363f462014-01-03 12:31:53 +020034To filter data through the XSS filter use the ``xss_clean()`` method::
Derek Jones8ede1a22011-10-05 13:34:52 -050035
36 $data = $this->security->xss_clean($data);
37
38If you want the filter to run automatically every time it encounters
39POST or COOKIE data you can enable it by opening your
40application/config/config.php file and setting this::
41
42 $config['global_xss_filtering'] = TRUE;
43
Andrey Andreev5363f462014-01-03 12:31:53 +020044An optional second parameter, *is_image*, allows this function to be used
Derek Jones8ede1a22011-10-05 13:34:52 -050045to test images for potential XSS attacks, useful for file upload
46security. When this second parameter is set to TRUE, instead of
47returning an altered string, the function returns TRUE if the image is
48safe, and FALSE if it contained potentially malicious information that a
49browser may attempt to execute.
50
51::
52
Derek Joneseb946d02011-10-05 15:47:43 -050053 if ($this->security->xss_clean($file, TRUE) === FALSE)
54 {
Andrey Andreev5363f462014-01-03 12:31:53 +020055 // file failed the XSS test
Derek Joneseb946d02011-10-05 15:47:43 -050056 }
Derek Jones8ede1a22011-10-05 13:34:52 -050057
James L Parry08191be2014-12-20 02:37:13 -080058*********************************
Derek Jones8ede1a22011-10-05 13:34:52 -050059Cross-site request forgery (CSRF)
James L Parry08191be2014-12-20 02:37:13 -080060*********************************
Derek Jones8ede1a22011-10-05 13:34:52 -050061
Andrey Andreev5363f462014-01-03 12:31:53 +020062You can enable CSRF protection by altering your **application/config/config.php**
63file in the following way::
Derek Jones8ede1a22011-10-05 13:34:52 -050064
65 $config['csrf_protection'] = TRUE;
66
Andrey Andreevf795ab52012-10-24 21:28:25 +030067If you use the :doc:`form helper <../helpers/form_helper>`, then
Andrey Andreev5363f462014-01-03 12:31:53 +020068:func:`form_open()` will automatically insert a hidden csrf field in
Andrey Andreev25a246c2013-12-17 13:07:26 +020069your forms. If not, then you can use ``get_csrf_token_name()``
70and ``get_csrf_hash()``
Andrey Andreevf795ab52012-10-24 21:28:25 +030071::
72
73 $csrf = array(
Andrey Andreev25a246c2013-12-17 13:07:26 +020074 'name' => $this->security->get_csrf_token_name(),
75 'hash' => $this->security->get_csrf_hash()
Andrey Andreevf795ab52012-10-24 21:28:25 +030076 );
77
78 ...
79
80 <input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />
81
82Tokens may be either regenerated on every submission (default) or
83kept the same throughout the life of the CSRF cookie. The default
84regeneration of tokens provides stricter security, but may result
85in usability concerns as other tokens become invalid (back/forward
86navigation, multiple tabs/windows, asynchronous actions, etc). You
87may alter this behavior by editing the following config parameter
88
89::
RS7123ea93b2012-01-03 12:43:16 -020090
91 $config['csrf_regeneration'] = TRUE;
92
Derek Jones8ede1a22011-10-05 13:34:52 -050093Select URIs can be whitelisted from csrf protection (for example API
94endpoints expecting externally POSTed content). You can add these URIs
95by editing the 'csrf_exclude_uris' config parameter::
96
97 $config['csrf_exclude_uris'] = array('api/person/add');
98
Andrey Andreev6c520962014-08-18 12:24:42 +030099Regular expressions are also supported (case-insensitive)::
Casey Hancock2f4c3bc2014-08-11 12:52:20 -0400100
Andrey Andreev6c520962014-08-18 12:24:42 +0300101 $config['csrf_exclude_uris'] = array(
102 'api/record/[0-9]+',
103 'api/title/[a-z]+'
104 );
Casey Hancock2f4c3bc2014-08-11 12:52:20 -0400105
Andrey Andreev5363f462014-01-03 12:31:53 +0200106***************
107Class Reference
108***************
Andrey Andreevf795ab52012-10-24 21:28:25 +0300109
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200110.. php:class:: CI_Security
Andrey Andreevf795ab52012-10-24 21:28:25 +0300111
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200112 .. php:method:: xss_clean($str[, $is_image = FALSE])
Andrey Andreevf795ab52012-10-24 21:28:25 +0300113
Andrey Andreev28c2c972014-02-08 04:27:48 +0200114 :param mixed $str: Input string or an array of strings
115 :returns: XSS-clean data
116 :rtype: mixed
Andrey Andreev5363f462014-01-03 12:31:53 +0200117
118 Tries to remove XSS exploits from the input data and returns the cleaned string.
119 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.
120
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200121 .. php:method:: sanitize_filename($str[, $relative_path = FALSE])
Andrey Andreev5363f462014-01-03 12:31:53 +0200122
Andrey Andreev28c2c972014-02-08 04:27:48 +0200123 :param string $str: File name/path
124 :param bool $relative_path: Whether to preserve any directories in the file path
125 :returns: Sanitized file name/path
126 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200127
128 Tries to sanitize filenames in order to prevent directory traversal attempts
129 and other security threats, which is particularly useful for files that were supplied via user input.
130 ::
131
132 $filename = $this->security->sanitize_filename($this->input->post('filename'));
133
134 If it is acceptable for the user input to include relative paths, e.g.
135 *file/in/some/approved/folder.txt*, you can set the second optional parameter, ``$relative_path`` to TRUE.
136 ::
137
138 $filename = $this->security->sanitize_filename($this->input->post('filename'), TRUE);
139
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200140 .. php:method:: get_csrf_token_name()
Andrey Andreev5363f462014-01-03 12:31:53 +0200141
Andrey Andreev28c2c972014-02-08 04:27:48 +0200142 :returns: CSRF token name
143 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200144
145 Returns the CSRF token name (the ``$config['csrf_token_name']`` value).
146
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200147 .. php:method:: get_csrf_hash()
Andrey Andreev5363f462014-01-03 12:31:53 +0200148
Andrey Andreev28c2c972014-02-08 04:27:48 +0200149 :returns: CSRF hash
150 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200151
152 Returns the CSRF hash value. Useful in combination with ``get_csrf_token_name()``
153 for manually building forms or sending valid AJAX POST requests.
154
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200155 .. php:method:: entity_decode($str[, $charset = NULL])
Andrey Andreev5363f462014-01-03 12:31:53 +0200156
Andrey Andreev28c2c972014-02-08 04:27:48 +0200157 :param string $str: Input string
158 :param string $charset: Character set of the input string
159 :returns: Entity-decoded string
160 :rtype: string
Andrey Andreev5363f462014-01-03 12:31:53 +0200161
162 This method acts a lot like PHP's own native ``html_entity_decode()`` function in ENT_COMPAT mode, only
163 it tries to detect HTML entities that don't end in a semicolon because some browsers allow that.
164
Andrey Andreev487ccc92014-08-27 16:26:23 +0300165 If the ``$charset`` parameter is left empty, then your configured ``$config['charset']`` value will be used.
166
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200167 .. php:method:: get_random_bytes($length)
Andrey Andreev487ccc92014-08-27 16:26:23 +0300168
169 :param int $length: Output length
170 :returns: A binary stream of random bytes or FALSE on failure
171 :rtype: string
172
173 A convenience method for getting proper random bytes via ``mcrypt_create_iv()``,
174 ``/dev/urandom`` or ``openssl_random_pseudo_bytes()`` (in that order), if one
175 of them is available.
176
177 Used for generating CSRF and XSS tokens.
178
179 .. note:: The output is NOT guaranteed to be cryptographically secure,
180 just the best attempt at that.