diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2013-08-13 11:00:13 -0500 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2013-08-13 11:00:13 -0500 |
| commit | ec67af0bd609c412b76eaa4cc89968a2a8e5ad6a (patch) | |
| tree | 3fcd042c80623b86f1606778e6accc259f4ae353 | |
| parent | b50be6857ca6779a190e8e42127512618d52591d (diff) | |
Fixed is_safe_url() to reject URLs that use a scheme other than HTTP/S.
This is a security fix; disclosure to follow shortly.
| -rw-r--r-- | django/contrib/auth/tests/views.py | 8 | ||||
| -rw-r--r-- | django/utils/http.py | 7 |
2 files changed, 10 insertions, 5 deletions
diff --git a/django/contrib/auth/tests/views.py b/django/contrib/auth/tests/views.py index 603d380e9d..6d7029bae8 100644 --- a/django/contrib/auth/tests/views.py +++ b/django/contrib/auth/tests/views.py @@ -309,7 +309,8 @@ class LoginTest(AuthViewsTestCase): for bad_url in ('http://example.com', 'https://example.com', 'ftp://exampel.com', - '//example.com'): + '//example.com', + 'javascript:alert("XSS")'): nasty_url = '%(url)s?%(next)s=%(bad_url)s' % { 'url': login_url, @@ -330,6 +331,7 @@ class LoginTest(AuthViewsTestCase): '/view?param=ftp://exampel.com', 'view/?param=//example.com', 'https:///', + 'HTTPS:///', '//testserver/', '/url%20with%20spaces/'): # see ticket #12534 safe_url = '%(url)s?%(next)s=%(good_url)s' % { @@ -467,7 +469,8 @@ class LogoutTest(AuthViewsTestCase): for bad_url in ('http://example.com', 'https://example.com', 'ftp://exampel.com', - '//example.com'): + '//example.com', + 'javascript:alert("XSS")'): nasty_url = '%(url)s?%(next)s=%(bad_url)s' % { 'url': logout_url, 'next': REDIRECT_FIELD_NAME, @@ -486,6 +489,7 @@ class LogoutTest(AuthViewsTestCase): '/view?param=ftp://exampel.com', 'view/?param=//example.com', 'https:///', + 'HTTPS:///', '//testserver/', '/url%20with%20spaces/'): # see ticket #12534 safe_url = '%(url)s?%(next)s=%(good_url)s' % { diff --git a/django/utils/http.py b/django/utils/http.py index d2e4eb5adb..21c84dc821 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -228,11 +228,12 @@ else: def is_safe_url(url, host=None): """ Return ``True`` if the url is a safe redirection (i.e. it doesn't point to - a different host). + a different host and uses a safe scheme). Always returns ``False`` on an empty url. """ if not url: return False - netloc = urlparse.urlparse(url)[1] - return not netloc or netloc == host + url_info = urlparse.urlparse(url) + return (not url_info[1] or url_info[1] == host) and \ + (not url_info[0] or url_info[0] in ['http', 'https']) |
