MDL Shield

Restriction by password

availability_password

Print Report
Plugin Information

Availability condition that restricts access to a course activity until the user enters a teacher-defined password. Accepted passwords are remembered either permanently (as salted hashes in a plugin table) or until logout (in the user's session). The plugin provides a YUI popup that submits via an AJAX endpoint, and a non-JS fallback page that uses a moodleform.

Privacy API
Unit Tests
Behat Tests
Reviewed:2026-11-10
27 files·3,043 lines
Grade Justification

The only finding is low severity. Students can submit unlimited password guesses, and the server does not throttle or lock out repeated attempts. The impact is limited to unlocking one activity inside a course the attacker is already enrolled in. Every attempt is logged as a password_rejected event, so abuse can be audited.

Everything else checked out against core:

  • Both entry points call require_login($course).
  • ajax.php enforces require_sesskey(), and index.php uses a moodleform, which handles its own sesskey.
  • Passwords are compared with hash_equals() after a string cast, so type juggling is not possible.
  • Only salted SHA-256 hashes are persisted.
  • All DB access is parameterised.
  • The Privacy API is complete and covered by tests.
  • Cleanup observers handle course, cm and user deletion.
AI Summary

availability_password is a small availability condition plugin of about 1,000 lines of PHP plus two YUI modules. It lets teachers gate an activity behind a password. Students can enter the password in a popup that posts to ajax.php, or on a fallback page (index.php) built on a moodleform. Accepted passwords are remembered as salted SHA-256 hashes, either in a plugin table or in the session.

The security posture is solid:

  • Both endpoints require course login, and the AJAX endpoint checks the sesskey.
  • Password comparison is constant-time and type-safe (hash_equals on strings). Unit tests cover the scientific-notation and loose-equality cases explicitly.
  • An upgrade step migrates earlier cleartext grant records to hashes.
  • The popup reuses the course-page .instancename markup and the server-side language string, so it adds no new XSS sink.
  • Core only exposes the raw availability JSON, which holds the configured password, to users who can view hidden activities or update the course. I confirmed this in course/externallib.php.

The one finding: password submission is not throttled. An enrolled student can script ajax.php to guess a weak password. The cost is that a teacher's restriction on one activity can be bypassed. Each attempt is logged as an event, which makes abuse auditable but does not prevent it.

The code is otherwise clean and well tested, with PHPUnit tests for the condition and the privacy provider plus Behat coverage.

Findings

best practiceLow
No rate limiting on password submissions

ajax.php and index.php accept any number of password attempts from a logged-in, enrolled user, and condition::submit_password_for_cm() does not throttle them. Each failed attempt triggers a password_rejected event, which gives an audit trail but does not stop a scripted attack. Teacher-chosen activity passwords are often short or guessable, so a student can unlock the activity offline-style by looping requests against ajax.php. Both endpoints only require course login (require_login($course, false)), so they also tell you whether a guess is correct for an activity the student cannot otherwise see. This is a weakness of the gate's design, not an injection or authorisation flaw.

Risk Assessment

Low risk. The attacker must already be enrolled in the course, and success only unlocks a single activity that the teacher restricted. It does not expose other users' data or escalate privileges. The logged password_rejected events let teachers or admins detect brute-force attempts after the fact.

Context

The YUI popup posts id, password and sesskey to ajax.php. That endpoint calls submit_password_for_cm(), which compares the guess with each password condition on the cm and records a grant when one matches. The non-JS form in index.php calls the same method.

Identified Code
if (\availability_password\condition::submit_password_for_cm($cm, $password)) {
    $ret->success = 1;
Suggested Fix

Before checking the password, count recent password_rejected attempts for this user and cm (for example, in a small cache or the grant table). Refuse further attempts for a cooling-off period once a threshold is reached, and show a localised error string.

Identified Code
        if ((string) $password !== '') {
            foreach ($pconds as $pcond) {
                if ($pcond->check_password($password)) {
                    $pcond->save_available($cm, $userid);
                    $correct = true;
                }
            }
        }
Suggested Fix

Put the throttle check here so that both ajax.php and index.php are covered, for example by using a \cache keyed by userid and cmid that stores a failed-attempt counter and a timestamp.

Additional AI Notes

The configured password is stored in cleartext inside course_modules.availability, as the availability framework requires. That is why get_debug_string() prints it and why the plugin documents doing so. In core, this JSON reaches web service clients only when the user has moodle/course:viewhiddenactivities or can update the course (course/externallib.php). It is also included in course backups. Teachers should not reuse sensitive passwords here.

Remembered grants hold a deterministic sha256(cmid . ':' . password). Anyone with DB read access could recover a weak password by brute force against this hash. However, the same password is already stored in cleartext in course_modules.availability, so the hash exposes nothing new.

The session-based remember mode keeps hashes on the $USER object ($USER->availability_password). This works because $USER is persisted in the session, but $SESSION would be the more conventional place for per-session plugin state.

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