Skip to content

Commit fc1805a

Browse files
fixed challenges bug
1 parent 188959a commit fc1805a

6 files changed

Lines changed: 45 additions & 336 deletions

File tree

CTFd/plugins/LuaUtils/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ def is_owned_wrapper(*args, **kwargs):
111111
new = function([ret] + list(*args), **kwargs)
112112
return new if new else ret
113113
else:
114-
function(*args, **kwargs)
115-
return f(*args, **kwargs)
114+
new = function(*args, **kwargs)
115+
ret = f(*args, **kwargs)
116+
return new if new else ret
116117
return is_owned_wrapper
117118
return decorator
118119

CTFd/plugins/emailnotifications/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def notif_config():
180180

181181
@admins_only
182182
def notification_post(response):
183-
if response[0].get_json():
183+
if request.method == "GET" and response[0].get_json():
184184
email = get_config("sendEmailNotif")
185185
if email:
186186
send_mail_all_users(response[0].get_json()["data"])
@@ -192,7 +192,7 @@ def notification_post(response):
192192
# put every new user in table
193193
@check_registration_visibility
194194
@ratelimit(method="POST", limit=10, interval=5)
195-
def notif_register():
195+
def notif_register(res):
196196
# add user checkmark for email notifications
197197
if get_current_user():
198198
check = UserNotifs(get_current_user(), False)

CTFd/plugins/hintpointdelay/__init__.py

Lines changed: 22 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ def __init__(self, user, hint):
3333
self.hint = hint.id
3434
self.challenge = hint.challenge_id
3535

36-
def get_modified_challenge_points(challenge):
36+
def get_modified_challenge_points(challenge_id,challenge_value):
3737
user = get_current_user()
3838
hintids = DelayedHints.query.filter(
39-
DelayedHints.challenge == challenge.id,
39+
DelayedHints.challenge == challenge_id,
4040
DelayedHints.user == user.id,
4141
).all()
4242

43-
score = challenge.value
43+
score = challenge_value
4444
if hintids:
4545
for hid in hintids:
4646
hint = Hints.query.filter(
@@ -50,10 +50,10 @@ def get_modified_challenge_points(challenge):
5050

5151
return score
5252

53-
def apply_delayed_hints(challenge):
53+
def apply_delayed_hints(challenge_id):
5454
user = get_current_user()
5555
hintids = DelayedHints.query.filter(
56-
DelayedHints.challenge == challenge.id,
56+
DelayedHints.challenge == challenge_id,
5757
DelayedHints.user == user.id,
5858
).all()
5959

@@ -103,6 +103,7 @@ def apply_delayed_hints(challenge):
103103
)
104104

105105
def load(app):
106+
106107
app.db.create_all()
107108

108109
#jinja globals
@@ -115,7 +116,6 @@ def load(app):
115116
@admins_only
116117
def hintpoint_config():
117118
standard = get_config("hintpointdelay")
118-
119119
if standard:
120120
standard = "enabled"
121121
else:
@@ -141,9 +141,11 @@ def modify_award(res):
141141
user = get_current_user()
142142

143143
Model = get_class_by_tablename(req["type"])
144-
hint = Model.query.filter_by(id=req["target"]).first_or_404()
145-
144+
target = Model.query.filter_by(id=req["target"]).first_or_404()
145+
146+
# replace costly hint with non cost hint
146147
if(req["type"] == "hints"):
148+
hint = target
147149
name = hint.name
148150
description = hint.description
149151
category = hint.category
@@ -175,117 +177,20 @@ def modify_award(res):
175177

176178
db.session.commit()
177179
clear_standings()
178-
179180

180181
run_after_route(app,'api.unlocks_unlock_list',modify_award)
181182

