Development:Security: Difference between revisions
From MoodleDocs
No edit summary |
|||
Line 16: | Line 16: | ||
* [[Development:Security:Unauthorised access|Unauthorised access]] | * [[Development:Security:Unauthorised access|Unauthorised access]] | ||
* [[Development:Security:Cross-site_request_forgery|Cross-site request forgery]] (XSRF) | * [[Development:Security:Cross-site_request_forgery|Cross-site request forgery]] (XSRF) | ||
* Cross-site scripting (XSS) | * [[Development:Security:Cross-site scripting|Cross-site scripting]] (XSS) | ||
* SQL injection | * [[Development:Security:SQL injection|SQL injection]] | ||
* Command-line injection | * [[Development:Security:Command-line injection|Command-line injection]] | ||
* Data-loss | * [[Development:Security:Data-loss|Data-loss]] | ||
* Confidential information leakage | * [[Development:Security:Confidential information leakage|Confidential information leakage]] | ||
* Configuration information leakage | * [[Development:Security:Configuration information leakage|Configuration information leakage]] | ||
* Session fixation | * [[Development:Security:Session fixation|Session fixation]] | ||
* [[Development:Security:Denial of service|Denial of service]] | * [[Development:Security:Denial of service|Denial of service]] | ||
* [[Development:Security:Brute-forcing login|Brute-forcing login]] | * [[Development:Security:Brute-forcing login|Brute-forcing login]] | ||
Line 28: | Line 28: | ||
* [[Development:Security:Buffer overruns, and other platform weaknesses|Buffer overruns, and other platform weaknesses]] | * [[Development:Security:Buffer overruns, and other platform weaknesses|Buffer overruns, and other platform weaknesses]] | ||
* [[Development:Security:Social engineering|Social engineering]] | * [[Development:Security:Social engineering|Social engineering]] | ||
==Summary of the guidelines== | ==Summary of the guidelines== |
Revision as of 15:08, 6 November 2009
This page describes how to write secure Moodle code that is not vulnerable to anything that evil people my try to throw at it.
The page is organised around the common types of security vulnerability. For each one, it explains
- what the danger is,
- how Moodle is designed to avoid the problem,
- what you need to do as a Moodle developer to keep your code secure, and
- what you can do as an administrator, to make your Moodle more secure.
The explanation of each vulnerability is on a separate page, linked to in the list below.
This page also summarises all the key guidelines.
Common types of security vulnerability
- Unauthenticated access
- Unauthorised access
- Cross-site request forgery (XSRF)
- Cross-site scripting (XSS)
- SQL injection
- Command-line injection
- Data-loss
- Confidential information leakage
- Configuration information leakage
- Session fixation
- Denial of service
- Brute-forcing login
- Insecure configuration management
- Buffer overruns, and other platform weaknesses
- Social engineering
Summary of the guidelines
Authenticate the user
- With very few exceptions, every script should call require_login or require_course_login as near the start as possible.
Check permissions
- Before allowing the user to see anything or do anything, call to has_capability or require_capability.
- If appropriate, restrict what people can see according to groups.
Don't trust any input from users
- Use moodleforms whenever possible.
- Before performing actions, use is_post_with_sesskey to check sesskey and that you are handling a POST request.
- In Moodle 1.9, use data_submitted() && confirm_sesskey() instead.
- Before destroying large amounts of data, add a confirmation step.
- If not using a moodleform, clean input using optional_param or required_param with an appropriate PARAM_... type.
- Do not access $_GET, $_POST or $_REQUEST directly.
- Group optional_param and required_param calls together at the top of the script, to make them easy to find.
Similarly, clean data from other external resources like RSS feeds before use.
Clean and escape data before output
- Use s or p to output plain text content.
- use format_string to output content with minimal HTML like multi-lang spans (for example, course and activity names).
- Use format_text to output all other content.
- Only use $options->noclean if it requires a capability with RISK_XSS to input that content (for example web page resources).
- Before Moodle 2.0, input from optional_param or required_param must have stripslashes or stripslashes_recursive applied if it is being output directly, rather than being stored in the database.
See Development:Output functions for more details.
Escape data before storing it in the database
- Use the XMLDB library. This takes care of most escaping issues for you.
- When you must use custom SQL code, use place-holders to insert values into the queries.
- Before Moodle 2.0, you have to build SQL be concatenating strings. Take particular care, especially with quoting values, to avoid SQL injection vulnerabilities.
- Before Moodle 2.0, data loaded from the database must have addslashes or addslashes_object applied to it before it can be written back to the database. (addslashes should no longer be use anywhere in Moodle 2.0 code.)
Escape data before using it in shell commands
- Avoid shell commands if at all possible.
- Look to see if there is a PHP library instead.
- If you can't avoid shell commands, use escapeshellcmd and escapeshellarg.
Log every request
- Every script should call add_to_log.
Other good practice
... that helps with security.
- Structure your code nicely, minimising the use of global variables. This makes the flow of data, and hence security, easier to verify.
- Initialise objects ($x = new stdClass;) and arrays ($x = array()) before you first use them.
- Test every input field with tricky input to ensure that it is escaped and un-escaped the right number of times everywhere, and that Unicode characters are not corrupted. My standard test input is:
< > & < > & ' \' 碁 \ \\