MDL Shield

Sandbox

local_sandbox

Print Report
Plugin Information

Admin-only local plugin that periodically resets designated "sandbox" courses to a predefined state. A disabled-by-default scheduled task restores admin-uploaded .mbz course backups (stored in a system-context file area) over the existing course with the matching shortname, using configurable restore settings, and emails configured site administrators about the outcome.

Version:2026100501
Release:v5.3-r2
Reviewed for:5.3
Privacy API
Unit Tests
Behat Tests
Reviewed:2026-11-11
13 files·2,098 lines
Grade Justification

No security vulnerabilities were found. The plugin has no web entry points of its own. Its only user-facing surface is an admin settings page gated by $hassiteconfig, which uses core admin setting classes, including a stored-file setting limited to .mbz files. All processing happens in a scheduled task that uses core APIs throughout: the File API, get_file_packer(), make_backup_temp_directory(), the core restore controller and parameterised $DB calls.

The one finding is a low-severity correctness issue. delete_course() is called without $asyncpreferred = false. On sites with asynchronous course deletion enabled, the old course is only queued for deletion, so for a while it exists next to the recreated course with the same shortname.

The codebase is otherwise clean and well tested. It has PHPUnit coverage of the task's success and failure paths, Behat coverage, a correct null Privacy provider, and no bundled third-party code.

AI Summary

local_sandbox is a small, admin-only plugin of about 600 lines of production PHP. A scheduled task deletes or empties each sandbox course and restores it from an .mbz backup that an administrator uploaded to a system-context file area. The course is matched to the backup by its shortname, which is taken from the filename. Restore options, start-date adjustment and course-ID retention are configured on a single admin settings page.

The security posture is good. The plugin has no standalone pages, AJAX endpoints or web services. Only site administrators can reach the settings, and the backups are accessed through the File API and restored through a thin subclass of core's restore_controller. All database access uses $DB with array conditions. There is no lib.php and no pluginfile callback, so uploaded backups are never served over HTTP.

One finding is worth acting on. On Moodle 5.3, delete_course() defaults to asynchronous deletion when the site enables Delete courses asynchronously. The task does not opt out, so on such sites the old course is only queued for deletion while a new course with the same shortname is created right away. This is a one-argument fix.

The test suite is unusually thorough for a plugin of this size. It covers unzip failures, precheck errors and warnings, exceptions during restore, email thresholds and recipient filtering.

Findings

code qualityLow
delete_course() may defer deletion, leaving duplicate-shortname courses

The task calls delete_course($oldcourseid, false) and then immediately creates a new course with the same shortname. In this Moodle version delete_course() has a third parameter, $asyncpreferred, which defaults to true. When the site setting moodlecourse/enablecourseasyncdeletion is enabled, the function only marks the course as deletion in progress, queues a course_async_deletion adhoc task, and returns true. The plugin treats that as a completed deletion and calls restore_dbops::create_new_course(), which inserts a second course record with the same shortname. The course.shortname index is not unique, so nothing prevents this.

Risk Assessment

Low risk. This only happens on sites that turn on asynchronous course deletion, and it fixes itself once the adhoc task runs. Until then, two courses with the same shortname exist in the same category. If the adhoc task has not run before the next sandbox run, $DB->get_record('course', ['shortname' => ...]) matches several records, and the task can emit a debugging warning or pick the course that is being deleted.

Context

When Keep course ID is disabled (the default), the task deletes the existing course, creates a skeleton course with restore_dbops::create_new_course($shortname, $shortname, $categoryid) and restores the backup into it. Core's delete_course() in lib/moodlelib.php returns early after queuing the adhoc task whenever $asyncpreferred is true and async deletion is enabled on the site.

Identified Code
if (!delete_course($oldcourseid, false)) {
Suggested Fix

Force synchronous deletion, as the task depends on the old course being gone before it recreates it:

if (!delete_course($oldcourseid, false, false)) {
Additional AI Notes

Notifications bypass the Message API. local_sandbox_inform_admin() sends mail directly with email_to_user() rather than declaring a message provider in db/messages.php. Recipients are restricted to users with moodle/site:config, so this is not a security concern. However, administrators cannot route or mute these notifications through their messaging preferences. A message provider would be the more idiomatic choice.

Destructive ordering is by design. The existing course is deleted or emptied before the backup is unzipped and pre-checked. A corrupt or incompatible backup therefore leaves an empty course behind. The plugin documents this in its skippingprecheckfailed string and tests it, so it is not reported as a defect. Administrators should still validate their backups before enabling the task.

Unused variable in a legacy upgrade step. In db/upgrade.php, the 2018020902 step collects legacy file paths in $todelete but never uses them, and the strpos($file, '.mbz') check also matches names that only contain .mbz. This step only runs when upgrading from very old plugin versions, so the practical impact is negligible.

This review was generated by an AI system and may contain inaccuracies. Findings should be verified by a human reviewer before acting on them.