Files
ctfd-cheaters/__init__.py
Matte23andClaude Opus 5.5 a42e7f2823 fix: Silence CTFd's Flags polymorphic-identity warning on flag creation
CTFd's Flags model sets polymorphic_on without mapping any type, so every
flag flush warns. Mapping "personal" would break loading the other types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 18:38:48 +01:00

103 lines
3.5 KiB
Python

import warnings
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 flask import Blueprint, redirect, render_template, request, url_for
from sqlalchemy.exc import SAWarning
from sqlalchemy.orm import joinedload
from .flags import PersonalFlag
from .models import CheatReport
# 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
bp = Blueprint(PLUGIN_NAME, __name__, template_folder="templates")
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:
flag = Flags(
challenge_id=challenge_id,
type="personal",
content=flag_content,
data=data,
)
db.session.add(flag)
# CTFd's Flags model sets polymorphic_on without mapping any type, so
# SQLAlchemy warns on every flag flush (static flags too). Mapping
# "personal" would break loading the other types: ignore it here.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
message="Flushing object .* with incompatible polymorphic identity",
category=SAWarning,
)
db.session.commit()
return flag.id
@bp.route("/admin/cheaters")
@admins_only
def show_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=PLUGIN_NAME)
FLAG_CLASSES["personal"] = PersonalFlag
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)