Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ######## |
| 2 | Security |
| 3 | ######## |
| 4 | |
| 5 | This page describes some "best practices" regarding web security, and |
| 6 | details CodeIgniter's internal security features. |
| 7 | |
Andrey Andreev | 4bb2b95 | 2015-10-12 13:55:24 +0300 | [diff] [blame] | 8 | .. note:: If you came here looking for a security contact, please refer to |
| 9 | our `Contribution Guide <../contributing/index>`. |
| 10 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 11 | URI Security |
| 12 | ============ |
| 13 | |
| 14 | CodeIgniter is fairly restrictive regarding which characters it allows |
| 15 | in your URI strings in order to help minimize the possibility that |
| 16 | malicious data can be passed to your application. URIs may only contain |
| 17 | the following: |
| 18 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 19 | - Alpha-numeric text (latin characters only) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | - Tilde: ~ |
vlakoff | 4ad3708 | 2013-03-29 18:14:22 +0100 | [diff] [blame] | 21 | - Percent sign: % |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | - Period: . |
| 23 | - Colon: : |
| 24 | - Underscore: \_ |
| 25 | - Dash: - |
vlakoff | 4ad3708 | 2013-03-29 18:14:22 +0100 | [diff] [blame] | 26 | - Space |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | |
| 28 | Register_globals |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 29 | ================ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 31 | During system initialization all global variables that are found to exist |
| 32 | in the ``$_GET``, ``$_POST``, ``$_REQUEST`` and ``$_COOKIE`` are unset. |
| 33 | |
| 34 | The unsetting routine is effectively the same as *register_globals = off*. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 36 | display_errors |
| 37 | ============== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 39 | In production environments, it is typically desirable to "disable" PHP's |
| 40 | error reporting by setting the internal *display_errors* flag to a value |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | of 0. This disables native PHP errors from being rendered as output, |
| 42 | which may potentially contain sensitive information. |
| 43 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 44 | Setting CodeIgniter's **ENVIRONMENT** constant in index.php to a value of |
| 45 | **\'production\'** will turn off these errors. In development mode, it is |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | recommended that a value of 'development' is used. More information |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 47 | about differentiating between environments can be found on the |
| 48 | :doc:`Handling Environments <environments>` page. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | |
| 50 | magic_quotes_runtime |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 51 | ==================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 53 | The *magic_quotes_runtime* directive is turned off during system |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | initialization so that you don't have to remove slashes when retrieving |
| 55 | data from your database. |
| 56 | |
| 57 | ************** |
| 58 | Best Practices |
| 59 | ************** |
| 60 | |
| 61 | Before accepting any data into your application, whether it be POST data |
| 62 | from a form submission, COOKIE data, URI data, XML-RPC data, or even |
| 63 | data from the SERVER array, you are encouraged to practice this three |
| 64 | step approach: |
| 65 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | #. Validate the data to ensure it conforms to the correct type, length, |
Aaron Melocik | dd6222b | 2015-06-18 08:40:43 -0700 | [diff] [blame] | 67 | size, etc. |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 68 | #. Filter the data as if it were tainted. |
| 69 | #. Escape the data before submitting it into your database or outputting |
| 70 | it to a browser. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 71 | |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 72 | CodeIgniter provides the following functions and tips to assist you |
| 73 | in this process: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
| 75 | XSS Filtering |
| 76 | ============= |
| 77 | |
| 78 | CodeIgniter comes with a Cross Site Scripting filter. This filter |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 79 | looks for commonly used techniques to embed malicious JavaScript into |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | your data, or other types of code that attempt to hijack cookies or |
| 81 | do other malicious things. The XSS Filter is described |
| 82 | :doc:`here <../libraries/security>`. |
| 83 | |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 84 | .. note:: XSS filtering should *only be performed on output*. Filtering |
| 85 | input data may modify the data in undesirable ways, including |
| 86 | stripping special characters from passwords, which reduces |
| 87 | security instead of improving it. |
| 88 | |
| 89 | CSRF protection |
| 90 | =============== |
| 91 | |
| 92 | CSRF stands for Cross-Site Request Forgery, which is the process of an |
| 93 | attacker tricking their victim into unknowingly submitting a request. |
| 94 | |
| 95 | CodeIgniter provides CSRF protection out of the box, which will get |
| 96 | automatically triggered for every non-GET HTTP request, but also needs |
| 97 | you to create your submit forms in a certain way. This is explained in |
| 98 | the :doc:`Security Library <../libraries/security>` documentation. |
| 99 | |
| 100 | Password handling |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 101 | ================= |
| 102 | |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 103 | It is *critical* that you handle passwords in your application properly. |
| 104 | |
| 105 | Unfortunately, many developers don't know how to do that, and the web is |
| 106 | full of outdated or otherwise wrongful advices, which doesn't help. |
| 107 | |
| 108 | We would like to give you a list of combined do's and don'ts to help you |
| 109 | with that. Please read below. |
| 110 | |
| 111 | - DO NOT store passwords in plain-text format. |
| 112 | |
| 113 | Always **hash** your passwords. |
| 114 | |
| 115 | - DO NOT use Base64 or similar encoding for storing passwords. |
| 116 | |
| 117 | This is as good as storing them in plain-text. Really. Do **hashing**, |
| 118 | not *encoding*. |
| 119 | |
| 120 | Encoding, and encryption too, are two-way processes. Passwords are |
| 121 | secrets that must only be known to their owner, and thus must work |
| 122 | only in one direction. Hashing does that - there's *no* un-hashing or |
| 123 | de-hashing, but there is decoding and decryption. |
| 124 | |
| 125 | - DO NOT use weak or broken hashing algorithms like MD5 or SHA1. |
| 126 | |
| 127 | These algorithms are old, proven to be flawed, and not designed for |
| 128 | password hashing in the first place. |
| 129 | |
| 130 | Also, DON'T invent your own algorithms. |
| 131 | |
| 132 | Only use strong password hashing algorithms like BCrypt, which is used |
| 133 | in PHP's own `Password Hashing <http://php.net/password>`_ functions. |
| 134 | |
| 135 | Please use them, even if you're not running PHP 5.5+, CodeIgniter |
Andrey Andreev | a838279 | 2016-07-28 16:40:12 +0300 | [diff] [blame] | 136 | provides them for you. |
Andrey Andreev | f1ca865 | 2015-02-24 20:25:16 +0200 | [diff] [blame] | 137 | |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 138 | - DO NOT ever display or send a password in plain-text format! |
| 139 | |
| 140 | Even to the password's owner, if you need a "Forgotten password" |
| 141 | feature, just randomly generate a new, one-time (this is also important) |
| 142 | password and send that instead. |
| 143 | |
Andrey Andreev | a8c499d | 2015-03-31 15:01:36 +0300 | [diff] [blame] | 144 | - DO NOT put unnecessary limits on your users' passwords. |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 145 | |
Andrey Andreev | a8c499d | 2015-03-31 15:01:36 +0300 | [diff] [blame] | 146 | If you're using a hashing algorithm other than BCrypt (which has a limit |
| 147 | of 72 characters), you should set a relatively high limit on password |
| 148 | lengths in order to mitigate DoS attacks - say, 1024 characters. |
| 149 | |
| 150 | Other than that however, there's no point in forcing a rule that a |
| 151 | password can only be up to a number of characters, or that it can't |
| 152 | contain a certain set of special characters. |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 153 | |
| 154 | Not only does this **reduce** security instead of improving it, but |
| 155 | there's literally no reason to do it. No technical limitations and |
| 156 | no (practical) storage constraints apply once you've hashed them, none! |
| 157 | |
| 158 | Validate input data |
| 159 | =================== |
| 160 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 161 | CodeIgniter has a :doc:`Form Validation Library |
| 162 | <../libraries/form_validation>` that assists you in |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 163 | validating, filtering, and prepping your data. |
| 164 | |
Andrey Andreev | 1c6522c | 2015-01-21 23:12:13 +0200 | [diff] [blame] | 165 | Even if that doesn't work for your use case however, be sure to always |
| 166 | validate and sanitize all input data. For example, if you expect a numeric |
| 167 | string for an input variable, you can check for that with ``is_numeric()`` |
| 168 | or ``ctype_digit()``. Always try to narrow down your checks to a certain |
| 169 | pattern. |
| 170 | |
| 171 | Have it in mind that this includes not only ``$_POST`` and ``$_GET`` |
| 172 | variables, but also cookies, the user-agent string and basically |
| 173 | *all data that is not created directly by your own code*. |
| 174 | |
| 175 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 176 | Escape all data before database insertion |
| 177 | ========================================= |
| 178 | |
| 179 | Never insert information into your database without escaping it. |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 180 | Please see the section that discusses :doc:`database queries |
| 181 | <../database/queries>` for more information. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 182 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 183 | Hide your files |
| 184 | =============== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 185 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 186 | Another good security practice is to only leave your *index.php* |
| 187 | and "assets" (e.g. .js, css and image files) under your server's |
| 188 | *webroot* directory (most commonly named "htdocs/"). These are |
| 189 | the only files that you would need to be accessible from the web. |
| 190 | |
| 191 | Allowing your visitors to see anything else would potentially |
| 192 | allow them to access sensitive data, execute scripts, etc. |
| 193 | |
| 194 | If you're not allowed to do that, you can try using a .htaccess |
| 195 | file to restrict access to those resources. |
| 196 | |
| 197 | CodeIgniter will have an index.html file in all of its |
| 198 | directories in an attempt to hide some of this data, but have |
| 199 | it in mind that this is not enough to prevent a serious |
Aaron Melocik | dd6222b | 2015-06-18 08:40:43 -0700 | [diff] [blame] | 200 | attacker. |