Skip to content

Commit

Permalink
Put up a fix for challenges whose requirements were deleted (CTFd#1861)
Browse files Browse the repository at this point in the history
* Works on CTFd#1860 
* Don't consider deleted challenges when evaluating challenge prereqs
  • Loading branch information
ColdHeat authored Apr 6, 2021
1 parent 26dbbbe commit 87711d7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
21 changes: 17 additions & 4 deletions CTFd/api/v1/challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,17 @@ def get(self, query_args):
# will be JSONified back to the client
response = []
tag_schema = TagSchema(view="user", many=True)

# Gather all challenge IDs so that we can determine invalid challenge prereqs
all_challenge_ids = {
c.id for c in Challenges.query.with_entities(Challenges.id).all()
}
for challenge in chal_q:
if challenge.requirements:
requirements = challenge.requirements.get("prerequisites", [])
anonymize = challenge.requirements.get("anonymize")
prereqs = set(requirements)
if user_solves >= prereqs:
prereqs = set(requirements).intersection(all_challenge_ids)
if user_solves >= prereqs or admin_view:
pass
else:
if anonymize:
Expand Down Expand Up @@ -365,6 +370,10 @@ def get(self, challenge_id):
if chal.requirements:
requirements = chal.requirements.get("prerequisites", [])
anonymize = chal.requirements.get("anonymize")
# Gather all challenge IDs so that we can determine invalid challenge prereqs
all_challenge_ids = {
c.id for c in Challenges.query.with_entities(Challenges.id).all()
}
if challenges_visible():
user = get_current_user()
if user:
Expand All @@ -378,7 +387,7 @@ def get(self, challenge_id):
# We need to handle the case where a user is viewing challenges anonymously
solve_ids = []
solve_ids = {value for value, in solve_ids}
prereqs = set(requirements)
prereqs = set(requirements).intersection(all_challenge_ids)
if solve_ids >= prereqs or is_admin():
pass
else:
Expand Down Expand Up @@ -603,7 +612,11 @@ def post(self):
.all()
)
solve_ids = {solve_id for solve_id, in solve_ids}
prereqs = set(requirements)
# Gather all challenge IDs so that we can determine invalid challenge prereqs
all_challenge_ids = {
c.id for c in Challenges.query.with_entities(Challenges.id).all()
}
prereqs = set(requirements).intersection(all_challenge_ids)
if solve_ids >= prereqs:
pass
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
v-for="requirement in requirements.prerequisites"
:key="requirement"
>
<td>{{ getChallengeById(requirement).name }}</td>
<td>{{ getChallengeNameById(requirement) }}</td>
<td>
<i
role="button"
Expand Down Expand Up @@ -99,8 +99,11 @@ export default {
}
});
},
getChallengeById: function(challenge_id) {
return this.challenges.find(challenge => challenge.id === challenge_id);
getChallengeNameById: function(challenge_id) {
let challenge = this.challenges.find(
challenge => challenge.id === challenge_id
);
return challenge ? challenge.name : "";
},
loadRequirements: function() {
CTFd.fetch(
Expand Down
Loading

0 comments on commit 87711d7

Please sign in to comment.