blob: 3f93443bb6df49716efd30902f8d160f95c50014 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001########
2Security
3########
4
5This page describes some "best practices" regarding web security, and
6details CodeIgniter's internal security features.
7
8URI Security
9============
10
11CodeIgniter is fairly restrictive regarding which characters it allows
12in your URI strings in order to help minimize the possibility that
13malicious data can be passed to your application. URIs may only contain
14the following:
15
Andrey Andreev16a704c2012-11-09 17:25:00 +020016- Alpha-numeric text (latin characters only)
Derek Jones8ede1a22011-10-05 13:34:52 -050017- Tilde: ~
vlakoff4ad37082013-03-29 18:14:22 +010018- Percent sign: %
Derek Jones8ede1a22011-10-05 13:34:52 -050019- Period: .
20- Colon: :
21- Underscore: \_
22- Dash: -
vlakoff4ad37082013-03-29 18:14:22 +010023- Space
Derek Jones8ede1a22011-10-05 13:34:52 -050024
25Register_globals
26=================
27
28During system initialization all global variables are unset, except
Andrey Andreev16a704c2012-11-09 17:25:00 +020029those found in the ``$_GET``, ``$_POST``, and ``$_COOKIE`` arrays.
30The unsetting routine is effectively the same as
31*register_globals = off*.
Derek Jones8ede1a22011-10-05 13:34:52 -050032
Andrey Andreev16a704c2012-11-09 17:25:00 +020033display_errors
34==============
Derek Jones8ede1a22011-10-05 13:34:52 -050035
Andrey Andreev16a704c2012-11-09 17:25:00 +020036In production environments, it is typically desirable to "disable" PHP's
37error reporting by setting the internal *display_errors* flag to a value
Derek Jones8ede1a22011-10-05 13:34:52 -050038of 0. This disables native PHP errors from being rendered as output,
39which may potentially contain sensitive information.
40
purwandi89f6f1a2011-10-07 19:58:22 +070041Setting CodeIgniter's **ENVIRONMENT** constant in index.php to a value of
42**\'production\'** will turn off these errors. In development mode, it is
Derek Jones8ede1a22011-10-05 13:34:52 -050043recommended that a value of 'development' is used. More information
Andrey Andreev16a704c2012-11-09 17:25:00 +020044about differentiating between environments can be found on the
45:doc:`Handling Environments <environments>` page.
Derek Jones8ede1a22011-10-05 13:34:52 -050046
47magic_quotes_runtime
Andrey Andreev16a704c2012-11-09 17:25:00 +020048====================
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Andrey Andreev16a704c2012-11-09 17:25:00 +020050The *magic_quotes_runtime* directive is turned off during system
Derek Jones8ede1a22011-10-05 13:34:52 -050051initialization so that you don't have to remove slashes when retrieving
52data from your database.
53
54**************
55Best Practices
56**************
57
58Before accepting any data into your application, whether it be POST data
59from a form submission, COOKIE data, URI data, XML-RPC data, or even
60data from the SERVER array, you are encouraged to practice this three
61step 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
68CodeIgniter provides the following functions to assist in this process:
69
70XSS Filtering
71=============
72
73CodeIgniter comes with a Cross Site Scripting filter. This filter
Andrey Andreev16a704c2012-11-09 17:25:00 +020074looks for commonly used techniques to embed malicious JavaScript into
Derek Jones8ede1a22011-10-05 13:34:52 -050075your data, or other types of code that attempt to hijack cookies or
76do other malicious things. The XSS Filter is described
77:doc:`here <../libraries/security>`.
78
79Validate the data
80=================
81
Andrey Andreev16a704c2012-11-09 17:25:00 +020082CodeIgniter has a :doc:`Form Validation Library
83<../libraries/form_validation>` that assists you in
Derek Jones8ede1a22011-10-05 13:34:52 -050084validating, filtering, and prepping your data.
85
86Escape all data before database insertion
87=========================================
88
89Never insert information into your database without escaping it.
Andrey Andreev16a704c2012-11-09 17:25:00 +020090Please see the section that discusses :doc:`database queries
91<../database/queries>` for more information.
Derek Jones8ede1a22011-10-05 13:34:52 -050092
Andrey Andreev16a704c2012-11-09 17:25:00 +020093Hide your files
94===============
Derek Jones8ede1a22011-10-05 13:34:52 -050095
Andrey Andreev16a704c2012-11-09 17:25:00 +020096Another good security practice is to only leave your *index.php*
97and "assets" (e.g. .js, css and image files) under your server's
98*webroot* directory (most commonly named "htdocs/"). These are
99the only files that you would need to be accessible from the web.
100
101Allowing your visitors to see anything else would potentially
102allow them to access sensitive data, execute scripts, etc.
103
104If you're not allowed to do that, you can try using a .htaccess
105file to restrict access to those resources.
106
107CodeIgniter will have an index.html file in all of its
108directories in an attempt to hide some of this data, but have
109it in mind that this is not enough to prevent a serious
110attacker.