33
33
from django .http import FileResponse , HttpResponseRedirect
34
34
from django .urls import get_resolver , get_script_prefix , reverse
35
35
from django .utils import timezone
36
+ from django .utils .http import url_has_allowed_host_and_scheme
36
37
from django .utils .translation import gettext as _
37
38
38
39
from dojo .authorization .roles_permissions import Permissions
@@ -1828,17 +1829,6 @@ def user_post_save(sender, instance, created, **kwargs):
1828
1829
instance .save ()
1829
1830
1830
1831
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
-
1842
1832
def get_return_url (request ):
1843
1833
return_url = request .POST .get ("return_url" , None )
1844
1834
if return_url is None or not return_url .strip ():
@@ -1862,7 +1852,7 @@ def redirect_to_return_url_or_else(request, or_else):
1862
1852
1863
1853
def redirect (request , redirect_to ):
1864
1854
"""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 ):
1866
1856
return HttpResponseRedirect (redirect_to )
1867
1857
msg = "invalid redirect, host and scheme not in allowed_hosts"
1868
1858
raise ValueError (msg )
0 commit comments