|
26 | 26 | from django.http import Http404, HttpResponse |
27 | 27 | from django.shortcuts import render |
28 | 28 | from django.template import TemplateDoesNotExist |
29 | | -from django.utils.http import is_safe_url |
30 | 29 | from django.views.decorators.csrf import csrf_exempt |
31 | 30 | from django.views.decorators.http import require_POST |
32 | 31 | from saml2 import BINDING_HTTP_REDIRECT, BINDING_HTTP_POST |
|
55 | 54 | from .overrides import Saml2Client |
56 | 55 | from .signals import post_authenticated |
57 | 56 | from .utils import (available_idps, fail_acs_response, get_custom_setting, |
58 | | - get_idp_sso_supported_bindings, get_location) |
| 57 | + get_idp_sso_supported_bindings, get_location, |
| 58 | + validate_referral_url) |
59 | 59 |
|
60 | 60 | try: |
61 | 61 | from django.contrib.auth.views import LogoutView |
@@ -104,17 +104,7 @@ def login(request, |
104 | 104 | if not came_from: |
105 | 105 | logger.warning('The next parameter exists but is empty') |
106 | 106 | came_from = settings.LOGIN_REDIRECT_URL |
107 | | - |
108 | | - # Ensure the user-originating redirection url is safe. |
109 | | - # By setting SAML_ALLOWED_HOSTS in settings.py the user may provide a list of "allowed" |
110 | | - # hostnames for post-login redirects, much like one would specify ALLOWED_HOSTS . |
111 | | - # If this setting is absent, the default is to use the hostname that was used for the current |
112 | | - # request. |
113 | | - saml_allowed_hosts = set(getattr(settings, 'SAML_ALLOWED_HOSTS', [request.get_host()])) |
114 | | - |
115 | | - if not is_safe_url(url=came_from, allowed_hosts=saml_allowed_hosts): |
116 | | - came_from = settings.LOGIN_REDIRECT_URL |
117 | | - |
| 107 | + came_from = validate_referral_url(request, came_from) |
118 | 108 |
|
119 | 109 | # if the user is already authenticated that maybe because of two reasons: |
120 | 110 | # A) He has this URL in two browser windows and in the other one he |
@@ -358,16 +348,8 @@ def assertion_consumer_service(request, |
358 | 348 | if not relay_state: |
359 | 349 | logger.warning('The RelayState parameter exists but is empty') |
360 | 350 | relay_state = default_relay_state |
361 | | - |
362 | | - # Ensure the user-originating redirection url is safe. |
363 | | - # By setting SAML_ALLOWED_HOSTS in settings.py the user may provide a list of "allowed" |
364 | | - # hostnames for post-login redirects, much like one would specify ALLOWED_HOSTS . |
365 | | - # If this setting is absent, the default is to use the hostname that was used for the current |
366 | | - # request. |
367 | | - saml_allowed_hosts = set(getattr(settings, 'SAML_ALLOWED_HOSTS', [request.get_host()])) |
368 | | - |
369 | | - if not is_safe_url(url=relay_state, allowed_hosts=saml_allowed_hosts): |
370 | | - relay_state = settings.LOGIN_REDIRECT_URL |
| 351 | + relay_state = validate_referral_url(request, relay_state) |
| 352 | + |
371 | 353 | logger.debug('Redirecting to the RelayState: %s', relay_state) |
372 | 354 | return HttpResponseRedirect(relay_state) |
373 | 355 |
|
|
0 commit comments