Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add problem visibility #18

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pwncore/models/ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Problem(BaseProblem):

mi = fields.IntField(default=100) # Arbitrary meaning full defaults
ma = fields.IntField(default=600)
visible = fields.BooleanField(default=True)

hints: fields.ReverseRelation[Hint]

Expand Down
12 changes: 7 additions & 5 deletions src/pwncore/routes/ctf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Flag(BaseModel):

@router.get("/list")
async def ctf_list():
problems = await BaseProblem_Pydantic.from_queryset(Problem.all())
problems = await BaseProblem_Pydantic.from_queryset(Problem.filter(visible=True))
return problems


Expand All @@ -73,7 +73,7 @@ async def flag_post(
req: Request, ctf_id: int, flag: Flag, response: Response, jwt: RequireJwt
):
team_id = jwt["team_id"]
problem = await Problem.get_or_none(id=ctf_id)
problem = await Problem.get_or_none(id=ctf_id, visible=True)
if not problem:
response.status_code = 404
return {"msg_code": config.msg_codes["ctf_not_found"]}
Expand Down Expand Up @@ -102,7 +102,7 @@ async def flag_post(
@router.get("/{ctf_id}/hint")
async def hint_get(ctf_id: int, response: Response, jwt: RequireJwt):
team_id = jwt["team_id"]
problem = await Problem.exists(id=ctf_id)
problem = await Problem.exists(id=ctf_id, visible=True)
if not problem:
response.status_code = 404
return {"msg_code": config.msg_codes["ctf_not_found"]}
Expand Down Expand Up @@ -151,14 +151,16 @@ async def viewed_problem_hints_get(ctf_id: int, jwt: RequireJwt):
async def completed_problem_get(jwt: RequireJwt):
team_id = jwt["team_id"]
problems = await BaseProblem_Pydantic.from_queryset(
Problem.filter(solvedproblems__team_id=team_id)
Problem.filter(solvedproblems__team_id=team_id, visible=True)
)
return problems


@router.get("/{ctf_id}")
async def ctf_get(ctf_id: int, response: Response):
problem = await BaseProblem_Pydantic.from_queryset(Problem.filter(id=ctf_id))
problem = await BaseProblem_Pydantic.from_queryset(
Problem.filter(id=ctf_id, visible=True)
)
if not problem:
response.status_code = 404
return {"msg_code": config.msg_codes["ctf_not_found"]}
Expand Down
4 changes: 3 additions & 1 deletion src/pwncore/routes/ctf/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def start_docker_container(ctf_id: int, response: Response, jwt: RequireJw
}
"""

ctf = await Problem.get_or_none(id=ctf_id)
ctf = await Problem.get_or_none(id=ctf_id, visible=True)
if not ctf:
response.status_code = 404
return {"msg_code": config.msg_codes["ctf_not_found"]}
Expand Down Expand Up @@ -131,6 +131,8 @@ async def stopall_docker_container(response: Response, jwt: RequireJwt):
@atomic()
@router.post("/{ctf_id}/stop")
async def stop_docker_container(ctf_id: int, response: Response, jwt: RequireJwt):
# Let this work on invisible problems incase
# we mess up the database while making problems visible
ctf = await Problem.get_or_none(id=ctf_id)
if not ctf:
response.status_code = 404
Expand Down
2 changes: 1 addition & 1 deletion src/pwncore/routes/leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, period: float) -> None:
async def _do_update(self):
self.data = dict(
await Team.all()
.filter(solved_problem__problem__id__gt=-1)
.filter(solved_problem__problem__visible=True)
.annotate(
tpoints=Sum(
RawSQL(
Expand Down