Skip to content
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

certificate_generator/app/routes.py: fix for potential path manipulation #957

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions certificate_generator/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,32 @@ def render_certificate():
return render_template('download.html', file_name=file_name)


def is_valid_filename(filename):
"""
Check if the filename is valid
- Prevents directory traversal attacks (with / or ..)
- Only allows alphanumeric characters and dots

Args:
filename: str

Returns:
bool - whether the filename is valid (True = valid, False = invalid)
"""
return filename.isalnum() or filename .replace('.', '').isalnum()


@app.route('/download_certificate', methods=['GET'])
def download():
"""
Download the generated certificate
"""
if request.method == "GET":
filename = request.args.get("filename")
if not filename or '..' in filename or not is_valid_filename(filename):
return "Invalid filename", 400
filepath = os.path.join("static/certificates/generated", filename)
if not os.path.isfile(filepath):
return "File not found", 404
return send_file(filepath, as_attachment=True, cache_timeout=0,
attachment_filename=filename)
Loading