Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions flask.py
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.
Comment on lines +16 to +18
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 The filename is 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.

Suggested change
# !!! VULNERABLE LINE !!!
# The user's input is directly joined to the directory path.
# VULNERABLE LINE !!!
# The user's input is directly joined to the directory path.
file_path = os.path.join(UPLOAD_DIR, os.path.basename(filename))

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)
19 changes: 19 additions & 0 deletions secure_code.py
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 The hostname is directly used to construct a command, which can lead to a command injection vulnerability. A malicious user could execute arbitrary commands on the server.

Suggested change
# 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}"
# VULNERABLE: Building a command string and using shell=True
# The f-string directly inserts the user's text into the command.
command = ["ping", "-c", "3", hostname]


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
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 Using shell=True with user-provided input is extremely dangerous as it can allow for command injection attacks.

Suggested change
# (like /bin/sh or cmd.exe) to be interpreted.
try:
subprocess.run(command, shell=True, check=True)
# (like /bin/sh or cmd.exe) to be interpreted.
try:
subprocess.run(command, shell=False, check=True)
except subprocess.CalledProcessError as e:

except subprocess.CalledProcessError as e:
print(f"Command failed: {e}")

print("--- Ping finished ---")