Running AI-written Moodle plugins safely: what we learned
AI builders now write Moodle plugin code, and someone has to run it. How we test untrusted plugins in gVisor with no network, and the two host-side holes we found in ordinary code checks.
Short answer: treat a plugin written by an AI model, or by anyone you have not vetted, as untrusted code. Install and test it in a container that has no network and runs under a sandboxing runtime such as gVisor. Then check that the tools you run outside the container never execute anything from the plugin’s folder. That second part is where we found real holes.
We run the build server behind DevLMS Studio, where a model writes a Moodle plugin and every version is installed and tested in a real Moodle before anyone downloads it. This post is what we learned putting that server on the public internet this week.
Why is AI-written plugin code a security problem?
A Moodle plugin is PHP that runs with the full rights of the Moodle site: it can read config.php, query any table and open network connections. When a person writes a plugin, you trust the person. When a model writes it, from a prompt that anyone can type, you cannot. A prompt can ask for something harmful outright, or hide the request inside an innocent-looking spec.
This is not only a concern for AI tools. Agencies run CI on plugins clients send them, and reviewers install plugins from strangers to test them. The same rules apply.
What does the sandbox look like?
Every check and every test Moodle the builder uses runs in its own container, with:
- No network (
--network none). The plugin cannot call out, and nothing can call in. The server talks to the container only throughdocker execanddocker cp. - gVisor (
--runtime runsc). gVisor is a user-space kernel from Google: the container’s system calls go to gVisor instead of the host’s Linux kernel, so a kernel bug is much harder to reach from inside.1 - Caps on memory, CPU and the number of processes, and no privilege gain (
no-new-privileges). - The plugin folder mounted read-only. Code running inside can read its own files but cannot add a file or a link to the folder that a later step on the host would read.
Inside, the check does what a careful reviewer would: install the plugin on a fresh Moodle, open every page with debugging on, run PHPUnit and Behat, and run the coding standard.
What does gVisor cost?
Less than we expected. When we benchmarked it in early November on a 6-core server:
| Plain Docker (runc) | gVisor (runsc) | |
|---|---|---|
| Moodle page, 95th percentile | 13 ms | 35 ms |
| A plugin’s PHPUnit run | 5 s | 9 s |
| Idle memory per Moodle | 72 MiB | 166 MiB |
On the live server this week, a test Moodle with the plugin installed is ready in about 3 seconds, and a full check (install, pages, a scripted walkthrough per role, PHPUnit, Behat and code checks) takes two and a half to three and a half minutes. Slower page loads do not matter to a test run. The memory does, if you run many at once.
The gVisor setting that broke every check
Our first checks under gVisor all failed with “Could not find the file /tmp/pages”. The check writes its results inside the container, and the server copies them out with docker cp.
By default, gVisor keeps the container’s own file changes in an overlay that only the sandbox can read: a temporary file system stored in a single file, not as ordinary files on the host. That is faster, but docker cp reads the container’s files from the host, so it saw none of what the check had written. gVisor’s documentation says to turn the overlay off with --overlay2=none when changes must reach the host. With that set for the runsc runtime in Docker’s daemon.json, the checks passed.2 If you put an existing test pipeline under gVisor and files go missing, look there first.
Two holes outside the sandbox
The sandbox only protects what runs inside it. Some checks run on the host, because they only read files: our coding-standard step runs PHP_CodeSniffer, and our check script lists the plugin’s files first. We reviewed every host-side step against one question: can anything in the plugin’s folder make this tool run code? Two could.
A .git folder shipped with the plugin. Our script used git ls-files to list files when the folder was a Git repository. Git reads .git/config from the folder it runs in, and some settings in that file name a program for Git to run. A plugin that includes its own .git folder could therefore run a command on the build server. Git’s own documentation warns about running Git in untrusted repositories.3 The fix: on untrusted code, list files with find and never run Git.
A file named like an option. The script passed every .php file name to phpcs. A file whose name looks like a command-line option is read as an option, and PHP_CodeSniffer has an option that loads a PHP file before it starts.4 So a plugin with the right two file names could have made the coding standard check run its PHP on the host. The fix: put ./ in front of every path, so a file name can never start with a dash.
We wrote a test that builds a plugin with both tricks and fails if either one runs, before fixing anything, and watched it fail. Both now pass on the live server.
Neither hole needs an AI to exploit it. Any CI job that runs Git or PHP_CodeSniffer over plugin code from someone else has the same exposure, so it is worth checking yours.
What we would check in any plugin pipeline
- Install and test in a container with no network, under gVisor or a micro-VM if you can.
- List every step that runs outside the container, and ask whether a file in the plugin can change what that tool does: config files it reads from the folder, file names it receives as arguments, scripts it runs. Mount the plugin read-only, so the code under test cannot add such files while it runs.
- Never run Git on an untrusted folder, or run it with the repository’s config ignored.
- Pass file names after
./(or after--where the tool supports it). - Keep secrets off the machine that runs checks where you can. Ours shares the box with the studio for now, which is why the host-side review mattered.
Most plugin authors run moodle-plugin-ci, the standard Moodle plugin test tool, in GitHub Actions, where every job gets a throwaway virtual machine. That is the right instinct. If you run checks on your own servers, that isolation is your job.
If you want a plugin built and tested this way, or an existing one reviewed, ask us for a fixed price.
Sources
-
gVisor documentation, What is gVisor?. ↑
-
gVisor documentation, Filesystem, on the root filesystem overlay and the
--overlay2flag. ↑ -
Git documentation, git-config, sections on
safe.directoryand on settings that run commands. ↑ -
PHP_CodeSniffer wiki, Advanced usage, on the
--bootstrapoption. ↑
Need a Moodle plugin built or rescued?
Tell the scoping assistant what you need. You get a fixed price and a timeline in a few minutes, and an engineer reviews it within one business day.
Get a fixed quote