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 the 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, the public image of the institution may be damaged too. Limiting factors may be the cost of application development, maintenance and 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 systems have to accept rich text messages with file attachments, mail messages often contain very sensitive information. In fact, the Web 2.0 idea goes directly against 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 (DOS)
- 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_course_loginas near the start as possible.
Verify course and module access
- All course areas have to be protected by
- All module areas have to be protected by
- Before allowing the user to see anything or do anything, call to
- 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
setTypemethod 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
required_paramwith an appropriate
PARAM_...type. Do not access
$_REQUESTdirectly. 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
pto output plain text content.
format_stringto output content with minimal HTML like multi-lang spans (for example, course and activity names).
format_textto output all other content.
- Only use
$options->nocleanif it requires a capability with
RISK_XSSto input that content (for example web page resources).
- Only use
$PAGE->requires->data_for_js(Moodle 2.0 onwards) or
We recommend that you follow the Output functions to get a better understanding of how dynamic data should be sent from Moodle to the browser.
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 had to build SQL by concatenating strings. Take particular care, especially with quoting values, to avoid SQL injection vulnerabilities.
addslashesmethod should no longer be use anywhere in Moodle 2.0 onwards.
- 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
Log every request
- Every script should log an event
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 (for instance,
$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. One standard test you can use 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.