Skip to content
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
5 changes: 4 additions & 1 deletion vulnerable_path_traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
def download_file():
filename = request.args.get('file')

file_path = os.path.join('/var/www/uploads/', filename)
base_dir = '/var/www/uploads/'
file_path = os.path.realpath(os.path.join(base_dir, filename))
if not file_path.startswith(os.path.realpath(base_dir)):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Path traversal fix is bypassable due to missing trailing separator in startswith check

The startswith check on line 12 is vulnerable to a directory-prefix bypass. os.path.realpath('/var/www/uploads/') strips the trailing slash, returning /var/www/uploads. An attacker can supply filename=../uploads_evil/secret.txt, which resolves to /var/www/uploads_evil/secret.txt. This passes the startswith('/var/www/uploads') check because the string /var/www/uploads_evil/... does start with /var/www/uploads. The fix must append os.sep to the resolved base directory to ensure only true children of the base directory are matched.

Suggested change
if not file_path.startswith(os.path.realpath(base_dir)):
if not file_path.startswith(os.path.realpath(base_dir) + os.sep):
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown

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 names

High Severity

The startswith check is bypassable because os.path.realpath(base_dir) strips the trailing slash from /var/www/uploads/, producing /var/www/uploads. A sibling directory like /var/www/uploads_evil/ would also match startswith('/var/www/uploads'), allowing access to files outside the intended directory. The check needs to compare against the resolved base path with a trailing os.sep appended.

Fix in Cursor Fix in Web

return 'Access denied', 403
return send_file(file_path)

@app.route('/read')
Expand Down
Loading