Skip to content

Commit 382ab13

Browse files
mstriemertimgraham
authored andcommitted
[1.8.x] Fixed CVE-2016-2512 -- Prevented spoofing is_safe_url() with basic auth.
This is a security fix.
1 parent 922f228 commit 382ab13

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

django/utils/http.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,12 @@ def is_safe_url(url, host=None):
277277
url = url.strip()
278278
if not url:
279279
return False
280-
# Chrome treats \ completely as /
281-
url = url.replace('\\', '/')
280+
# Chrome treats \ completely as / in paths but it could be part of some
281+
# basic auth credentials so we need to check both URLs.
282+
return _is_safe_url(url, host) and _is_safe_url(url.replace('\\', '/'), host)
283+
284+
285+
def _is_safe_url(url, host):
282286
# Chrome considers any URL with more than two slashes to be absolute, but
283287
# urlparse is not so flexible. Treat any url with three slashes as unsafe.
284288
if url.startswith('///'):

docs/releases/1.8.10.txt

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ Django 1.8.10 release notes
66

77
Django 1.8.10 fixes two security issues and several bugs in 1.8.9.
88

9+
CVE-2016-2512: Malicious redirect and possible XSS attack via user-supplied redirect URLs containing basic auth
10+
===============================================================================================================
11+
12+
Django relies on user input in some cases (e.g.
13+
:func:`django.contrib.auth.views.login` and :doc:`i18n </topics/i18n/index>`)
14+
to redirect the user to an "on success" URL. The security check for these
15+
redirects (namely ``django.utils.http.is_safe_url()``) considered some URLs
16+
with basic authentication credentials "safe" when they shouldn't be.
17+
18+
For example, a URL like ``http://mysite.example.com\@attacker.com`` would be
19+
considered safe if the request's host is ``http://mysite.example.com``, but
20+
redirecting to this URL sends the user to ``attacker.com``.
21+
22+
Also, if a developer relies on ``is_safe_url()`` to provide safe redirect
23+
targets and puts such a URL into a link, they could suffer from an XSS attack.
24+
925
Bugfixes
1026
========
1127

tests/utils_tests/test_http.py

+12
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ def test_is_safe_url(self):
117117
'javascript:alert("XSS")',
118118
'\njavascript:alert(x)',
119119
'\x08//example.com',
120+
r'http://otherserver\@example.com',
121+
r'http:\\testserver\@example.com',
122+
r'http://testserver\me:[email protected]',
123+
r'http://testserver\@example.com',
124+
r'http:\\testserver\confirm\[email protected]',
120125
'\n'):
121126
self.assertFalse(http.is_safe_url(bad_url, host='testserver'), "%s should be blocked" % bad_url)
122127
for good_url in ('/view/?param=http://example.com',
@@ -126,8 +131,15 @@ def test_is_safe_url(self):
126131
'https://testserver/',
127132
'HTTPS://testserver/',
128133
'//testserver/',
134+
'http://testserver/[email protected]',
129135
'/url%20with%20spaces/'):
130136
self.assertTrue(http.is_safe_url(good_url, host='testserver'), "%s should be allowed" % good_url)
137+
# Valid basic auth credentials are allowed.
138+
self.assertTrue(http.is_safe_url(r'http://user:pass@testserver/', host='user:pass@testserver'))
139+
# A path without host is allowed.
140+
self.assertTrue(http.is_safe_url('/confirm/[email protected]'))
141+
# Basic auth without host is not allowed.
142+
self.assertFalse(http.is_safe_url(r'http://testserver\@example.com'))
131143

132144
def test_urlsafe_base64_roundtrip(self):
133145
bytestring = b'foo'

0 commit comments

Comments
 (0)