-
Notifications
You must be signed in to change notification settings - Fork 0
[Security] Fix CodeQL alert #30: Uncontrolled data used in path expression #98
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 |
|---|---|---|
|
|
@@ -22,7 +22,10 @@ def read_file(): | |
| @app.route('/view') | ||
| def view_document(): | ||
| doc = request.args.get('doc') | ||
| path = f"/documents/{doc}" | ||
| base_dir = os.path.realpath("/documents/") | ||
| path = os.path.realpath(os.path.join(base_dir, doc)) | ||
| if not path.startswith(base_dir): | ||
|
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 fix bypassed via
|
||
| return 'Access denied', 403 | ||
|
|
||
| with open(path) as file: | ||
| return file.read() | ||
|
|
||


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 bypass via
startswithcheck without trailing separatoros.path.realpath("/documents/")strips the trailing slash, yieldingbase_dir = "/documents". The checkpath.startswith(base_dir)on line 27 will therefore also match paths in sibling directories whose names share the same prefix (e.g.,/documents_secret/). An attacker supplyingdoc=../documents_secret/passwdwould producepath = "/documents_secret/passwd", which passes"/documents_secret/passwd".startswith("/documents")→True, bypassing the security check and reading arbitrary files.The fix is to ensure the resolved path starts with
base_dir + os.sep(or equalsbase_direxactly).Was this helpful? React with 👍 or 👎 to provide feedback.