Skip to content

Commit

Permalink
Fixed is_safe_url() to reject URLs that use a scheme other than HTTP/S.
Browse files Browse the repository at this point in the history
This is a security fix; disclosure to follow shortly.
  • Loading branch information
jacobian committed Aug 13, 2013
1 parent 4f470f5 commit 79594b4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions django/contrib/auth/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ def test_security_check(self, password='password'):
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,
Expand All @@ -468,6 +469,7 @@ def test_security_check(self, password='password'):
'/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' % {
Expand Down Expand Up @@ -662,7 +664,8 @@ def test_security_check(self, password='password'):
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,
Expand All @@ -681,6 +684,7 @@ def test_security_check(self, password='password'):
'/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' % {
Expand Down
7 changes: 4 additions & 3 deletions django/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,12 @@ def same_origin(url1, url2):
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 = urllib_parse.urlparse(url)[1]
return not netloc or netloc == host
url_info = urllib_parse.urlparse(url)
return (not url_info.netloc or url_info.netloc == host) and \
(not url_info.scheme or url_info.scheme in ['http', 'https'])

0 comments on commit 79594b4

Please sign in to comment.