mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-17 14:04:20 +01:00
Alpha release of CTFd v3.
# 3.0.0a1 / 2020-07-01
**General**
- CTFd is now Python 3 only
- Render markdown with the CommonMark spec provided by `cmarkgfm`
- Render markdown stripped of any malicious JavaScript or HTML.
- This is a significant change from previous versions of CTFd where any HTML content from an admin was considered safe.
- Inject `Config`, `User`, `Team`, `Session`, and `Plugin` globals into Jinja
- User sessions no longer store any user-specific attributes.
- Sessions only store the user's ID, CSRF nonce, and an hmac of the user's password
- This allows for session invalidation on password changes
- The user facing side of CTFd now has user and team searching
- GeoIP support now available for converting IP addresses to guessed countries
**Admin Panel**
- Use EasyMDE as an improved description/text editor for Markdown enabled fields.
- Media Library button now integrated into EasyMDE enabled fields
- VueJS now used as the underlying implementation for the Media Library
- Fix setting theme color in Admin Panel
- Green outline border has been removed from the Admin Panel
**API**
- Significant overhauls in API documentation provided by Swagger UI and Swagger json
- Make almost all API endpoints provide filtering and searching capabilities
- Change `GET /api/v1/config/<config_key>` to return structured data according to ConfigSchema
**Themes**
- Themes now have access to the `Configs` global which provides wrapped access to `get_config`.
- For example, `{{ Configs.ctf_name }}` instead of `get_ctf_name()` or `get_config('ctf_name')`
- Themes must now specify a `challenge.html` which control how a challenge should look.
- The main library for charts has been changed from Plotly to Apache ECharts.
- Forms have been moved into wtforms for easier form rendering inside of Jinja.
- From Jinja you can access forms via the Forms global i.e. `{{ Forms }}`
- This allows theme developers to more easily re-use a form without having to copy-paste HTML.
- Themes can now provide a theme settings JSON blob which can be injected into the theme with `{{ Configs.theme_settings }}`
- Core theme now includes the challenge ID in location hash identifiers to always refer the right challenge despite duplicate names
**Plugins**
- Challenge plugins have changed in structure to better allow integration with themes and prevent obtrusive Javascript/XSS.
- Challenge rendering now uses `challenge.html` from the provided theme.
- Accessing the challenge view content is now provided by `/api/v1/challenges/<challenge_id>` in the `view` section. This allows for HTML to be properly sanitized and rendered by the server allowing CTFd to remove client side Jinja rendering.
- `challenge.html` now specifies what's required and what's rendered by the theme. This allows the challenge plugin to avoid having to deal with aspects of the challenge besides the description and input.
- A more complete migration guide will be provided when CTFd v3 leaves beta
- Display current attempt count in challenge view when max attempts is enabled
- `get_standings()`, `get_team_stanadings()`, `get_user_standings()` now has a fields keyword argument that allows for specificying additional fields that SQLAlchemy should return when building the response set.
- Useful for gathering additional data when building scoreboard pages
- Flags can now control the message that is shown to the user by raising `FlagException`
- Fix `override_template()` functionality
**Deployment**
- Enable SQLAlchemy's `pool_pre_ping` by default to reduce the likelihood of database connection issues
- Mailgun email settings are now deprecated. Admins should move to SMTP email settings instead.
- Postgres is now considered a second class citizen in CTFd. It is tested against but not a main database backend. If you use Postgres, you are entirely on your own with regards to supporting CTFd.
- Docker image now uses Debian instead of Alpine. See https://github.com/CTFd/CTFd/issues/1215 for rationale.
- `docker-compose.yml` now uses a non-root user to connect to MySQL/MariaDB
- `config.py` should no longer be editting for configuration, instead edit `config.ini` or the environment variables in `docker-compose.yml`
317 lines
11 KiB
Python
317 lines
11 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from freezegun import freeze_time
|
|
|
|
from CTFd.models import Users
|
|
from tests.helpers import (
|
|
create_ctfd,
|
|
destroy_ctfd,
|
|
gen_award,
|
|
gen_challenge,
|
|
gen_flag,
|
|
gen_solve,
|
|
get_scores,
|
|
login_as_user,
|
|
register_user,
|
|
)
|
|
|
|
|
|
def test_user_get_scoreboard_components():
|
|
app = create_ctfd()
|
|
with app.app_context():
|
|
register_user(app)
|
|
client = login_as_user(app)
|
|
|
|
# test_user_get_scoreboard
|
|
"""Can a registered user load scoreboard components"""
|
|
r = client.get("/scoreboard")
|
|
assert r.status_code == 200
|
|
|
|
# test_user_get_scores
|
|
"""Can a registered user load /api/v1/scoreboard"""
|
|
r = client.get("/api/v1/scoreboard")
|
|
assert r.status_code == 200
|
|
|
|
# test_user_get_topteams
|
|
"""Can a registered user load /api/v1/scoreboard/top/10"""
|
|
r = client.get("/api/v1/scoreboard/top/10")
|
|
assert r.status_code == 200
|
|
destroy_ctfd(app)
|
|
|
|
|
|
def test_user_score_is_correct():
|
|
"""Test that a user's score is correct"""
|
|
app = create_ctfd()
|
|
with app.app_context():
|
|
# create user1
|
|
register_user(app, name="user1", email="user1@ctfd.io")
|
|
|
|
# create challenge
|
|
chal = gen_challenge(app.db, value=100)
|
|
gen_flag(app.db, challenge_id=chal.id, content="flag")
|
|
chal_id = chal.id
|
|
|
|
# create a solve for the challenge for user1. (the id is 2 because of the admin)
|
|
gen_solve(app.db, user_id=2, challenge_id=chal_id)
|
|
user1 = Users.query.filter_by(id=2).first()
|
|
|
|
# assert that user1's score is 100
|
|
assert user1.score == 100
|
|
assert user1.place == "1st"
|
|
|
|
# create user2
|
|
register_user(app, name="user2", email="user2@ctfd.io")
|
|
|
|
# user2 solves the challenge
|
|
gen_solve(app.db, 3, challenge_id=chal_id)
|
|
|
|
# assert that user2's score is 100 but is in 2nd place
|
|
user2 = Users.query.filter_by(id=3).first()
|
|
assert user2.score == 100
|
|
assert user2.place == "2nd"
|
|
|
|
# create an award for user2
|
|
gen_award(app.db, user_id=3, value=5)
|
|
|
|
# assert that user2's score is now 105 and is in 1st place
|
|
assert user2.score == 105
|
|
assert user2.place == "1st"
|
|
destroy_ctfd(app)
|
|
|
|
|
|
def test_top_10():
|
|
"""Make sure top10 returns correct information"""
|
|
app = create_ctfd()
|
|
with app.app_context():
|
|
register_user(app, name="user1", email="user1@ctfd.io")
|
|
register_user(app, name="user2", email="user2@ctfd.io")
|
|
register_user(app)
|
|
|
|
chal1 = gen_challenge(app.db)
|
|
gen_flag(app.db, challenge_id=chal1.id, content="flag")
|
|
chal1_id = chal1.id
|
|
|
|
chal2 = gen_challenge(app.db)
|
|
gen_flag(app.db, challenge_id=chal2.id, content="flag")
|
|
chal2_id = chal2.id
|
|
|
|
# Generates solve for user1
|
|
with freeze_time("2017-10-3 03:21:34"):
|
|
gen_solve(app.db, user_id=2, challenge_id=chal1_id)
|
|
|
|
with freeze_time("2017-10-4 03:25:45"):
|
|
gen_solve(app.db, user_id=2, challenge_id=chal2_id)
|
|
|
|
# Generate solve for user2
|
|
with freeze_time("2017-10-3 03:21:34"):
|
|
gen_solve(app.db, user_id=3, challenge_id=chal1_id)
|
|
|
|
client = login_as_user(app)
|
|
r = client.get("/api/v1/scoreboard/top/10")
|
|
response = r.get_json()["data"]
|
|
|
|
saved = {
|
|
"1": {
|
|
"id": 2,
|
|
"solves": [
|
|
{
|
|
"date": "2017-10-03T03:21:34Z",
|
|
"challenge_id": 1,
|
|
"account_id": 2,
|
|
"user_id": 2,
|
|
"team_id": None,
|
|
"value": 100,
|
|
},
|
|
{
|
|
"date": "2017-10-04T03:25:45Z",
|
|
"challenge_id": 2,
|
|
"account_id": 2,
|
|
"user_id": 2,
|
|
"team_id": None,
|
|
"value": 100,
|
|
},
|
|
],
|
|
"name": "user1",
|
|
},
|
|
"2": {
|
|
"id": 3,
|
|
"solves": [
|
|
{
|
|
"date": "2017-10-03T03:21:34Z",
|
|
"challenge_id": 1,
|
|
"account_id": 3,
|
|
"user_id": 3,
|
|
"team_id": None,
|
|
"value": 100,
|
|
}
|
|
],
|
|
"name": "user2",
|
|
},
|
|
}
|
|
assert saved == response
|
|
destroy_ctfd(app)
|
|
|
|
|
|
def test_scoring_logic():
|
|
"""Test that scoring logic is correct"""
|
|
app = create_ctfd()
|
|
with app.app_context():
|
|
admin = login_as_user(app, name="admin", password="password")
|
|
|
|
register_user(app, name="user1", email="user1@ctfd.io", password="password")
|
|
client1 = login_as_user(app, name="user1", password="password")
|
|
register_user(app, name="user2", email="user2@ctfd.io", password="password")
|
|
client2 = login_as_user(app, name="user2", password="password")
|
|
|
|
chal1 = gen_challenge(app.db)
|
|
gen_flag(app.db, challenge_id=chal1.id, content="flag")
|
|
chal1_id = chal1.id
|
|
|
|
chal2 = gen_challenge(app.db)
|
|
gen_flag(app.db, challenge_id=chal2.id, content="flag")
|
|
chal2_id = chal2.id
|
|
|
|
# user1 solves chal1
|
|
with freeze_time("2017-10-3 03:21:34"):
|
|
with client1.session_transaction():
|
|
data = {"submission": "flag", "challenge_id": chal1_id}
|
|
client1.post("/api/v1/challenges/attempt", json=data)
|
|
|
|
# user1 is now on top
|
|
scores = get_scores(admin)
|
|
assert scores[0]["name"] == "user1"
|
|
|
|
# user2 solves chal1 and chal2
|
|
with freeze_time("2017-10-4 03:30:34"):
|
|
with client2.session_transaction():
|
|
# solve chal1
|
|
data = {"submission": "flag", "challenge_id": chal1_id}
|
|
client2.post("/api/v1/challenges/attempt", json=data)
|
|
# solve chal2
|
|
data = {"submission": "flag", "challenge_id": chal2_id}
|
|
client2.post("/api/v1/challenges/attempt", json=data)
|
|
|
|
# user2 is now on top
|
|
scores = get_scores(admin)
|
|
assert scores[0]["name"] == "user2"
|
|
|
|
# user1 solves chal2
|
|
with freeze_time("2017-10-5 03:50:34"):
|
|
with client1.session_transaction():
|
|
data = {"submission": "flag", "challenge_id": chal2_id}
|
|
client1.post("/api/v1/challenges/attempt", json=data)
|
|
|
|
# user2 should still be on top because they solved chal2 first
|
|
scores = get_scores(admin)
|
|
assert scores[0]["name"] == "user2"
|
|
destroy_ctfd(app)
|
|
|
|
|
|
def test_scoring_logic_with_zero_point_challenges():
|
|
"""Test that scoring logic is correct with zero point challenges. Zero point challenges should not tie break"""
|
|
app = create_ctfd()
|
|
with app.app_context():
|
|
admin = login_as_user(app, name="admin", password="password")
|
|
|
|
register_user(app, name="user1", email="user1@ctfd.io", password="password")
|
|
client1 = login_as_user(app, name="user1", password="password")
|
|
register_user(app, name="user2", email="user2@ctfd.io", password="password")
|
|
client2 = login_as_user(app, name="user2", password="password")
|
|
|
|
chal1 = gen_challenge(app.db)
|
|
gen_flag(app.db, challenge_id=chal1.id, content="flag")
|
|
chal1_id = chal1.id
|
|
|
|
chal2 = gen_challenge(app.db)
|
|
gen_flag(app.db, challenge_id=chal2.id, content="flag")
|
|
chal2_id = chal2.id
|
|
|
|
# A 0 point challenge shouldn't influence the scoreboard (see #577)
|
|
chal0 = gen_challenge(app.db, value=0)
|
|
gen_flag(app.db, challenge_id=chal0.id, content="flag")
|
|
chal0_id = chal0.id
|
|
|
|
# user1 solves chal1
|
|
with freeze_time("2017-10-3 03:21:34"):
|
|
with client1.session_transaction():
|
|
data = {"submission": "flag", "challenge_id": chal1_id}
|
|
client1.post("/api/v1/challenges/attempt", json=data)
|
|
|
|
# user1 is now on top
|
|
scores = get_scores(admin)
|
|
assert scores[0]["name"] == "user1"
|
|
|
|
# user2 solves chal1 and chal2
|
|
with freeze_time("2017-10-4 03:30:34"):
|
|
with client2.session_transaction():
|
|
# solve chal1
|
|
data = {"submission": "flag", "challenge_id": chal1_id}
|
|
client2.post("/api/v1/challenges/attempt", json=data)
|
|
# solve chal2
|
|
data = {"submission": "flag", "challenge_id": chal2_id}
|
|
client2.post("/api/v1/challenges/attempt", json=data)
|
|
|
|
# user2 is now on top
|
|
scores = get_scores(admin)
|
|
assert scores[0]["name"] == "user2"
|
|
|
|
# user1 solves chal2
|
|
with freeze_time("2017-10-5 03:50:34"):
|
|
with client1.session_transaction():
|
|
data = {"submission": "flag", "challenge_id": chal2_id}
|
|
client1.post("/api/v1/challenges/attempt", json=data)
|
|
|
|
# user2 should still be on top because they solved chal2 first
|
|
scores = get_scores(admin)
|
|
assert scores[0]["name"] == "user2"
|
|
|
|
# user2 solves a 0 point challenge
|
|
with freeze_time("2017-10-5 03:55:34"):
|
|
with client2.session_transaction():
|
|
data = {"submission": "flag", "challenge_id": chal0_id}
|
|
client2.post("/api/v1/challenges/attempt", json=data)
|
|
|
|
# user2 should still be on top because 0 point challenges should not tie break
|
|
scores = get_scores(admin)
|
|
assert scores[0]["name"] == "user2"
|
|
destroy_ctfd(app)
|
|
|
|
|
|
def test_hidden_users_should_not_influence_scores():
|
|
app = create_ctfd()
|
|
with app.app_context():
|
|
register_user(app, name="user1", email="user1@ctfd.io", password="password")
|
|
register_user(app, name="user2", email="user2@ctfd.io", password="password")
|
|
register_user(app, name="user3", email="user3@ctfd.io", password="password")
|
|
|
|
user = Users.query.filter_by(name="user3").first()
|
|
user.hidden = True
|
|
app.db.session.commit()
|
|
|
|
client1 = login_as_user(app, name="user1", password="password")
|
|
|
|
# User 1 solves 1st challenge
|
|
chal1 = gen_challenge(app.db)
|
|
gen_solve(app.db, user_id=2, challenge_id=chal1.id)
|
|
|
|
# User 2 solves 2nd challenge
|
|
chal2 = gen_challenge(app.db)
|
|
gen_solve(app.db, user_id=3, challenge_id=chal2.id)
|
|
|
|
# User 3 solves both
|
|
gen_solve(app.db, user_id=4, challenge_id=chal1.id)
|
|
gen_solve(app.db, user_id=4, challenge_id=chal2.id)
|
|
|
|
scores = get_scores(client1)
|
|
|
|
for entry in scores:
|
|
assert entry["name"] != "user3"
|
|
|
|
user1 = Users.query.filter_by(name="user1").first()
|
|
assert user1.place == "1st"
|
|
|
|
user2 = Users.query.filter_by(name="user2").first()
|
|
assert user2.place == "2nd"
|
|
destroy_ctfd(app)
|