Files
ctfd-cheaters/migrations/5b1e0c7d9a24_unique_cheat_reports.py
Matte23andClaude Opus 5.5 8084b3996a 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>
2026-11-18 10:21:53 +01:00

70 lines
2.1 KiB
Python

"""One cheat report per cheater, challenge and flag
The same cheat used to be reported once per submission, and repeatedly
with the "all" flag logic, which re-checks earlier submissions. Drop the
duplicates (keeping the oldest report) and add a unique constraint.
Revision ID: 5b1e0c7d9a24
Revises:
Create Date: 2026-11-18
"""
import sqlalchemy as sa
from CTFd.plugins.migrations import get_all_tables
revision = "5b1e0c7d9a24"
down_revision = None
branch_labels = None
depends_on = None
TABLE = "cheater_teams"
CONSTRAINT = "uq_cheater_teams_report"
COLUMNS = ["challenge_id", "cheater_id", "flag_id"]
def _has_unique(op):
"""Created unnamed by db.create_all() on fresh installs: match on columns.
MySQL reports unique constraints as indexes.
"""
inspector = sa.inspect(op.get_bind())
for uc in inspector.get_unique_constraints(TABLE):
if sorted(uc["column_names"]) == sorted(COLUMNS):
return True
for index in inspector.get_indexes(TABLE):
if index.get("unique") and sorted(index["column_names"]) == sorted(COLUMNS):
return True
return False
def upgrade(op=None):
if TABLE not in get_all_tables(op) or _has_unique(op):
return
conn = op.get_bind()
duplicates = conn.execute(
sa.text(
f"SELECT challenge_id, cheater_id, flag_id, MIN(id) FROM {TABLE} "
"GROUP BY challenge_id, cheater_id, flag_id HAVING COUNT(*) > 1"
)
).fetchall()
for challenge_id, cheater_id, flag_id, keep_id in duplicates:
conn.execute(
sa.text(
f"DELETE FROM {TABLE} WHERE challenge_id = :c AND cheater_id = :u "
"AND flag_id = :f AND id != :keep"
),
{"c": challenge_id, "u": cheater_id, "f": flag_id, "keep": keep_id},
)
op.create_unique_constraint(CONSTRAINT, TABLE, COLUMNS)
def downgrade(op=None):
if TABLE not in get_all_tables(op):
return
names = {
uc["name"] for uc in sa.inspect(op.get_bind()).get_unique_constraints(TABLE)
}
if CONSTRAINT in names:
op.drop_constraint(CONSTRAINT, TABLE, type_="unique")