-
Notifications
You must be signed in to change notification settings - Fork 0
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
[CM 27] Created CM to Json Parsing Endpoint #23
base: main
Are you sure you want to change the base?
Conversation
parsed_json, output_path = parser2.to_json(school_name, course_name, term, start_date, class_levels, student_levels, root) | ||
return jsonify({"parsed_data": parsed_json, "file_saved": output_path}), 200 | ||
except Exception as e: | ||
return jsonify({"error": str(e)}), 500 |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Stack trace information
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 4 months ago
To fix the problem, we need to ensure that detailed error information is not exposed to the user. Instead, we should log the detailed error information on the server and return a generic error message to the user. This can be achieved by modifying the exception handling block to log the exception and return a generic error message.
- Modify the exception handling block in the
parse_text
function to log the exception using Python'slogging
module. - Return a generic error message to the user instead of the detailed exception message.
-
Copy modified line R68 -
Copy modified line R70 -
Copy modified lines R143-R144
@@ -67,3 +67,5 @@ | ||
|
||
import logging | ||
app = Flask(__name__) | ||
app.logger.setLevel(logging.ERROR) | ||
|
||
@@ -140,3 +142,4 @@ | ||
except Exception as e: | ||
return jsonify({"error": str(e)}), 500 | ||
app.logger.error("Exception occurred", exc_info=True) | ||
return jsonify({"error": "An internal error has occurred!"}), 500 | ||
|
"fillcolor": "#{}".format(style_match.group(4)) | ||
} | ||
elif parse_mode == "CLASS_LEVEL": | ||
level_match = re.search(r"\s*([A-Za-z-_\s]+): #([A-Za-z0-9]+)", line) |
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data High
regular expression
user-provided value
level_match = re.search(r"\s*([A-Za-z-_\s]+): #([A-Za-z0-9]+)", line) | ||
class_levels.append({"name": level_match.group(1), "color": "#{}".format(level_match.group(2))}) | ||
elif parse_mode == "STUDENT_LEVEL": | ||
level_match = re.search(r"\s*([A-Za-z-_\s]+): #([A-Za-z0-9]+)", line) |
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data High
regular expression
user-provided value
level_match = re.search(r"\s*([A-Za-z-_\s]+): #([A-Za-z0-9]+)", line) | ||
student_levels.append({"name": level_match.group(1), "color": "#{}".format(level_match.group(2))}) | ||
elif parse_mode == "NODE": | ||
node_match = re.search(r"(\s+)([A-Za-z0-9\-\s\\/]+) \[([A-Za-z0-9]+), Week([0-9]+)]", line) |
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data High
regular expression
user-provided value
This
regular expression
user-provided value
} | ||
|
||
output_path = 'data/{}_{}.json'.format(school_name, course_name) | ||
with open(output_path, 'w', encoding='utf-8') as json_out_file: |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 4 months ago
To fix the problem, we need to ensure that the file paths constructed from user input are validated and sanitized to prevent path traversal attacks. We can achieve this by normalizing the paths and ensuring they remain within a designated safe directory.
- Normalize the constructed file path using
os.path.normpath
to remove any ".." segments. - Check that the normalized path starts with the intended base directory.
- Use
secure_filename
to sanitize the file names.
-
Copy modified lines R140-R143
@@ -139,3 +139,6 @@ | ||
|
||
output_path = 'data/{}_{}.json'.format(school_name, course_name) | ||
base_path = 'data' | ||
output_path = os.path.normpath(os.path.join(base_path, '{}_{}.json'.format(school_name, course_name))) | ||
if not output_path.startswith(base_path): | ||
raise Exception("Invalid file path") | ||
with open(output_path, 'w', encoding='utf-8') as json_out_file: |
-
Copy modified lines R134-R135 -
Copy modified lines R146-R147
@@ -133,4 +133,4 @@ | ||
content = request.data.decode('utf-8') | ||
school_name = request.args.get("school_name", "Berkeley") | ||
course_name = request.args.get("course_name", "CS10") | ||
school_name = secure_filename(request.args.get("school_name", "Berkeley")) | ||
course_name = secure_filename(request.args.get("course_name", "CS10")) | ||
try: | ||
@@ -145,4 +145,4 @@ | ||
def parse(): | ||
school_name = request.args.get("school_name", "Berkeley") | ||
course_name = request.form.get("course_name", "CS10") | ||
school_name = secure_filename(request.args.get("school_name", "Berkeley")) | ||
course_name = secure_filename(request.form.get("course_name", "CS10")) | ||
parser2.generate_map(school_name=secure_filename(school_name), course_name=secure_filename(course_name), render=False) |
Jira Ticket
Jira Ticket
Description
Type of Change
Changes
Testing
Checklist
<ticket-id>/<brief-description-of-change>
[<ticket-id>] <brief-description-of-change>
Screenshots/Video
Additional Notes