MDL Shield

User related debugmode

tool_userdebug

Print Report
Plugin Information

Admin tool that turns on Moodle's debugging features (debug level, display errors, SMTP/IMAP debug, performance info, string identifiers, page info, deprecated-style highlighting) for an admin-selected list of users, or for the current session only ("ad hoc" debugging) for users holding tool/userdebug:adhocdebug. The site-wide debug settings stay unchanged. It works through the after_config hook and overrides $CFG for each request.

Version:2025070400
Release:v5.0.4 (2026112200)
Reviewed for:5.3
Privacy API
Unit Tests
Behat Tests
Reviewed:2026-11-22
27 files·2,244 lines
Grade Justification

The review found no security vulnerabilities. Every state-changing path is protected:

  • adhocdebug.php calls require_sesskey() and require_login(), checks the capability against the real user, and limits the redirect target with PARAM_LOCALURL.
  • index.php is gated by admin_externalpage_setup(), which requires moodle/site:config in core, and its add/remove actions call confirm_sesskey().
  • The settings form is a moodleform, and the user selectors check every submitted ID through core's user_selector_base.
  • Templates escape all user-influenced values. The only raw outputs are core-generated HTML and moodle_url objects, whose query parameters core encodes.

All three findings are low:

  • A functional bug: the return URL is built from an HTML-escaped moodle_url, so query parameters after the first are lost when the user returns.
  • The capability's risk bitmask is too weak to trigger core's safeguard that stops guests from holding risky capabilities.
  • The on/off state labels are untranslatable English literals.

The plugin also ships PHPUnit and Behat coverage for its access controls. None of the findings can be exploited by a low-privilege user without an administrator misconfiguring roles first.

AI Summary

tool_userdebug is a small admin tool (about 10 PHP classes and scripts, one AMD module, four templates). It turns on Moodle's debugging features for chosen users, or for a single session when the user holds tool/userdebug:adhocdebug. It applies the overrides early in each request through the after_config hook, writing to $CFG and $CFG->config_php_settings.

The security posture is good. The toggle script requires a session key and a login, and checks the capability against the real user, so it works correctly in "log in as" sessions. Its return path is limited to local URLs. The settings page is admin-only and handles CSRF correctly. Core's user_selector_base checks every submitted user ID. All template output is escaped or comes from core renderers, and the plugin ships unit and Behat tests covering these controls.

The review found only low-severity issues. Users of the toggle will notice one of them: the return URL is built from an HTML-escaped moodle_url, so toggling ad hoc debugging on a page with several query parameters returns to a different page state. The other change worth making is to give the capability RISK_CONFIG. As declared (read, RISK_PERSONAL only), an administrator could grant it to the guest role by mistake and core would not stop it. Debug display reveals file paths, SQL and stack traces.

Findings

code qualityLow
Return path is HTML-escaped, so query parameters are lost after the toggle

get_settings_node() passes the $PAGE->url object straight in as the returnpath parameter. Core's moodle_url::params() turns parameter values into strings with (string), which calls __toString() → out(true). That joins the query parameters with & rather than &. adhocdebug.php then accepts this value through PARAM_LOCALURL and passes it to new moodle_url(), which reads id=2&section=3 as id=2 plus a junk key amp;section=3. The result is that every query parameter after the first is lost when the user is sent back.

Risk Assessment

Low risk. This is a functional bug with no security impact. PARAM_LOCALURL still stops off-site redirects. Users lose their place on any page whose URL has more than one parameter.

Context

The node URL is used in three places: the navbar icon (render_navbar_output), the user menu item (extend_user_menu hook) and the front page and preferences navigation nodes. Every ad hoc toggle therefore goes through this path.

Identified Code
        $returnpath = $PAGE->url;
        $url        = new \moodle_url(
            '/admin/tool/userdebug/adhocdebug.php',
            [
                'returnpath' => $returnpath,
                'sesskey'    => sesskey(),
            ]
        );
Suggested Fix
        $url = new \moodle_url(
            '/admin/tool/userdebug/adhocdebug.php',
            [
                'returnpath' => $PAGE->url->out(false),
                'sesskey'    => sesskey(),
            ]
        );

With the unescaped URL string, all of the page's parameters survive the round trip. PARAM_LOCALURL still accepts absolute URLs under wwwroot.

Identified Code
$returnpath = optional_param('returnpath', '/', PARAM_LOCALURL);
$returnurl  = new \moodle_url($returnpath ?: '/');
best practiceLow
Ad hoc debug capability's risk flags too weak for core's guest safeguard

