Skip to content

Commit

Permalink
Fix an open redirect vulnerability
Browse files Browse the repository at this point in the history
  • Loading branch information
Koert van der Veer authored and cyface committed Oct 7, 2021
1 parent 0ccdfd7 commit 0a0f7ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
26 changes: 26 additions & 0 deletions termsandconditions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,32 @@ def test_accept(self):
)
self.assertContains(accept_version_post_response, "Secure")

def test_accept_redirect_safe(self):
# Pre-accept terms 2 and 3
UserTermsAndConditions.objects.create(user=self.user1, terms=self.terms2)
UserTermsAndConditions.objects.create(user=self.user1, terms=self.terms3)

LOGGER.debug("Test user1 login for test_accept_redirect")
login_response = self.client.login(username="user1", password="user1password")
self.assertTrue(login_response)

LOGGER.debug("Test /terms/accept/site-terms/1/ post")
accept_response = self.client.post(
"/terms/accept/", {"terms": 1, "returnTo": "/secure/"}, follow=True
)
self.assertRedirects(accept_response, "/secure/")

def test_accept_redirect_unsafe(self):
# Pre-accept terms 2 and 3
UserTermsAndConditions.objects.create(user=self.user1, terms=self.terms2)
UserTermsAndConditions.objects.create(user=self.user1, terms=self.terms3)

LOGGER.debug("Test /terms/accept/contrib-terms/3/ post")
accept_response = self.client.post(
"/terms/accept/", {"terms": 3, "returnTo": "http://attacker/"}, follow=False
)
self.assertRedirects(accept_response, "/")

def test_accept_store_ip_address(self):
"""Test with IP address storage setting true (default)"""
self.client.login(username="user1", password="user1password")
Expand Down
6 changes: 6 additions & 0 deletions termsandconditions/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Django Views for the termsandconditions module"""
from urllib.parse import urlparse

# pylint: disable=E1120,R0901,R0904
from django.contrib.auth.models import User
Expand Down Expand Up @@ -79,6 +80,11 @@ def post(self, request, *args, **kwargs):
return_url = request.POST.get("returnTo", "/")
terms_ids = request.POST.getlist("terms")

parsed = urlparse(return_url)
if parsed.hostname and parsed.hostname not in settings.ALLOWED_HOSTS:
# Make sure the return url is a relative path or a trusted hostname
return_url = '/'

if not terms_ids: # pragma: nocover
return HttpResponseRedirect(return_url)

Expand Down

0 comments on commit 0a0f7ac

Please sign in to comment.