Python Flask Write Path Traversal
Description
This detector identifies path traversal vulnerabilities in Flask applications where user-controlled input can be used to write files to arbitrary locations on the filesystem. The vulnerability occurs when file paths are constructed using unsanitized user input, potentially allowing attackers to overwrite system files or write malicious files outside the intended directory.
Detection Strategy
• Reports when Flask library is imported in the Python code
• Reports when file write operations (like open(), file(), or similar file writing functions) are detected
• Reports when the file path or destination for these write operations can be influenced by user input through Flask request data (form data, URL parameters, headers, etc.)
• Reports when there is insufficient validation or sanitization of the user input that determines the file write destination
Vulnerable code example
from flask import Flask, request
from pathlib import Path
app = Flask(__name__)
@app.post("/upload")
def upload():
filename = request.form["filename"]...✅ Secure code example
from flask import Flask, request
from pathlib import Path
from werkzeug.utils import secure_filename
app = Flask(__name__)
@app.post("/upload")
def upload():...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Continuous Hacking Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' hacking team, fill out this contact form.