182-
@during_ctf_time_only
183-
@require_verified_emails
184-
@authed_only
185-
def post(self):
186-
req = request.get_json()
187-
user = get_current_user()
188-
189-
target_type = req["type"]
190-
191-
req["user_id"] = user.id
192-
req["team_id"] = user.team_id
193-
194-
Model = get_class_by_tablename(req["type"])
195-
target = Model.query.filter_by(id=req["target"]).first_or_404()
196-
197-
if target_type == "hints":
198-
# We should use the team's score if in teams mode
199-
# user.account gives the appropriate account based on team mode
200-
# Use get_score with admin to get the account's full score value
201-
if target.cost > user.account.get_score(admin=True):
202-
return (
203-
{
204-
"success": False,
205-
"errors": {
206-
"score": "You do not have enough points to unlock this hint"
207-
},
208-
},
209-
400,
210-
)
211-
212-
schema = UnlockSchema()
213-
response = schema.load(req, session=db.session)
214-
215-
if response.errors:
216-
return {"success": False, "errors": response.errors}, 400
183+
def modify_challenge_correct(res):
184+
response = res[0].get_json()
185+
log('registrations',format="########################## {response}",
186+
response= response['data']['status'] )
187+
if (response['success'] and response['data']['status'] == 'correct'):
188+
if not request.is_json:
189+
request_data = request.form
190+
else:
191+
request_data = request.get_json()
217192

218-
# Search for an existing unlock that matches the target and type
219-
# And matches either the requesting user id or the requesting team id
220-
existing = Unlocks.query.filter(
221-
Unlocks.target == req["target"],
222-
Unlocks.type == req["type"],
223-
Unlocks.account_id == user.account_id,
224-
).first()
225-
if existing:
226-
return (
227-
{
228-
"success": False,
229-
"errors": {"target": "You've already unlocked this target"},
230-
},
231-
400,
232-
)
193+
challenge_id = request_data.get("challenge_id")
194+
apply_delayed_hints(challenge_id)
233195

234-
db.session.add(response.data)
235-
236-
award_schema = AwardSchema()
237-
award = {
238-
"user_id": user.id,
239-
"team_id": user.team_id,
240-
"name": target.name,
241-
"description": target.description,
242-
"value": (-target.cost),
243-
"category": target.category,
244-
}
245-
246-
award = award_schema.load(award)
247-
db.session.add(award.data)
248-
db.session.commit()
249-
clear_standings()
250-
251-
response = schema.dump(response.data)
252-
253-
return {"success": True, "data": response.data}
254-
255-
elif target_type == "solutions":
256-
schema = UnlockSchema()
257-
response = schema.load(req, session=db.session)
258-
259-
if response.errors:
260-
return {"success": False, "errors": response.errors}, 400
261-
262-
# Search for an existing unlock that matches the target and type
263-
# And matches either the requesting user id or the requesting team id
264-
existing = Unlocks.query.filter(
265-
Unlocks.target == req["target"],
266-
Unlocks.type == req["type"],
267-
Unlocks.account_id == user.account_id,
268-
).first()
269-
if existing:
270-
return (
271-
{
272-
"success": False,
273-
"errors": {"target": "You've already unlocked this target"},
274-
},
275-
400,
276-
)
277-
278-
db.session.add(response.data)
279-
db.session.commit()
280-
281-
response = schema.dump(response.data)
282-
283-
return {"success": True, "data": response.data}
284-
else:
285-
return (
286-
{
287-
"success": False,
288-
"errors": {"type": "Unknown target type"},
289-
},
290-
400,
291-
)
196+
run_after_route(app,'api.challenges_challenge_attempt',modify_challenge_correct)

CTFd/plugins/userchallenge/__init__.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ def user_config():
108108
# add creation date and user to listing
109109
@admins_only
110110
def challenges_listing(res):
111+
112+
111113
q = request.args.get("q")
112114
field = request.args.get("field")
113115
filters = []
@@ -140,16 +142,17 @@ def challenges_listing(res):
140142
)
141143
)
142144

143-
return merge_text(
144-
res[0],
145-
render_template(
146-
"adminChallenges.html",
147-
challenges=challenges,
148-
total=total,
149-
q=q,
150-
field=field,
151-
),
152-
)
145+
if res[0]:
146+
return merge_text(
147+
res[0],
148+
render_template(
149+
"adminChallenges.html",
150+
challenges=challenges,
151+
total=total,
152+
q=q,
153+
field=field,
154+
),
155+
)
153156

154157
run_after_route(app, "admin.challenges_listing", challenges_listing)
155158

0 commit comments

Comments
 (0)