diff --git a/AUTHORS b/AUTHORS index f95e3ea15dd..c2fad3204b6 100644 --- a/AUTHORS +++ b/AUTHORS @@ -678,6 +678,7 @@ answer newbie questions, and generally made Django that much better: Preston Holmes Preston Timmons Priyansh Saxena + Przemysław Suliga Rachel Tobin Rachel Willmer Radek Švarz diff --git a/django/utils/http.py b/django/utils/http.py index 4558c6874a8..caaab4f9e56 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -298,6 +298,8 @@ def is_safe_url(url, allowed_hosts, require_https=False): return False if allowed_hosts is None: allowed_hosts = set() + elif isinstance(allowed_hosts, str): + allowed_hosts = {allowed_hosts} # Chrome treats \ completely as / in paths but it could be part of some # basic auth credentials so we need to check both URLs. return (_is_safe_url(url, allowed_hosts, require_https=require_https) and diff --git a/tests/utils_tests/test_http.py b/tests/utils_tests/test_http.py index 86fcff9d8e2..05b43c814f2 100644 --- a/tests/utils_tests/test_http.py +++ b/tests/utils_tests/test_http.py @@ -165,6 +165,10 @@ class IsSafeURLTests(unittest.TestCase): # Basic auth without host is not allowed. self.assertIs(is_safe_url(r'http://testserver\@example.com', allowed_hosts=None), False) + def test_allowed_hosts_str(self): + self.assertIs(is_safe_url('http://good.com/good', allowed_hosts='good.com'), True) + self.assertIs(is_safe_url('http://good.co/evil', allowed_hosts='good.com'), False) + def test_secure_param_https_urls(self): secure_urls = ( 'https://example.com/p',