티스토리 수익 글 보기

티스토리 수익 글 보기

Fixed is_safe_url() to reject URLs that use a scheme other than HTTP/S. · django/django@ec67af0 · GitHub
Skip to content

Commit ec67af0

Browse files
committed
Fixed is_safe_url() to reject URLs that use a scheme other than HTTP/S.
This is a security fix; disclosure to follow shortly.
1 parent b50be68 commit ec67af0

File tree

2 files changed

+10
5
lines changed

2 files changed

+10
5
lines changed

django/contrib/auth/tests/views.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ def test_security_check(self, password='password'):
309309
for bad_url in ('http://example.com',
310310
'https://example.com',
311311
'ftp://exampel.com',
312-
'//example.com'):
312+
'//example.com',
313+
'javascript:alert("XSS")'):
313314

314315
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
315316
'url': login_url,
@@ -330,6 +331,7 @@ def test_security_check(self, password='password'):
330331
'/view?param=ftp://exampel.com',
331332
'view/?param=//example.com',
332333
'https:///',
334+
'HTTPS:///',
333335
'//testserver/',
334336
'/url%20with%20spaces/'): # see ticket #12534
335337
safe_url = '%(url)s?%(next)s=%(good_url)s' % {
@@ -467,7 +469,8 @@ def test_security_check(self, password='password'):
467469
for bad_url in ('http://example.com',
468470
'https://example.com',
469471
'ftp://exampel.com',
470-
'//example.com'):
472+
'//example.com',
473+
'javascript:alert("XSS")'):
471474
nasty_url = '%(url)s?%(next)s=%(bad_url)s' % {
472475
'url': logout_url,
473476
'next': REDIRECT_FIELD_NAME,
@@ -486,6 +489,7 @@ def test_security_check(self, password='password'):
486489
'/view?param=ftp://exampel.com',
487490
'view/?param=//example.com',
488491
'https:///',
492+
'HTTPS:///',
489493
'//testserver/',
490494
'/url%20with%20spaces/'): # see ticket #12534
491495
safe_url = '%(url)s?%(next)s=%(good_url)s' % {

django/utils/http.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,12 @@ def same_origin(url1, url2):
228228
def is_safe_url(url, host=None):
229229
"""
230230
Return ``True`` if the url is a safe redirection (i.e. it doesn't point to
231-
a different host).
231+
a different host and uses a safe scheme).
232232
233233
Always returns ``False`` on an empty url.
234234
"""
235235
if not url:
236236
return False
237-
netloc = urlparse.urlparse(url)[1]
238-
return not netloc or netloc == host
237+
url_info = urlparse.urlparse(url)
238+
return (not url_info[1] or url_info[1] == host) and \
239+
(not url_info[0] or url_info[0] in ['http', 'https'])

0 commit comments

Comments
 (0)