- plugin_tests/: 9 tests running the plugin inside CTFd 3.8.9's test app (users and teams mode): own/shared flags, single reports, admins, invalid owners and empty flags, teams, create_flag_if_missing, the admin flag form, the admin page and dismissing. - make venv (uv, Python 3.11), make lint, make format, make test: same tools and versions as ctfd-whale. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
184 lines
7.0 KiB
Python
184 lines
7.0 KiB
Python
from conftest import plugin, reports, submit
|
|
from CTFd.models import Flags, Users, db
|
|
from tests.helpers import (
|
|
gen_challenge,
|
|
gen_flag,
|
|
gen_team,
|
|
login_as_user,
|
|
register_user,
|
|
)
|
|
|
|
|
|
def _challenge_with_flags(app, *owners):
|
|
"""A challenge with one personal flag per owner id; returns (id, {owner: flag})."""
|
|
with app.app_context():
|
|
challenge = gen_challenge(db)
|
|
flags = {
|
|
owner: gen_flag(
|
|
db,
|
|
challenge.id,
|
|
content=f"flag{{{owner}}}",
|
|
type="personal",
|
|
data=str(owner),
|
|
).id
|
|
for owner in owners
|
|
}
|
|
return challenge.id, flags
|
|
|
|
|
|
def _users(app, *names):
|
|
for name in names:
|
|
register_user(app, name=name, email=f"{name}@examplectf.com")
|
|
with app.app_context():
|
|
return [Users.query.filter_by(name=name).one().id for name in names]
|
|
|
|
|
|
def test_own_flag_is_correct_and_not_reported(app):
|
|
alice, bob = _users(app, "alice", "bob")
|
|
challenge_id, _ = _challenge_with_flags(app, alice, bob)
|
|
with login_as_user(app, name="alice") as client:
|
|
assert submit(client, challenge_id, f"flag{{{alice}}}") == "correct"
|
|
assert reports(app) == []
|
|
|
|
|
|
def test_shared_flag_is_accepted_and_reported_once(app):
|
|
alice, bob = _users(app, "alice", "bob")
|
|
challenge_id, flags = _challenge_with_flags(app, alice, bob)
|
|
with login_as_user(app, name="alice") as client:
|
|
assert submit(client, challenge_id, "wrong") == "incorrect"
|
|
assert submit(client, challenge_id, f"flag{{{bob}}}") == "correct"
|
|
assert reports(app) == [(alice, bob, flags[bob])]
|
|
# reporting the same cheat again (e.g. "all" logic re-checks) adds nothing
|
|
with app.test_request_context(), login_as_user(app, name="alice"):
|
|
cheater = Users.query.get(alice)
|
|
owner = Users.query.get(bob)
|
|
plugin("flags").report_cheater(challenge_id, cheater, owner, flags[bob])
|
|
assert len(reports(app)) == 1
|
|
|
|
|
|
def test_admins_are_not_reported(app):
|
|
(alice,) = _users(app, "alice")
|
|
challenge_id, _ = _challenge_with_flags(app, alice)
|
|
with login_as_user(app, name="admin") as admin:
|
|
assert (
|
|
submit(admin, challenge_id, f"flag{{{alice}}}", preview=True) == "correct"
|
|
)
|
|
assert reports(app) == []
|
|
|
|
|
|
def test_invalid_owner_and_empty_flags(app):
|
|
(alice,) = _users(app, "alice")
|
|
with app.app_context():
|
|
challenge_id = gen_challenge(db).id
|
|
db.session.add(
|
|
Flags(challenge_id=challenge_id, type="personal", content="flag{x}")
|
|
)
|
|
db.session.add(
|
|
Flags(
|
|
challenge_id=challenge_id,
|
|
type="personal",
|
|
content="flag{y}",
|
|
data="bob",
|
|
)
|
|
)
|
|
db.session.add(Flags(challenge_id=challenge_id, type="personal", content=""))
|
|
db.session.commit()
|
|
with login_as_user(app, name="alice") as client:
|
|
# no crash for a flag without (or with an invalid) owner, and no report
|
|
assert submit(client, challenge_id, "flag{x}") == "correct"
|
|
with login_as_user(app, name="alice") as client:
|
|
assert submit(client, challenge_id, "") in ("incorrect", "already_solved")
|
|
assert reports(app) == []
|
|
|
|
|
|
def test_teams_mode_only_reports_other_teams(teams_app):
|
|
app = teams_app
|
|
with app.app_context():
|
|
red = gen_team(db, name="red", email="red@examplectf.com", member_count=2)
|
|
blue = gen_team(db, name="blue", email="blue@examplectf.com", member_count=1)
|
|
red_a, red_b = (m.id for m in red.members)
|
|
blue_a = blue.members[0].id
|
|
names = {u.id: u.name for u in Users.query.all()}
|
|
challenge_id, flags = _challenge_with_flags(app, red_a, red_b, blue_a)
|
|
with login_as_user(app, name=names[red_b]) as client:
|
|
# a teammate's flag: shared within the team, not a cheat
|
|
assert submit(client, challenge_id, f"flag{{{red_a}}}") == "correct"
|
|
assert reports(app) == []
|
|
second = _challenge_with_flags(app, red_a, blue_a)[0]
|
|
with login_as_user(app, name=names[blue_a]) as client:
|
|
assert submit(client, second, f"flag{{{red_a}}}") == "correct"
|
|
with app.app_context():
|
|
assert [
|
|
(r.cheater_id, r.owner_id) for r in plugin("models").CheatReport.query
|
|
] == [(blue_a, red_a)]
|
|
|
|
|
|
def test_create_flag_if_missing(app):
|
|
alice, bob = _users(app, "alice", "bob")
|
|
create = plugin().create_flag_if_missing
|
|
with app.app_context():
|
|
challenge_id = gen_challenge(db).id
|
|
# a non-personal flag with the same data is never picked up
|
|
gen_flag(db, challenge_id, content="static", data=str(alice))
|
|
first = create(challenge_id, alice, "flag{one}")
|
|
again = create(challenge_id, alice, "flag{two}")
|
|
other = create(challenge_id, bob, "flag{bob}")
|
|
assert first == again != other
|
|
flag = Flags.query.get(first)
|
|
assert (flag.type, flag.content, flag.data) == (
|
|
"personal",
|
|
"flag{one}",
|
|
str(alice),
|
|
)
|
|
|
|
|
|
def test_admin_flag_form_sets_the_owner(app):
|
|
(alice,) = _users(app, "alice")
|
|
with app.app_context():
|
|
challenge_id = gen_challenge(db).id
|
|
with login_as_user(app, name="admin") as admin:
|
|
# what the admin form posts (its fields: content, data, type)
|
|
r = admin.post(
|
|
"/api/v1/flags",
|
|
json={
|
|
"challenge": challenge_id,
|
|
"content": "flag{a}",
|
|
"data": str(alice),
|
|
"type": "personal",
|
|
},
|
|
)
|
|
assert r.status_code == 200, r.get_json()
|
|
assert r.get_json()["data"]["data"] == str(alice)
|
|
r = admin.get("/api/v1/flags/types/personal")
|
|
templates = r.get_json()["data"]["templates"]
|
|
assert templates["create"] == "/plugins/ctfd_cheaters/assets/create.html"
|
|
assert admin.get(templates["create"]).status_code == 200
|
|
|
|
|
|
def test_admin_page_and_dismiss(app):
|
|
alice, bob = _users(app, "alice", "bob")
|
|
challenge_id, _ = _challenge_with_flags(app, alice, bob)
|
|
with login_as_user(app, name="alice") as client:
|
|
assert submit(client, challenge_id, f"flag{{{bob}}}") == "correct"
|
|
[report_id] = _report_ids(app)
|
|
with login_as_user(app, name="admin") as admin:
|
|
page = admin.get("/admin/cheaters").get_data(as_text=True)
|
|
assert "alice" in page and "bob" in page and f"flag{{{bob}}}" in page
|
|
with admin.session_transaction() as sess:
|
|
nonce = sess["nonce"]
|
|
r = admin.post(f"/admin/cheaters/{report_id}/dismiss", data={"nonce": nonce})
|
|
assert r.status_code == 302
|
|
assert "No reports" in admin.get("/admin/cheaters").get_data(as_text=True)
|
|
assert reports(app) == []
|
|
|
|
|
|
def test_players_cannot_see_the_admin_page(app):
|
|
_users(app, "alice")
|
|
with login_as_user(app, name="alice") as client:
|
|
assert client.get("/admin/cheaters").status_code in (302, 403)
|
|
|
|
|
|
def _report_ids(app):
|
|
with app.app_context():
|
|
return [r.id for r in plugin("models").CheatReport.query.all()]
|