@@ -105,9 +105,16 @@ def login(request,
105105 came_from = settings .LOGIN_REDIRECT_URL
106106
107107 # Ensure the user-originating redirection url is safe.
108- if not is_safe_url (url = came_from , allowed_hosts = {request .get_host ()}):
108+ # By setting SAML_ALLOWED_HOSTS in settings.py the user may provide a list of "allowed"
109+ # hostnames for post-login redirects, much like one would specify ALLOWED_HOSTS .
110+ # If this setting is absent, the default is to use the hostname that was used for the current
111+ # request.
112+ saml_allowed_hosts = set (getattr (settings , 'SAML_ALLOWED_HOSTS' , [request .get_host ()]))
113+
114+ if not is_safe_url (url = came_from , allowed_hosts = saml_allowed_hosts ):
109115 came_from = settings .LOGIN_REDIRECT_URL
110116
117+
111118 # if the user is already authenticated that maybe because of two reasons:
112119 # A) He has this URL in two browser windows and in the other one he
113120 # has already initiated the authenticated session.
@@ -348,7 +355,15 @@ def assertion_consumer_service(request,
348355 if not relay_state :
349356 logger .warning ('The RelayState parameter exists but is empty' )
350357 relay_state = default_relay_state
351- if not is_safe_url (url = relay_state , allowed_hosts = {request .get_host ()}):
358+
359+ # Ensure the user-originating redirection url is safe.
360+ # By setting SAML_ALLOWED_HOSTS in settings.py the user may provide a list of "allowed"
361+ # hostnames for post-login redirects, much like one would specify ALLOWED_HOSTS .
362+ # If this setting is absent, the default is to use the hostname that was used for the current
363+ # request.
364+ saml_allowed_hosts = set (getattr (settings , 'SAML_ALLOWED_HOSTS' , [request .get_host ()]))
365+
366+ if not is_safe_url (url = relay_state , allowed_hosts = saml_allowed_hosts ):
352367 relay_state = settings .LOGIN_REDIRECT_URL
353368 logger .debug ('Redirecting to the RelayState: %s' , relay_state )
354369 return HttpResponseRedirect (relay_state )
0 commit comments