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.
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...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.