Skip to content

Commit 0d9253b

Browse files
Copilotkuboschek
andcommitted
Fix login redirect to respect 'next' parameter with security validation
Co-authored-by: kuboschek <1071495+kuboschek@users.noreply.github.com>
1 parent fdd703a commit 0d9253b

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

custom_auth/views.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.shortcuts import render
88
from django.urls import reverse
99
from django.utils.decorators import method_decorator
10+
from django.utils.http import url_has_allowed_host_and_scheme
1011
from django.views.decorators.cache import never_cache
1112
from django.views.decorators.csrf import csrf_protect
1213
from django.views.decorators.debug import sensitive_post_parameters
@@ -47,7 +48,15 @@ def email_token_login(request: HttpRequest) -> HttpResponse:
4748
if res is not None:
4849
login(request, res)
4950
next_url = request.POST.get("next", None)
50-
return HttpResponseRedirect(next_url)
51+
# Validate the next URL to prevent open redirects
52+
if next_url and url_has_allowed_host_and_scheme(
53+
url=next_url,
54+
allowed_hosts={request.get_host()},
55+
require_https=request.is_secure(),
56+
):
57+
return HttpResponseRedirect(next_url)
58+
else:
59+
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
5160
else:
5261
return render(request, "auth/token_login.html", context={"error": True})
5362

0 commit comments

Comments
 (0)