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

Commit 8c6b158

Browse files
Completing 1.2.2 - QoL updates, Security and bug fixes
1 parent fb057be commit 8c6b158

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

.idea/csv-editor.xml

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

API/API_FrameWork.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def framework():
2525

2626
# Open files within the same scope as the POST request to ensure they stay open
2727
with open("db.config", "rb") as config_file, open(
28-
"API.json", "rb"
28+
"API.json", "rb"
2929
) as api_file, open(
3030
"Test.csv", "rb"
3131
) as csv_file: # Optional, can be omitted if not needed
@@ -60,6 +60,7 @@ def framework():
6060
code = 500
6161

6262
elif code == 202:
63+
# Attempt to download Server.log
6364
try:
6465
download_response = requests.get(log_download_url)
6566
if download_response.status_code == 200:

DataBase.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,9 @@ def init():
881881
api, username, password, exclusion_titles = temp
882882

883883
if api == "REC":
884-
log.info(f"A request has been made to generate an exam by the user {username}")
884+
log.info(
885+
f"A request has been made to generate an exam by the user {username}"
886+
)
885887
if um.verify_password(username, password):
886888
DATA = exam_generator(username)
887889
if not check_ERROR(DATA):
@@ -901,9 +903,7 @@ def init():
901903
)
902904
DATA = um.add_exclusion_db(username, exclusion_titles, password)
903905
if not check_ERROR(DATA):
904-
log.info(
905-
"Exclusion titles added successfully based on the request"
906-
)
906+
log.info("Exclusion titles added successfully based on the request")
907907
elif api == "RUR":
908908
log.info(
909909
f"A request has been made to remove the user {username} from the database"

flask_server.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ def index():
9999

100100
@app.after_request
101101
def add_security_headers(response):
102-
response.headers.add('X-Content-Type-Options', 'nosniff')
103-
response.headers.add('X-Frame-Options', 'SAMEORIGIN')
104-
response.headers.add('X-XSS-Protection', '1; mode=block')
105-
response.headers.add('Strict-Transport-Security', 'max-age=63072000; includeSubDomains; preload')
106-
response.headers.add('Referrer-Policy', 'same-origin')
107-
response.headers.add('Cross-Origin-Opener-Policy', 'same-origin')
102+
response.headers.add("X-Content-Type-Options", "nosniff")
103+
response.headers.add("X-Frame-Options", "SAMEORIGIN")
104+
response.headers.add("X-XSS-Protection", "1; mode=block")
105+
response.headers.add(
106+
"Strict-Transport-Security", "max-age=63072000; includeSubDomains; preload"
107+
)
108+
response.headers.add("Referrer-Policy", "same-origin")
109+
response.headers.add("Cross-Origin-Opener-Policy", "same-origin")
108110
return response
109111

110112

@@ -150,9 +152,9 @@ def upload_file():
150152

151153
# Validate filenames
152154
if (
153-
not validate_filename(config_file.filename)
154-
or not validate_filename(api_file.filename)
155-
or not validate_filename(csv_file.filename)
155+
not validate_filename(config_file.filename)
156+
or not validate_filename(api_file.filename)
157+
or not validate_filename(csv_file.filename)
156158
):
157159
logger.error(
158160
f"Invalid filename(s). Filename must not contain '..' and must have an allowed extension."
@@ -163,9 +165,9 @@ def upload_file():
163165
)
164166

165167
if (
166-
config_file.filename != ""
167-
and api_file.filename != ""
168-
and csv_file.filename != ""
168+
config_file.filename != ""
169+
and api_file.filename != ""
170+
and csv_file.filename != ""
169171
):
170172

171173
# Get the file names
@@ -187,9 +189,9 @@ def upload_file():
187189
csv_file.save(csv_filename)
188190

189191
if (
190-
os.path.exists("db.config")
191-
and os.path.exists("API.json")
192-
and os.path.exists("Test.csv")
192+
os.path.exists("db.config")
193+
and os.path.exists("API.json")
194+
and os.path.exists("Test.csv")
193195
):
194196
# Return an HTML success message
195197
message = database_thread()
@@ -208,7 +210,11 @@ def upload_file():
208210
logger.error(
209211
f"Invalid message format: {message} with {len(parts)} parts."
210212
)
211-
return "The message does not match the expected format.", 400
213+
tempMessage = "The message does not match the expected format."
214+
return (
215+
f"<html><body><h1>Error</h1><h2>Error Number: 400</h2><p>{tempMessage}</p></body></html>",
216+
400,
217+
)
212218

213219
# Checking if the error number exists in err_codes
214220
if error_number in err_codes:
@@ -247,7 +253,10 @@ def upload_file():
247253
)
248254
elif message == "LOG":
249255
logger.info(f"Successfully received request to download log")
250-
return f"<html><body><h1>Success</h1>{message.replace('SUCCESS', '', 1)}</body></html>", 202
256+
return (
257+
f"<html><body><h1>Success</h1>{message.replace('SUCCESS', '', 1)}</body></html>",
258+
202,
259+
)
251260
else:
252261
logger.info(f"Successfully downloaded exam")
253262
return f"<html><body><h1>Success</h1>{message}</body></html>", 200
@@ -301,7 +310,10 @@ def download_log():
301310
if os.path.exists(log_path):
302311
return send_from_directory(directory=base_path, path="Server.log")
303312
else:
304-
return f"<html><body><h1>Error</h1><h2>Error Number: 404</h2><p>Server.log does not exist.</p></body></html>", 404
313+
return (
314+
f"<html><body><h1>Error</h1><h2>Error Number: 404</h2><p>Server.log does not exist.</p></body></html>",
315+
404,
316+
)
305317

306318

307319
if __name__ == "__main__":

0 commit comments

Comments
 (0)