Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

don't allow signin view redirect to different host #541

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions userena/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ def test_signin_view_success(self):
'next': '/accounts/'})
self.assertRedirects(response, '/accounts/')

def test_signin_view_with_unsafe_next(self):
"""
A ``POST`` to signin view with unsafe "next" parameter which
redirects to a different host, this should raise status forbidden
"""
response = self.client.post(reverse('userena_signin'),
data={'identification': '[email protected]',
'password': 'blowfish',
'next': 'http://example.com'})
self.assertEqual(response.status_code, 403)

def test_signin_view_with_invalid_next(self):
"""
If the value of "next" is not a real URL, this should not raise
Expand Down
5 changes: 4 additions & 1 deletion userena/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from django.views.generic.list import ListView
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.utils.http import is_safe_url
from django.utils.translation import ugettext as _
from django.http import Http404, HttpResponseRedirect
from django.http import Http404, HttpResponseRedirect, HttpResponseForbidden

from userena.forms import (SignupForm, SignupFormOnlyEmail, AuthenticationForm,
ChangeEmailForm, EditProfileForm)
Expand Down Expand Up @@ -457,6 +458,8 @@ def signin(request, auth_form=AuthenticationForm,
redirect_to = redirect_signin_function(
request.GET.get(redirect_field_name,
request.POST.get(redirect_field_name)), user)
if not is_safe_url(url=redirect_to, host=request.get_host()):
return HttpResponseForbidden("Redirect to %r forbidden" % redirect_to) # noqa
return HttpResponseRedirect(redirect_to)
else:
return redirect(reverse('userena_disabled',
Expand Down