This content of this page has been updated and migrated to the new Moodle Developer Resources. The information contained on the page should no longer be seen up-to-date.
This page describes how to write secure Moodle code that is not vulnerable to anything that evil people may 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.
Security of web applications
Secure web app requirements
Some companies require maximum level of security for web applications. You can often see similar general recommendations:
- separate administration backend
- no sensitive information stored in web application
- communication has to be encrypted using SSL
- log all user actions
- server applications have to be completely separated
- no files uploaded by users on server
- no rich text entered by users on server (limited plain text only)
- validate user identity and actions via separate channel
- always keep every software up-to-date
- no 3rd party browser extensions recommended
- use only one web page, do not open multiple windows with different sites, close/open browser before and after using the secure app
Web based banking systems are the best examples of these secure web applications. Security is the top most priority here, security incidents may cost money - either the customer, bank or insurance company, public image of the institution may be damaged too. Limiting factors may be cost of application development, maintenance, usability but also the cost of communication via the alternative channels.
As you can see many web applications today violate the security design rules. For example web based mail system have to accept rich text messages with file attachments, mail massages often contain very sensitive information. In fact the Web 2.0 idea goes directly agains the security design rules, everybody is submitting content - only app designer/administrator should be adding trusted content.
When designing web applications we have to find out what our users are supposed to be doing and then find some reasonable balance between features and security.
Moodle security design
The security of web application depends on the intended use and features available for each type of user.
Types of users
Administrators have following privileges:
- change all settings
- create courses
- access all courses
- modify language packs
- modify all users
Indirectly administrators are allowed to execute shell and PHP code. Moodle administrators may be partially restricted by hardcoding settings in config.php. Low level server administrators can not be restricted because they can read content of PHP files and may modify them.
All administrators have to be fully trusted.
Teachers are usually creating course content, enrolling students and teaching. They usually need following privileges:
- upload files and submit html texts
- create and manage activities
- access student grades and other personal information
Browser trusts everything coming from one server, it does not know anything about our courses or origin of data. Once the file is uploaded to server it becomes part of the server application. It is not possible to differentiate between wanted and unwanted code stored on the server.
All teachers with risky capabilities have to be trusted, it is not possible to give teacher access to students. In theory teachers may use XSS attacks to gain administrator access.
Students are participating in courses, they are not trusted. Students need following privileges:
- post formatted text with inline images and attachments
- upload binary documents
Uploaded files must not be opened directly in browser from the same server. Instead the files need to be served from different domain, or server has to force download of all files to local hard drive before opening them.
For security reasons unregistered users can not be allowed to upload any files or submit any text that is stored in database. Guests could try to spam other users, exploit problems in html cleaning routines, abuse other vulnerabilities or try social engineering based attacks.
Sites with enabled user sign-up via email have to take extra care in order to prevent spamming and other types of attacks.
Moodle is very flexible system, administrators may define multiple roles. Each role is a set of capabilities defined at system level, roles may be modified via overrides at lower context levels. Risks are part of description of each capability, administrators have to make sure only trusted users get potentially dangerous capabilities.
Common types of security vulnerability
- Unauthenticated access
- Unauthorised access
- Cross-site request forgery (XSRF)
- Cross-site scripting (XSS)
- SQL injection
- Command-line injection
- 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.
Verify course and module access
- all course areas have to be protected by "require_login" or "require_course_login" with correct $course parameter
- all module areas have to be protected by "require_login" or "require_course_login" with correct $course and $cm parameter
- Before allowing the user to see anything or do anything, call to has_capability or require_capability.
- Capabilities should be annotated with the appropriate risks.
- If appropriate, restrict what people can see according to groups.
Don't trust any input from users
- Use moodleforms whenever possible, with an appropriate setType method call for each field.
- Before performing actions, use data_submitted() && confirm_sesskey() to check sesskey and that you are handling a POST request.
- 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 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 by 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.)
- In Moodle 2.0 variables must be passed to database queries through bound parameters
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:
< > & < > & ' \' 碁 \ \\
- Security issue process
- The 2010 CWE/SANS Top 25 Most Dangerous Programming Errors - this is a generic list of common security mistakes, produced by a group of security experts. The above Moodle-specific guidelines cover most of these general points.
- PHP Security Cheat Sheet
- The Open Web Application Security Project (OWASP) - Large collection of documentation and code samples about preventing security issues while using web technologies.