-
Notifications
You must be signed in to change notification settings - Fork 0
[Security] Fix CodeQL alert #29: Uncontrolled data used in path expression #97
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 |
|---|---|---|
|
|
@@ -14,7 +14,11 @@ def download_file(): | |
| def read_file(): | ||
| file_name = request.args.get('filename', 'default.txt') | ||
|
|
||
| with open(file_name, 'r') as f: | ||
| base_dir = os.path.realpath('/var/www/files/') | ||
| safe_path = os.path.realpath(os.path.join(base_dir, file_name)) | ||
| if not safe_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 bypassable via sibling directory prefixHigh Severity
|
||
| return 'Access denied', 403 | ||
| with open(safe_path, 'r') as f: | ||
| content = f.read() | ||
|
|
||
| return content | ||
|
|
||


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('/var/www/files/')strips the trailing slash, sobase_diris/var/www/files. The checksafe_path.startswith(base_dir)can be bypassed by accessing sibling directories whose names share the same prefix (e.g.,/var/www/files_secret/). For example,filename=../files_secret/passwdresolves to/var/www/files_secret/passwd, which passes thestartswith('/var/www/files')check, allowing reads outside the intended directory.Example bypass
GET /read?filename=../files_secret/sensitive.txtbase_dir=/var/www/filessafe_path=os.path.realpath('/var/www/files/../files_secret/sensitive.txt')=/var/www/files_secret/sensitive.txt'/var/www/files_secret/sensitive.txt'.startswith('/var/www/files')→True→ access grantedWas this helpful? React with 👍 or 👎 to provide feedback.