Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit a60c7ae

Browse files
committed
minor improvements
1 parent 00e5d2b commit a60c7ae

File tree

5 files changed

+28
-37
lines changed

5 files changed

+28
-37
lines changed

hints/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from flask import Flask
22

3-
# from ctf.error_handlers import register_error_handlers # Adjust the
43
# import based on your project structure
54

6-
75
def create_app():
86
print("Creating app")
97
app = Flask(
@@ -27,19 +25,8 @@ def create_app():
2725
from . import routes
2826

2927
app.register_blueprint(routes.bp)
30-
# register_error_handlers(app)
3128
# app.config['referrer_policy'] = 'strict-origin-when-cross-origin'
3229
return app
3330

34-
35-
# app = Flask(
36-
# __name__,
37-
# static_folder="static",
38-
# static_url_path="/",
39-
# template_folder="templates",
40-
# )
41-
42-
43-
# register_error_handlers(app)
4431
# app.config['referrer_policy'] = 'strict-origin-when-cross-origin'
4532
from hints import routes # nopep8

hints/routes.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import datetime
2-
2+
import uuid
33
from flask import (Blueprint, flash, redirect, render_template, request,
44
session, url_for)
55
from markupsafe import escape
@@ -23,7 +23,9 @@ def flags():
2323
session["current_stage"] = 1
2424
if "hint_index" not in session:
2525
session["hint_index"] = 0
26-
26+
if "token" not in session:
27+
session["token"] = str(uuid.uuid4()) # generate a random token
28+
2729
current_stage = session["current_stage"]
2830
submitted_flags = session["submitted_flags"]
2931
hint_index = session["hint_index"]
@@ -35,8 +37,7 @@ def flags():
3537
if "submit_flag" in request.form:
3638
submitted_flag = request.form.get("flag")
3739
submitted_flag = escape(submitted_flag.strip())
38-
# make sure its ascii only and not invalid
39-
if not submitted_flag.isascii():
40+
if not submitted_flag.isascii(): # make sure its ascii only and not invalid
4041
flash("Invalid flag. Please try again.", "danger")
4142
return redirect(url_for("ctf.flags"))
4243

@@ -56,7 +57,7 @@ def flags():
5657
"You have completed all the stages. Congratulations!",
5758
"success")
5859
return render_template(
59-
"message.html",
60+
"error.html",
6061
title="Congratulations!",
6162
message="You have completed all the stages of the CTF. ",
6263
)
@@ -87,7 +88,7 @@ def flags():
8788
flash(
8889
"You have completed all the stages. Congratulations!", "success", )
8990
return render_template(
90-
"message.html",
91+
"error.html",
9192
title="Congratulations!",
9293
message="You have completed all the stages of the CTF. ",
9394
)
@@ -128,18 +129,20 @@ def flags():
128129

129130

130131
@bp.route("/", methods=["GET"]) # also for index
131-
@bp.route("/index", methods=["GET"])
132132
@bp.route("/home", methods=["GET"])
133+
@bp.route("/index", methods=["GET"])
133134
@bp.route("/index.html", methods=["GET"])
134135
def index():
135-
# initialize session variables
136+
# initialize session variables if they don't exist
136137
if "submitted_flags" not in session:
137138
session["submitted_flags"] = []
138139
if "current_stage" not in session:
139140
session["current_stage"] = 1
140141
if "hint_index" not in session:
141142
session["hint_index"] = 0
142-
143+
if "token" not in session:
144+
session["token"] = str(uuid.uuid4()) # generate a random token
145+
143146
flash("Welcome to the CTF, please read the following:", "info")
144147
brief = """
145148
Using this site is not required to solve the CTF challenge and is not a part of the CTF challenge itself, but a tool to help you keep track of your progress. You need to find the flags on your own and not via this site itself. Good luck!
@@ -158,10 +161,11 @@ def index():
158161
@bp.route("/reset", methods=["GET"])
159162
@bp.route("/reset.html", methods=["GET"])
160163
def restart():
161-
session.clear()
162-
flash("Progress reset. You are back to Stage 1.", "info")
164+
[session.pop(key, None)
165+
for key in list(session.keys())] # clear the session
166+
flash("Progress reset. You are back to the first stage.", "info")
163167
# reroute to index
164-
return redirect(url_for("ctf.index"))
168+
return redirect(url_for("ctf.index"), code=301)
165169

166170

167171
# error pages
@@ -170,12 +174,12 @@ def page_not_found(error):
170174
return render_template("404.html"), 404
171175

172176

173-
# nice page for anything else using message.html and return 500
177+
# nice page for anything else using error.html and return 500
174178
@bp.app_errorhandler(500)
175179
def internal_server_error(error):
176180
return (
177181
render_template(
178-
"message.html",
182+
"error.html",
179183
title="500 Internal Server Error",
180184
message="Please try again later",
181185
),
@@ -184,7 +188,7 @@ def internal_server_error(error):
184188

185189

186190
@bp.context_processor
187-
def inject_today_date():
191+
def inject_year():
188192
"""
189193
used for the footer to display the current year
190194
"""

hints/static/js/code.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
if (window.history.replaceState) {
2-
1+
if (window.history.replaceState) { // hacky way to prevent form resubmission on page refresh (avoiding reveal of hints if button not pressed but F5 is)
32
window.history.replaceState(null, null, window.location.href);
4-
53
}
File renamed without changes.

hints/templates/flags.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@
99
{% endblock %}
1010
{% block content %}
1111
<div class="container mt-5">
12-
<h1 class="text-center">CTF Platform</h1>
12+
<h1 class="text-center">CTFlask</h1>
1313
<div class="mt-4">
1414
<h3>Stage {{ stage }}</h3>
15-
<h5>Notes</h5>
16-
{% if notes is defined and notes %}
17-
<p>{{ notes }}</p>
18-
{% endif %}
19-
15+
2016
<form method="POST">
2117
<div class="form-group">
2218
<label for="flag">Enter Flag:</label>
2319
<input type="text" id="flag" name="flag" class="form-control" placeholder="flag{...}" required>
2420
</div>
2521
<button type="submit" name="submit_flag" class="btn btn-primary">Submit Flag</button>
2622
</form>
23+
24+
<h5>Notes</h5>
25+
{% if notes is defined and notes %}
26+
<p>{{ notes }}</p>
27+
{% endif %}
28+
2729
</div>
2830
{% with messages = get_flashed_messages(with_categories=true) %}
2931
{% if messages is defined and messages %}

0 commit comments

Comments
 (0)