Skip to content

Commit 046ebf4

Browse files
authored
User specified hostnames for redirect
Post-login redirect URL's may often be generated from a client that is accessing an API, however the current implementation only allows redirect urls to point to the same server. This results in the need for confusing redirect-to-another-route-so-we-can-redirect elsewhere type cases. This fixes that by allowing the user to list a set of domains that may be redirected to. The default is to work as it has in the past - so no end user work is needed on upgrading. However, by specifying a SAML_ALLOWED_HOSTS parameter they can open up the allowable redirect hostnames.
1 parent 6439697 commit 046ebf4

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

djangosaml2/views.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,15 @@ def login(request,
110110
came_from = settings.LOGIN_REDIRECT_URL
111111

112112
# Ensure the user-originating redirection url is safe.
113-
if not is_safe_url_compat(url=came_from, allowed_hosts={request.get_host()}):
113+
# By setting SAML_ALLOWED_HOSTS in settings.py the user may provide a list of "allowed"
114+
# hostnames for post-login redirects, much like one would specify ALLOWED_HOSTS .
115+
# If this setting is absent, the default is to use the hostname that was used for the current
116+
# request.
117+
saml_allowed_hosts = set(getattr(settings, 'SAML_ALLOWED_HOSTS', [request.get_host()]))
118+
if not is_safe_url_compat(url=came_from, allowed_hosts=saml_allowed_hosts):
114119
came_from = settings.LOGIN_REDIRECT_URL
115120

121+
116122
# if the user is already authenticated that maybe because of two reasons:
117123
# A) He has this URL in two browser windows and in the other one he
118124
# has already initiated the authenticated session.

0 commit comments

Comments
 (0)