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 | |
| 8 | URI Security |
| 9 | ============ |
| 10 | |
| 11 | CodeIgniter is fairly restrictive regarding which characters it allows |
| 12 | in your URI strings in order to help minimize the possibility that |
| 13 | malicious data can be passed to your application. URIs may only contain |
| 14 | the following: |
| 15 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 16 | - Alpha-numeric text (latin characters only) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 17 | - Tilde: ~ |
vlakoff | 4ad3708 | 2013-03-29 18:14:22 +0100 | [diff] [blame] | 18 | - Percent sign: % |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 19 | - Period: . |
| 20 | - Colon: : |
| 21 | - Underscore: \_ |
| 22 | - Dash: - |
vlakoff | 4ad3708 | 2013-03-29 18:14:22 +0100 | [diff] [blame] | 23 | - Space |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | |
| 25 | Register_globals |
| 26 | ================= |
| 27 | |
| 28 | During system initialization all global variables are unset, except |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 29 | those found in the ``$_GET``, ``$_POST``, and ``$_COOKIE`` arrays. |
| 30 | The unsetting routine is effectively the same as |
| 31 | *register_globals = off*. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 32 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 33 | display_errors |
| 34 | ============== |
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 | In production environments, it is typically desirable to "disable" PHP's |
| 37 | error reporting by setting the internal *display_errors* flag to a value |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | of 0. This disables native PHP errors from being rendered as output, |
| 39 | which may potentially contain sensitive information. |
| 40 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 41 | Setting CodeIgniter's **ENVIRONMENT** constant in index.php to a value of |
| 42 | **\'production\'** will turn off these errors. In development mode, it is |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | recommended that a value of 'development' is used. More information |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 44 | about differentiating between environments can be found on the |
| 45 | :doc:`Handling Environments <environments>` page. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
| 47 | magic_quotes_runtime |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 48 | ==================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 50 | The *magic_quotes_runtime* directive is turned off during system |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | initialization so that you don't have to remove slashes when retrieving |
| 52 | data from your database. |
| 53 | |
| 54 | ************** |
| 55 | Best Practices |
| 56 | ************** |
| 57 | |
| 58 | Before accepting any data into your application, whether it be POST data |
| 59 | from a form submission, COOKIE data, URI data, XML-RPC data, or even |
| 60 | data from the SERVER array, you are encouraged to practice this three |
| 61 | step approach: |
| 62 | |
| 63 | #. Filter the data as if it were tainted. |
| 64 | #. Validate the data to ensure it conforms to the correct type, length, |
| 65 | size, etc. (sometimes this step can replace step one) |
| 66 | #. Escape the data before submitting it into your database. |
| 67 | |
| 68 | CodeIgniter provides the following functions to assist in this process: |
| 69 | |
| 70 | XSS Filtering |
| 71 | ============= |
| 72 | |
| 73 | CodeIgniter comes with a Cross Site Scripting filter. This filter |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 74 | looks for commonly used techniques to embed malicious JavaScript into |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | your data, or other types of code that attempt to hijack cookies or |
| 76 | do other malicious things. The XSS Filter is described |
| 77 | :doc:`here <../libraries/security>`. |
| 78 | |
| 79 | Validate the data |
| 80 | ================= |
| 81 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 82 | CodeIgniter has a :doc:`Form Validation Library |
| 83 | <../libraries/form_validation>` that assists you in |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | validating, filtering, and prepping your data. |
| 85 | |
| 86 | Escape all data before database insertion |
| 87 | ========================================= |
| 88 | |
| 89 | Never insert information into your database without escaping it. |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 90 | Please see the section that discusses :doc:`database queries |
| 91 | <../database/queries>` for more information. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 93 | Hide your files |
| 94 | =============== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 95 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 96 | Another good security practice is to only leave your *index.php* |
| 97 | and "assets" (e.g. .js, css and image files) under your server's |
| 98 | *webroot* directory (most commonly named "htdocs/"). These are |
| 99 | the only files that you would need to be accessible from the web. |
| 100 | |
| 101 | Allowing your visitors to see anything else would potentially |
| 102 | allow them to access sensitive data, execute scripts, etc. |
| 103 | |
| 104 | If you're not allowed to do that, you can try using a .htaccess |
| 105 | file to restrict access to those resources. |
| 106 | |
| 107 | CodeIgniter will have an index.html file in all of its |
| 108 | directories in an attempt to hide some of this data, but have |
| 109 | it in mind that this is not enough to prevent a serious |
| 110 | attacker. |