DEV Community

Luuk Verhoeven
Luuk Verhoeven

Posted on

Prove Moodle dataroot is not on the public hostname

A Moodle™ install that looks healthy on /login/index.php can still be serving student submissions, session files and backup zips from a directory the browser can reach. The directory is $CFG->dataroot. Moodle stores uploaded files there on purpose. The web server is not supposed to.

The official check is Site administration → Reports → Security checks. That report is a start. It is not proof. The proof is a request from the public hostname that either 404s or is refused, against a file you know exists on disk.

What dataroot actually holds

On a typical production host:

/var/moodledata/filedir/     # hashed copies of every uploaded file
/var/moodledata/sessions/    # PHP session files if you still use file sessions
/var/moodledata/temp/        # backups, imports, unzip leftovers
/var/moodledata/cache/       # MUC file store
/var/moodledata/localcache/
/var/moodledata/trashdir/
Enter fullscreen mode Exit fullscreen mode

If that tree sits under the document root — public_html/moodledata, www/data, htdocs/moodledata — a guessable path plus directory listing, or a known hash, bypasses every capability check Moodle runs in pluginfile.php. The file is just a static object. No sesskey. No login.

Moodle 5.1 moved most of the code into a public/ subdirectory and asks you to point Apache DocumentRoot or nginx root at that folder (restructure notes). That change does not move dataroot. Dataroot is still $CFG->dataroot in config.php. A 5.1 upgrade that only flips the document root can leave a 4.x moodledata folder sitting next to public/ and still reachable if the vhost was not tightened.

Find the path, then try to fetch it

On the server:

php -r 'define("CLI_SCRIPT", true); require "config.php"; echo $CFG->dataroot, PHP_EOL, $CFG->dirroot, PHP_EOL, $CFG->wwwroot, PHP_EOL;'
Enter fullscreen mode Exit fullscreen mode

On Moodle 5.1 you also have $CFG->root (the install root, parent of public/). Compare three things:

  1. Document root of the vhost (grep -R "root \|DocumentRoot" /etc/nginx /etc/apache2).
  2. $CFG->dataroot.
  3. Whether (2) is a child of (1).

If dataroot is /var/www/moodle/moodledata and the vhost root is /var/www/moodle or /var/www/moodle/public, stop. Move it.

Place a canary the web server would serve if the tree were public:

CANARY="$CFG_DATAROOT/web-canary-$(date +%Y%m%d).txt"
# run as the deploy user who can write dataroot
echo "not-a-public-file" > /var/moodledata/web-canary-$(date +%Y%m%d).txt
chmod 600 /var/moodledata/web-canary-$(date +%Y%m%d).txt
Enter fullscreen mode Exit fullscreen mode

From a laptop that is not on the campus VPN allow-list:

HOST=lms.example.org

# Common leftover names. Every 200 here is an incident.
for p in \
  /moodledata/ \
  /data/ \
  /moodledata/filedir/ \
  /moodledata/web-canary-$(date +%Y%m%d).txt \
  /filedir/ \
  /public/../moodledata/ \
  ; do
  echo "=== $p ==="
  curl -sI "https://$HOST$p" | grep -iE 'HTTP/|location:|content-type|x-content-type'
done
Enter fullscreen mode Exit fullscreen mode

What you want: 404 from Moodle’s theme, or a 403 from the edge that does not list the directory. What you do not want: 200 with text/html (listing), 200 with text/plain (the canary), or a 301 into a path that then 200s.

Path traversal in the URL (/public/../moodledata/) is the upgrade-specific case. nginx alias plus a missing trailing slash, or Apache Alias /moodle /srv/moodle/public without a matching directory block, will serve the parent. The 5.1 restructure makes that class of bug more likely, not less, because people copy a new root line and leave an old alias.

Do not trust a 403 from the app

A 403 from PHP means Moodle’s router saw the request. That is already too far. Dataroot should never hit PHP. The edge should not map that URL to a file.

Also test the raw object store layout:

# Moodle hashes uploads as filedir/ab/cd/<contenthash>
curl -sI "https://lms.example.org/moodledata/filedir/"
curl -sI "https://lms.example.org/data/filedir/"
Enter fullscreen mode Exit fullscreen mode

If either is 200 or 403-with-body, an attacker with one contenthash (from an HTML export, an email, a backup) fetches the file without a Moodle session.

Move it without breaking pluginfile.php

Moodle serves those files through pluginfile.php and draftfile.php on purpose. Those scripts look at $CFG->dataroot on disk. The public URL never contains the filesystem path.

# downtime window. Drain php-fpm workers first.
sudo systemctl stop php8.3-fpm
sudo mv /var/www/moodle/moodledata /var/moodledata
# config.php
# $CFG->dataroot = '/var/moodledata';
sudo chown -R www-data:www-data /var/moodledata
sudo find /var/moodledata -type d -exec chmod 700 {} \;
sudo find /var/moodledata -type f -exec chmod 600 {} \;
sudo systemctl start php8.3-fpm
Enter fullscreen mode Exit fullscreen mode

Then:

curl -sI "https://lms.example.org/moodledata/" | head -n 1
# fetch a known course file the way a student would, via pluginfile.php, while logged in
Enter fullscreen mode Exit fullscreen mode

Site administration → Reports → Security checks should stop complaining about dataroot. Re-run the canary curls from outside. Delete the canary.

On nginx, keep dataroot off every location. Do not alias /moodledata. Do not try_files into $CFG->dataroot.

Moodle 5.1: two roots, one mistake

After 5.1:

Variable Typical value Browser-visible
$CFG->root /srv/moodle no, if document root is public/
$CFG->dirroot /srv/moodle/public yes
$CFG->dataroot /var/moodledata no
$CFG->wwwroot https://lms.example.org yes

A host that sets root /srv/moodle; instead of root /srv/moodle/public; publishes config.php, vendor/, and anything you parked next to the project — including dataroot if you put it there “to keep the backup simple”.

curl -sI https://lms.example.org/config.php
curl -sI https://lms.example.org/vendor/autoload.php
curl -sI https://lms.example.org/composer.json
Enter fullscreen mode Exit fullscreen mode

config.php 200 is a different incident from dataroot 200. Both are “the document root is too high”. Fix the vhost, then prove dataroot again. The security-checks report will mention public/private paths; the curls are what you keep in the change ticket.

I built GuardLMS to watch Moodle hosts for this class of exposure between pentests. Disclosure: I am the founder. The canary file and the curls above are still the check you run once, on staging, before any monitor is useful.

Top comments (0)