fix: Reliable cheat reports and a usable admin side

Reports
- Sharing within a team is no longer reported: owner and submitter are
  compared by account (the team in teams mode), not by user.
- Admins (e.g. testing in preview mode) are never reported.
- A personal flag without a valid owner no longer crashes the submission
  (int() of its data raised a 500); empty flags never match.
- One report per cheater, challenge and flag (unique constraint plus a
  check before inserting): "all" logic re-checks every earlier submission
  on each attempt, which created duplicates. Migration 5b1e0c7d9a24 drops
  existing duplicates and adds the constraint.
- Every new report is logged. Matching uses hmac.compare_digest.

Flags and helpers
- The admin flag forms sent the owner as "user id", which CTFd's flag
  API ignores: personal flags created by admins had no owner. They now
  send "data", with labels bound to their inputs.
- create_flag_if_missing only considers personal flags, deterministically.

Plugin
- Migrations run under the plugin's real directory (was "cheaters");
  asset paths follow the directory instead of hardcoding it.
- Model renamed CheatReport (owner instead of helper); table and column
  names unchanged. Dates stored in UTC.
- Admin page: newest first, paginated, related rows loaded with the page,
  real report ids, url_for links, team columns in teams mode, local-time
  dates, and a Dismiss action.
- README (install as ctfd_cheaters, "any" logic, API for other plugins);
  pyrightconfig without an absolute path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Matte23andClaude Opus 5.5 committed 2026-11-18 10:21:53 +01:00
1 parent 47b9a8a7f1
commit 8084b3996a
9 files changed
+368 -132

No files matched your search

+61 -26
View File
@@ -1,55 +1,90 @@
import os
from CTFd.plugins import register_admin_plugin_menu_bar, register_plugin_assets_directory
from CTFd.plugins.migrations import upgrade
from CTFd.models import Flags, Users, db
from CTFd.plugins import (
register_admin_plugin_menu_bar,
register_plugin_assets_directory,
)
from CTFd.plugins.flags import FLAG_CLASSES
from CTFd.plugins.migrations import upgrade
from CTFd.utils.config import is_teams_mode
from CTFd.utils.decorators import admins_only
from CTFd.models import Flags, db
from flask import Blueprint, render_template, request
from flask import Blueprint, redirect, render_template, request, url_for
from sqlalchemy.orm import joinedload
from .models import CheaterTeams
from .flags import PersonalFlag, report_cheater
from .flags import PersonalFlag
from .models import CheatReport
PLUGIN_PATH = os.path.dirname(__file__)
# The plugin's directory: CTFd keys migrations and serves assets by it. Other
# plugins import this one as CTFd.plugins.ctfd_cheaters, so install it there.
PLUGIN_NAME = __name__.split(".")[-1]
REPORTS_PER_PAGE = 50
directory_name = PLUGIN_PATH.split(os.sep)[-1] # Get the directory name of this file
bp = Blueprint(directory_name, __name__, template_folder="templates")
bp = Blueprint(PLUGIN_NAME, __name__, template_folder="templates")
def create_flag_if_missing(challenge_id: int, user_id: int, flag_content: str):
user_id_str = str(user_id)
flag = Flags.query.filter_by(challenge_id=challenge_id, data=user_id_str).first()
def create_flag_if_missing(challenge_id: int, user_id: int, flag_content: str) -> int:
"""Id of the user's personal flag for the challenge.
Created with ``flag_content`` if the user has none yet; an existing flag
is kept as it is. Used by ctfd-whale to give each user their own flag.
"""
data = str(user_id)
flag = (
Flags.query.filter_by(challenge_id=challenge_id, type="personal", data=data)
.order_by(Flags.id)
.first()
)
if flag is None:
new_flag = Flags(
flag = Flags(
challenge_id=challenge_id,
type="personal",
content=flag_content,
data=user_id_str,
data=data,
)
db.session.add(new_flag)
db.session.add(flag)
db.session.commit()
return new_flag.id
return flag.id
@bp.route("/admin/cheaters", methods=["GET"])
@bp.route("/admin/cheaters")
@admins_only
def show_cheaters():
if request.method == "GET":
cheaters = CheaterTeams.query.all()
return render_template("cheaters.html", cheaters=cheaters)
page = max(request.args.get("page", 1, type=int) or 1, 1)
reports = (
CheatReport.query.options(
joinedload(CheatReport.challenge),
joinedload(CheatReport.cheater).joinedload(Users.team),
joinedload(CheatReport.owner).joinedload(Users.team),
joinedload(CheatReport.flag),
)
.order_by(CheatReport.date.desc(), CheatReport.id.desc())
.paginate(page=page, per_page=REPORTS_PER_PAGE, error_out=False)
)
return render_template(
"cheaters.html",
reports=reports,
teams_mode=is_teams_mode(),
endpoint=PLUGIN_NAME,
)
@bp.route("/admin/cheaters/<int:report_id>/dismiss", methods=["POST"])
@admins_only
def dismiss_report(report_id):
CheatReport.query.filter_by(id=report_id).delete()
db.session.commit()
page = request.form.get("page", 1, type=int)
return redirect(url_for(f"{PLUGIN_NAME}.show_cheaters", page=page))
def load(app):
# create_all() creates missing tables; the migrations bring existing
# tables up to date (CTFd skips them on SQLite and only runs create_all)
app.db.create_all()
upgrade(plugin_name="cheaters")
upgrade(plugin_name=PLUGIN_NAME)
FLAG_CLASSES["personal"] = PersonalFlag
register_plugin_assets_directory(app, base_path="/plugins/ctfd_cheaters/assets/")
register_plugin_assets_directory(app, base_path=f"/plugins/{PLUGIN_NAME}/assets/")
register_admin_plugin_menu_bar(title="Cheaters", route="/admin/cheaters")
app.register_blueprint(bp)