logo

Database

Python Django Write Path Traversal

Description

This detector identifies Django applications vulnerable to path traversal attacks during file write operations. When user-controlled input is passed directly to file write functions without proper path validation, attackers can use sequences like "../" to write files outside the intended directory, potentially overwriting sensitive system files.

Weakness:

014 - Insecure functionality

Category: Functionality Abuse

Detection Strategy

    Django framework must be imported in the code (checks for django library imports)

    Code must contain file write operations (functions like open(), file.write(), or similar file writing methods)

    User input must flow into the file path parameter of write operations without proper sanitization

    The data flow from user input to the write destination must be traceable through the code

Vulnerable code example

from pathlib import Path
from django.http import HttpResponse
import shutil


def write_file_vulnerable(request):
    filename = request.GET["filename"]
    ...

✅ Secure code example

from pathlib import Path
from django.http import HttpResponse
import shutil
import os

BASE_DIR = Path("uploads")

def write_file_secure(request):...