-
Notifications
You must be signed in to change notification settings - Fork 0
Very secure code. #6
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import os | ||
| from flask import Flask, request, abort | ||
|
|
||
| app = Flask(__name__) | ||
|
|
||
| # The directory where user files are stored | ||
| UPLOAD_DIR = "/app/user_uploads/" | ||
|
|
||
| @app.route('/download') | ||
| def download_file(): | ||
| # Get the filename from a URL parameter (e.g., /download?file=report.pdf) | ||
| filename = request.args.get('file') | ||
|
|
||
| if not filename: | ||
| return "Missing 'file' parameter", 400 | ||
|
|
||
| # !!! VULNERABLE LINE !!! | ||
| # The user's input is directly joined to the directory path. | ||
| file_path = os.path.join(UPLOAD_DIR, filename) | ||
|
|
||
| try: | ||
| # The server attempts to read and return the file | ||
| with open(file_path, 'r') as f: | ||
| return f.read() | ||
| except FileNotFoundError: | ||
| abort(404, "File not found.") | ||
| except Exception: | ||
| abort(500, "An error occurred.") | ||
|
|
||
| if __name__ == '__main__': | ||
| # Note: This is a dev server, not for production. | ||
| app.run(debug=True) | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||||||||||||
| import subprocess | ||||||||||||||||
|
|
||||||||||||||||
| # Get untrusted input from a user | ||||||||||||||||
| hostname = input("Please enter the hostname to ping: ") | ||||||||||||||||
|
|
||||||||||||||||
| # VULNERABLE: Building a command string and using shell=True | ||||||||||||||||
| # The f-string directly inserts the user's text into the command. | ||||||||||||||||
| command = f"ping -c 3 {hostname}" | ||||||||||||||||
|
Comment on lines
+6
to
+8
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. 🔴 The
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| print(f"--- Running command: {command} ---") | ||||||||||||||||
|
|
||||||||||||||||
| # shell=True tells Python to pass the entire string to the system's shell | ||||||||||||||||
| # (like /bin/sh or cmd.exe) to be interpreted. | ||||||||||||||||
| try: | ||||||||||||||||
| subprocess.run(command, shell=True, check=True) | ||||||||||||||||
|
Comment on lines
+13
to
+15
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. 🔴 Using
Suggested change
|
||||||||||||||||
| except subprocess.CalledProcessError as e: | ||||||||||||||||
| print(f"Command failed: {e}") | ||||||||||||||||
|
|
||||||||||||||||
| print("--- Ping finished ---") | ||||||||||||||||
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.
🔴 The
filenameis directly used to construct a file path, which can lead to a path traversal vulnerability. A malicious user could access sensitive files outside of the intended directory.