🕹️PlayerPuzzle
mod_playerpuzzle
PlayerPuzzle (mod_playerpuzzle) is a gamified activity module: a turn-based match-3 RPG where students fight opponents and answer questions from the activity's own question bank (entered by hand, imported from the Moodle question bank, or generated by AI). It has Campaign and Single Match modes, a Demo practice mode, a PuzzleCoin shop, hero upgrades, rankings, a teacher report, gradebook and completion integration, optional block_playerhud economy integration, and a server-side deterministic replay that checks the outcome of each match.
I found no exploitable vulnerability with real-world impact. Every page calls require_login() with the course module and checks the right capability. Every state-changing action is protected: moodleforms, sesskey on the custom POST pages, or AJAX web services that validate their context. All SQL uses placeholders, and sort and filter values are checked against allow-lists. Teacher-authored question and answer HTML goes through format_text() with default cleaning before it reaches students. Templates escape their values, and the few unescaped values are already-formatted HTML or moodle_url output.
The game's economy and grading are well protected. Server-side locks guard the attempt and stock rows. Tokens rotate and are tied to the user and the instance. A deterministic server replay re-derives damage and coins instead of trusting what the client reports. Guests are kept to the Demo throughout.
The worst issue is low: in Demo mode, the gate that stops a client from drawing questions without playing is turned off. Any viewer, guests included, can script a dump of the activity's approved questions with their correct answers. The impact is limited because the game already shows correct answers to players after a wrong answer and in the post-match review. The other two findings are a performance concern (the ranking and the report rebuild the whole class's data on every request) and a bundled non-English language pack. The privacy provider, backup/restore, course reset and the test suite (about 880 PHPUnit tests plus Behat and Jest) are all thorough.
mod_playerpuzzle is a large activity module: about 100 production PHP classes and pages, 27 AMD modules, 24 Mustache templates and roughly 34,000 lines of tests. It runs a match-3 RPG. The server holds the authoritative state and checks each match with a deterministic replay, and the module covers gradebook, completion, backup/restore, privacy and course reset. The engineering is careful throughout. Locks guard attempt and stock rows. Tokens rotate on every resume. Client-sent question ids and client-reported totals are never trusted. Guests are kept to the Demo.
Overall the security posture is strong. Every entry point I traced checks login, capability and sesskey or web-service context correctly. All SQL is parameterised, and teacher-authored rich text is cleaned with format_text() before students see it. I found nothing above low severity.
The one finding that touches security is in the Demo. The real-attempt gate that refuses a fresh question draw unless the replayed move log has reached one is skipped for Demos. A script can therefore alternate draw_question and validate_answer to list every approved question with its correct answer. Guests can do this too, since the Demo is open to them. The game reveals correct answers to players by design anyway, so this mainly matters where teachers import questions from bank categories that graded quizzes also use.
The other findings are lower priority. The ranking and the teacher report recompute the whole class's data on every request, which will hurt large courses. A Brazilian Portuguese language pack is bundled, which the plugin directory asks authors not to do. Some integrity checks are delegated to block_playerhud and local_aihub, which were not available for review (see the notes).
Findings
In a Demo attempt, draw_question::is_question_due() returns true unconditionally. So draw_question hands out a fresh question as soon as validate_answer has consumed the previous one, with no board play in between. validate_answer returns correctanswerid for any wrong answer (including answerid 0). It also records the question in playerpuzzle_attempt_questions, so the next draw skips it. A client can therefore list every approved question of the activity, with its text, options and correct answer, at two web-service calls per question. This contradicts the draw_question docblock, which says such a loop cannot bulk-harvest the bank. For real attempts that guarantee does hold, because the draw is gated on the server replay of the move log.
Low risk. The game already shows correct answers to players after a wrong answer and in the post-match review, so a determined student could collect them by hand through repeated Demos. Scripting only removes the effort. The exposure matters more when teachers import questions from question bank categories that graded quizzes also use, and on courses with guest access, where anyone who can log in as guest can pull the answer key.
Real attempts are protected by replay::snapshot(): a fresh question is only drawn when the stored move log has actually triggered one, and the draw pool for a Campaign phase excludes questions already answered. A Demo is exempt from the replay because it grants no coins, grade or completion, but it draws from the same pool of approved questions that graded attempts use.
- As any user with
mod/playerpuzzle:view(a course guest works too), POST/mod/playerpuzzle/play.php?id=CMIDwithsesskey=…&isdemo=1and readtokenfrom thedata-configattribute. - Repeat until
availableis false:
[{"index":0,"methodname":"mod_playerpuzzle_draw_question","args":{"cmid":CMID,"token":"TOKEN"}}]
[{"index":0,"methodname":"mod_playerpuzzle_validate_answer","args":{"cmid":CMID,"token":"TOKEN","answerid":0,"forwhom":"player"}}]
Each pair returns the question text, its options and correctanswerid. mod_playerpuzzle_get_phase_questionlog then also returns the answer key as text.
private static function is_question_due(\stdClass $attempt, \stdClass $playerpuzzle): bool {
if (!empty($attempt->isdemo)) {
return true;
}
Cap how many questions one Demo may resolve, and throttle how often a user can start a new Demo:
/** @var int Most questions one Demo may draw: enough to practise, too few to dump the bank. */
private const DEMO_MAX_QUESTIONS = 10;
private static function is_question_due(\stdClass $attempt, \stdClass $playerpuzzle): bool {
if (!empty($attempt->isdemo)) {
return count(question_results::decode($attempt->questionresults)) < self::DEMO_MAX_QUESTIONS;
}
// ...
}
if (!$correct && $correctanswerid !== null) {
$result['correctanswerid'] = $correctanswerid;
}
No change is needed for real attempts, where revealing the answer after a wrong choice is a game feature. Rely on the Demo draw cap above. Optionally, also omit correctanswerid for a Demo whose cap has been reached.
When the ranking is on (the default, show_ranking = 1), every Lobby visit by any student calls ranking_service::get_ranking(). That runs report_service::get_student_pool(): get_enrolled_users() for the whole course plus get_users_by_capability(). It then loads every real attempt of every student and scores them all in PHP. The same work is repeated at the end of each Campaign through campaign_end::summary(). report.php is heavier still: it rebuilds the student pool five times and loads all students' attempts three times per request, because each report_service method computes everything on its own. Nothing is cached, so the cost grows with class size times attempts and is paid on the busiest page of the activity.
Low risk. This is a scalability concern, not a vulnerability. In large courses (thousands of students with many attempts) the Lobby page and the report will slow down noticeably and add database load each time a student opens the activity.
report_service::get_student_pool() is shared by the teacher report and the student ranking, so both apply the same enrolment and separate-groups rules. Every caller rebuilds the pool from scratch.
$ranking = ranking_service::get_ranking($instance, $cm, $userid);
Cache the computed ranking entries in a MUC application cache (add db/caches.php), keyed by instance and group. Invalidate it when an attempt finishes or a phase is won (save_progress, advance_phase, match_abandon, campaign_giveup, course reset).
$records = $DB->get_records_select(
'playerpuzzle_attempts',
"playerpuzzleid = :playerpuzzleid AND isdemo = 0 AND userid $insql",
$params,
'id ASC',
'id, userid, currentlevel, currentphase, campaign, status, questions_correct, questions_total, '
. 'frozenmaxlevels, timecreated, timefinished'
);
Serve this from the cache described above rather than querying it on every Lobby view.
$missedquestions = report_service::get_most_missed_questions($instance, $cm, $context, $viewerid);
$distribution = report_service::get_score_distribution($instance, $cm, $context, $viewerid);
$completion = report_service::get_completion_rate($instance, $cm, $context, $viewerid);
$difficulty = report_service::get_difficulty_breakdown($instance, $cm, $context, $viewerid);
Compute get_student_pool() and load_attempts_by_user() once in report.php (or a single report_service::build() method), then pass the results to each section.
The plugin ships lang/pt_br/playerpuzzle.php, a full Brazilian Portuguese translation of all 709 strings, alongside the English pack. The Moodle plugin contribution checklist says that only the English strings should ship with a plugin; other translations go to AMOS (lang.moodle.org) once the plugin is approved. A bundled translation also overrides the community-maintained AMOS pack and can drift from the English strings.
Low risk. This is a packaging and plugin-directory compliance item with no functional or security impact.
Both language files define the same 709 keys. The English pack is complete on its own.
Remove lang/pt_br/ from the plugin and contribute the translation through AMOS after the plugin is approved in the plugins directory.
| Library | Version | License | Declared |
|---|---|---|---|
King's Feast (RandomMind) Background music (pix/music.mp3) | — | CC0 | ✓ |
RPG sounds (Kenney) Swap sound effect (pix/swap.mp3) | — | CC0 | ✓ |
Interface Sounds (Kenney) Match sound effect (pix/match.mp3) | — | CC0 | ✓ |
RPG sounds (Kenney) Hit sound effect (pix/hit.mp3) | — | CC0 | ✓ |
Reliance on code that was not available for review. The PlayerHUD integration hands its integrity checks to \block_playerhud\local\external_items. That covers the item-ownership check in get_name_plain() and belongs_to_instance(), the balance check and locking in consume() used by transfer_hud_coins and the retry cost, and grant(). AI generation calls \local_aihub\ai::generate_text() before falling back to core_ai. Neither plugin is in the review environment. The plugin's side of both contracts is sound: it always passes the course's own block instance id, rejects non-positive amounts, and runs these calls under its own stock lock. Whether the external code enforces its side was not verified.
Ranking visibility. The class ranking is on by default and shows classmates' full names and points to every student. It respects separate groups and moodle/site:viewfullnames, and each student can choose to appear anonymously, but it does not check moodle/course:viewparticipants. Sites with strict privacy expectations may want teachers to turn show_ranking off, or may prefer anonymous-by-default.
Strengths worth noting. Attempt economics are well protected. Per-attempt and per-stock locks close double-spend races. Tokens rotate on every resume and are bound to the user and the instance. Damage and coins are re-derived by a deterministic PHP mirror of the JS engine, and unverifiable wins restart the phase instead of being credited. Question ids are always drawn on the server, and every lookup is scoped to the instance. The privacy provider covers every table and preference, backup/restore remaps all references, and the test suite is unusually thorough.