logo

Database

Python Django Open Redirect

Description

Detects open redirect vulnerabilities in Django applications where user-controlled input is passed directly to redirect functions without proper validation. This could allow attackers to redirect users to malicious websites through manipulation of redirect URLs.

Weakness:

156 - Uncontrolled external site redirect

Category: Deceptive Interactions

Detection Strategy

    • Checks if Django-related imports are present in the codebase

    • Identifies usage of dangerous redirect functions like HttpResponseRedirect, redirect, or similar Django redirect functions

    • Locates function calls where the redirect URL parameter (first argument) contains user-controlled input

    • Verifies the URL parameter is not properly sanitized or validated before use

    • Reports a vulnerability when unsafe user input flows into redirect functions without proper validation

Vulnerable code example

from django.shortcuts import redirect

def unsafe_redirect(request):
    # VULNERABLE: Directly using user-controlled URL from GET parameter
    target_url = request.GET.get('next', '/')
    return redirect(target_url)

✅ Secure code example

from django.shortcuts import redirect
from django.utils.http import url_has_allowed_host_and_scheme

def safe_redirect(request):
    # Get target URL from request parameters with a safe default
    target_url = request.GET.get('next', '/')
    
    # Validate URL using Django's security check against allowed hosts...