3333from django .http import FileResponse , HttpResponseRedirect
3434from django .urls import get_resolver , get_script_prefix , reverse
3535from django .utils import timezone
36+ from django .utils .http import url_has_allowed_host_and_scheme
3637from django .utils .translation import gettext as _
3738
3839from dojo .authorization .roles_permissions import Permissions
@@ -1828,17 +1829,6 @@ def user_post_save(sender, instance, created, **kwargs):
18281829 instance .save ()
18291830
18301831
1831- def is_safe_url (url ):
1832- try :
1833- # available in django 3+
1834- from django .utils .http import url_has_allowed_host_and_scheme
1835- except ImportError :
1836- # django < 3
1837- from django .utils .http import is_safe_url as url_has_allowed_host_and_scheme
1838-
1839- return url_has_allowed_host_and_scheme (url , allowed_hosts = None )
1840-
1841-
18421832def get_return_url (request ):
18431833 return_url = request .POST .get ("return_url" , None )
18441834 if return_url is None or not return_url .strip ():
@@ -1862,7 +1852,7 @@ def redirect_to_return_url_or_else(request, or_else):
18621852
18631853def redirect (request , redirect_to ):
18641854 """Only allow redirects to allowed_hosts to prevent open redirects"""
1865- if is_safe_url (redirect_to ):
1855+ if url_has_allowed_host_and_scheme (redirect_to , allowed_hosts = None ):
18661856 return HttpResponseRedirect (redirect_to )
18671857 msg = "invalid redirect, host and scheme not in allowed_hosts"
18681858 raise ValueError (msg )
0 commit comments