Summary
Moodle's external database enrolment plugin does not sufficiently sanitize data before using it in queries against the connected database. When the non-default plugin is enabled, an authenticated user with course editing permissions may be able to inject SQL and view, modify, or delete records in that database.
Affected context
The issue occurs in Moodle's external database enrolment plugin when it builds queries using insufficiently sanitized data. Exposure depends on the plugin being enabled and connected to an external database; the described attack also requires an authenticated account with course editing permissions.
Exploit preconditions
- Attack surface: Queries issued by the external database enrolment plugin
- Required access: An authenticated account with course editing permissions
- Trigger point: Processing data that is incorporated into a query against the external database
- Why exploitation is possible: Insufficient sanitization may allow supplied data to alter query structure
- Deployment condition: The non-default plugin must be enabled and connected to an external database
Why this happens
The plugin insufficiently sanitizes data used in external database queries. If input is incorporated into SQL in a way that allows it to change the query's intended structure, an attacker may be able to perform SQL injection. This behavior maps to CWE-89: Improper Neutralization of Special Elements used in an SQL Command.
Potential impact
- An attacker may view sensitive records in the connected external database.
- An attacker could modify or delete records, depending on the queries and database permissions available to the plugin.
- Data integrity or availability in systems relying on the external database may be affected.
General mitigation guidance
Defensive approaches generally focus on ensuring input cannot change SQL query structure and limiting the consequences of a compromised query.
- Use parameterized queries or prepared statements rather than constructing SQL from input.
- Validate inputs against expected formats and reduce reliance on user-controlled data in database operations.
- Apply least-privilege permissions to the database account used by the plugin.
- Keep the plugin disabled when it is not needed, and restrict access to course editing capabilities to appropriate users.
- Separate sensitive data and operations where practical to limit the impact of database access.
How teams detect issues like this
- Use static analysis to identify query construction that concatenates or interpolates data into SQL.
- Review database access code for parameter binding, input validation, and appropriate use of database APIs.
- Monitor application and database activity for unexpected query patterns or unusual access, modification, and deletion of records.
- Enforce secure coding policies that require parameterized queries and least-privilege database credentials.
- Automated code review tools may help identify unsafe query construction patterns.