tool/userdebug:adhocdebug is declared as captype => 'read' with only RISK_PERSONAL. Core's has_capability() refuses guests and not-logged-in users only when a capability is write or carries RISK_XSS, RISK_CONFIG or RISK_DATALOSS. If an administrator grants this capability to the guest or authenticated-user role by mistake, nothing in core blocks it, and the permissions UI shows no configuration-risk warning. Once enabled, ad hoc debugging can turn on display_errors and DEBUG_DEVELOPER for that session, revealing server file paths, SQL statements with parameters, and stack traces. That is configuration-level exposure, which RISK_CONFIG is meant to signal.

Risk Assessment

Low risk. This needs an administrator to misconfigure roles first; the plugin's default configuration is safe. Adding RISK_CONFIG lets core's built-in safeguard and the admin UI warning protect against that mistake.

Context

adhocdebug.php lets guest sessions through require_login(); the plugin's own test_require_login shows a guest reaching the capability check. So the capability is the only gate before a session can turn debugging on. By default no archetype is granted the capability, so only site administrators hold it.

Identified Code
    'tool/userdebug:adhocdebug' => [
        'riskbitmask'  => RISK_PERSONAL,
        'captype'      => 'read',
        'contextlevel' => CONTEXT_SYSTEM,
    ],
Suggested Fix
    'tool/userdebug:adhocdebug' => [
        'riskbitmask'  => RISK_CONFIG | RISK_PERSONAL,
        'captype'      => 'read',
        'contextlevel' => CONTEXT_SYSTEM,
    ],

Bump the plugin version so the capability definition is updated on existing sites.

code qualityLow
Ad hoc debug state labels "on"/"off" are hardcoded English

The ad hoc debug state is shown to users as "Ad hoc debug (on)" or "Ad hoc debug (off)". The words on and off are passed as English literals into the {$a} placeholder of adhocdebug, both in PHP and in the debuginfo template. Translators can change the surrounding text but not the state words, so non-English sites show a mixed-language label in the navbar icon title, the user menu and the infobox toggle.

Risk Assessment

Low risk. This is a localisation defect only, with no security impact.

Context

Every capable user sees this title on each page, through the navbar icon, the user menu item and the navigation nodes. The infobox template also hardcodes the on state.

Identified Code
        if (static::is_adhoc_debug()) {
            $pixicon  = 'debugon';
            $strstate = 'on';
        } else {
            $pixicon  = 'debugoff';
            $strstate = 'off';
        }
        $title = get_string('adhocdebug', 'tool_userdebug', $strstate);
Suggested Fix

Define two complete strings, for example $string['adhocdebugon'] = 'Ad hoc debug (on)'; and $string['adhocdebugoff'] = 'Ad hoc debug (off)';. Then use:

        $title = get_string(static::is_adhoc_debug() ? 'adhocdebugon' : 'adhocdebugoff', 'tool_userdebug');
Identified Code
    <a id="tool-userdebug-toggle-button" href="#" class="float-end" data-bs-toggle="collapse" data-bs-target="#debugsettings-{{uniqid}}" aria-expanded="false" aria-controls="debugsettings-{{uniqid}}" title="{{#str}}adhocdebug, tool_userdebug, on {{/str}}">
Suggested Fix
title="{{#str}}adhocdebugon, tool_userdebug{{/str}}"
Additional AI Notes

Version number vs. release label. version.php declares $plugin->version = 2025070400, but $plugin->release is 'v5.0.4 (2026112200)', and CHANGES.md lists several releases (v5.0.1 to v5.0.4, including Moodle 5.3 compatibility) that appear to post-date July 2025. I could not confirm the previously published version number. If the version was not bumped for this release, existing sites will not run an upgrade, so caches will not be purged and the updated templates may stay cached. The Moodle plugins directory also rejects uploads that reuse a version number.

Writing to $_GET to force string identifiers. util::enable_debugging() sets $_GET['strings'] = 1. Core shows string identifiers only when $CFG->debugstringids is set and optional_param('strings', 0, PARAM_INT) is true (moodlelib.php, lang_string.php, help_icon.php). Core has no API to force this, so the workaround is understandable. It is still a fragile coupling to core internals, and it affects any other code that reads the strings request parameter.

Privacy declaration. The plugin uses a null_provider, but it keeps a list of user IDs in the tool_userdebug/users config setting. Nothing removes an ID when that user is deleted. This mirrors core's own $CFG->debugusers, which is also not declared in a privacy provider, so I did not report it as a defect. A \core\event\user_deleted observer that prunes the list would keep the config tidy.

Redundant access check in index.php. The has_capability('moodle/site:config', …) check followed by a bare die repeats what admin_externalpage_setup() already enforces: admin_externalpage requires moodle/site:config by default. The check is harmless, but if it were ever reached the user would get a blank page instead of an error.

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