logo

Database

Python Flask Delete Path Traversal

Description

This detector identifies path traversal vulnerabilities in Flask applications where user-controlled input flows to file deletion operations. Path traversal attacks allow attackers to delete files outside the intended directory by using sequences like "../" to navigate the filesystem, potentially compromising application security or causing denial of service.

Weakness:

082 - Insecurely deleted files

Category: Information Collection

Detection Strategy

    Check if the Flask library is imported in the Python code

    Identify file deletion operations (such as calls to os.remove, os.unlink, pathlib.Path.unlink, or similar file deletion functions)

    Track data flow from Flask request parameters, form data, or route variables to the file deletion operations

    Report a vulnerability when user input can influence the file path being deleted without proper validation or sanitization

Vulnerable code example

from flask import Flask, request
import os
from pathlib import Path

app = Flask(__name__)

@app.route("/delete")
def delete_file():...

✅ Secure code example

from flask import Flask, request, abort
import os
from pathlib import Path
from werkzeug.utils import secure_filename

app = Flask(__name__)

UPLOAD_DIR = "/uploads"...