-
Notifications
You must be signed in to change notification settings - Fork 0
[Security] Fix CodeQL alert #31: Uncontrolled data used in path expression #99
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -46,7 +46,10 @@ def get_user_file(user_id, filename): | |||||||||
| @app.route('/image') | ||||||||||
| def serve_image(): | ||||||||||
| img_name = request.args.get('name') | ||||||||||
| img_path = "./static/images/" + img_name | ||||||||||
| base_dir = os.path.realpath("./static/images/") | ||||||||||
| img_path = os.path.realpath(os.path.join(base_dir, img_name)) | ||||||||||
| if not img_path.startswith(base_dir): | ||||||||||
| return 'Access denied', 403 | ||||||||||
|
Comment on lines
+51
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Path traversal bypass via The
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||
|
|
||||||||||
| return send_file(img_path) | ||||||||||
|
|
||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path traversal check bypassable via sibling directory prefix
High Severity
The
startswithcheck againstbase_dircan be bypassed becauseos.path.realpathstrips trailing slashes. Ifbase_dirresolves to e.g./app/static/images, a request for a file in a sibling directory like/app/static/images_evil/secret.txtwould pass theimg_path.startswith(base_dir)check. The comparison needs to ensure the path is within the directory by appendingos.septobase_dir(or also allowing an exact match withbase_diritself).