blob: 984ca840b21eb6825f182e34dab8d38978f2bcdd [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: ~
18- Period: .
19- Colon: :
20- Underscore: \_
21- Dash: -
Andrey Andreev16a704c2012-11-09 17:25:00 +020022- Pipe: |
Derek Jones8ede1a22011-10-05 13:34:52 -050023
24Register_globals
25=================
26
27During system initialization all global variables are unset, except
Andrey Andreev16a704c2012-11-09 17:25:00 +020028those found in the ``$_GET``, ``$_POST``, and ``$_COOKIE`` arrays.
29The unsetting routine is effectively the same as
30*register_globals = off*.
Derek Jones8ede1a22011-10-05 13:34:52 -050031
Andrey Andreev16a704c2012-11-09 17:25:00 +020032display_errors
33==============
Derek Jones8ede1a22011-10-05 13:34:52 -050034
Andrey Andreev16a704c2012-11-09 17:25:00 +020035In production environments, it is typically desirable to "disable" PHP's
36error reporting by setting the internal *display_errors* flag to a value
Derek Jones8ede1a22011-10-05 13:34:52 -050037of 0. This disables native PHP errors from being rendered as output,
38which may potentially contain sensitive information.
39
purwandi89f6f1a2011-10-07 19:58:22 +070040Setting CodeIgniter's **ENVIRONMENT** constant in index.php to a value of
41**\'production\'** will turn off these errors. In development mode, it is
Derek Jones8ede1a22011-10-05 13:34:52 -050042recommended that a value of 'development' is used. More information
Andrey Andreev16a704c2012-11-09 17:25:00 +020043about differentiating between environments can be found on the
44:doc:`Handling Environments <environments>` page.
Derek Jones8ede1a22011-10-05 13:34:52 -050045
46magic_quotes_runtime
Andrey Andreev16a704c2012-11-09 17:25:00 +020047====================
Derek Jones8ede1a22011-10-05 13:34:52 -050048
Andrey Andreev16a704c2012-11-09 17:25:00 +020049The *magic_quotes_runtime* directive is turned off during system
Derek Jones8ede1a22011-10-05 13:34:52 -050050initialization so that you don't have to remove slashes when retrieving
51data from your database.
52
53**************
54Best Practices
55**************
56
57Before accepting any data into your application, whether it be POST data
58from a form submission, COOKIE data, URI data, XML-RPC data, or even
59data from the SERVER array, you are encouraged to practice this three
60step approach:
61
62#. Filter the data as if it were tainted.
63#. Validate the data to ensure it conforms to the correct type, length,
64 size, etc. (sometimes this step can replace step one)
65#. Escape the data before submitting it into your database.
66
67CodeIgniter provides the following functions to assist in this process:
68
69XSS Filtering
70=============
71
72CodeIgniter comes with a Cross Site Scripting filter. This filter
Andrey Andreev16a704c2012-11-09 17:25:00 +020073looks for commonly used techniques to embed malicious JavaScript into
Derek Jones8ede1a22011-10-05 13:34:52 -050074your data, or other types of code that attempt to hijack cookies or
75do other malicious things. The XSS Filter is described
76:doc:`here <../libraries/security>`.
77
78Validate the data
79=================
80
Andrey Andreev16a704c2012-11-09 17:25:00 +020081CodeIgniter has a :doc:`Form Validation Library
82<../libraries/form_validation>` that assists you in
Derek Jones8ede1a22011-10-05 13:34:52 -050083validating, filtering, and prepping your data.
84
85Escape all data before database insertion
86=========================================
87
88Never insert information into your database without escaping it.
Andrey Andreev16a704c2012-11-09 17:25:00 +020089Please see the section that discusses :doc:`database queries
90<../database/queries>` for more information.
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Andrey Andreev16a704c2012-11-09 17:25:00 +020092Hide your files
93===============
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Andrey Andreev16a704c2012-11-09 17:25:00 +020095Another good security practice is to only leave your *index.php*
96and "assets" (e.g. .js, css and image files) under your server's
97*webroot* directory (most commonly named "htdocs/"). These are
98the only files that you would need to be accessible from the web.
99
100Allowing your visitors to see anything else would potentially
101allow them to access sensitive data, execute scripts, etc.
102
103If you're not allowed to do that, you can try using a .htaccess
104file to restrict access to those resources.
105
106CodeIgniter will have an index.html file in all of its
107directories in an attempt to hide some of this data, but have
108it in mind that this is not enough to prevent a serious
109attacker.