Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fffc3c

Browse files
Andreas Hugtimgraham
authored andcommitted
[2.0.x] Fixed CVE-2018-14574 -- Fixed open redirect possibility in CommonMiddleware.
1 parent af34469 commit 6fffc3c

File tree

8 files changed

+78
-8
lines changed

8 files changed

+78
-8
lines changed

django/middleware/common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
cc_delim_re, get_conditional_response, set_response_etag,
1212
)
1313
from django.utils.deprecation import MiddlewareMixin, RemovedInDjango21Warning
14+
from django.utils.http import escape_leading_slashes
1415

1516

1617
class CommonMiddleware(MiddlewareMixin):
@@ -88,6 +89,8 @@ def get_full_path_with_slash(self, request):
8889
POST, PUT, or PATCH.
8990
"""
9091
new_path = request.get_full_path(force_append_slash=True)
92+
# Prevent construction of scheme relative urls.
93+
new_path = escape_leading_slashes(new_path)
9194
if settings.DEBUG and request.method in ('POST', 'PUT', 'PATCH'):
9295
raise RuntimeError(
9396
"You called this URL via %(method)s, but the URL doesn't end "

django/urls/resolvers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from django.core.exceptions import ImproperlyConfigured
1818
from django.utils.datastructures import MultiValueDict
1919
from django.utils.functional import cached_property
20-
from django.utils.http import RFC3986_SUBDELIMS
20+
from django.utils.http import RFC3986_SUBDELIMS, escape_leading_slashes
2121
from django.utils.regex_helper import normalize
2222
from django.utils.translation import get_language
2323

@@ -604,9 +604,7 @@ def _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs):
604604
# safe characters from `pchar` definition of RFC 3986
605605
url = quote(candidate_pat % text_candidate_subs, safe=RFC3986_SUBDELIMS + '/~:@')
606606
# Don't allow construction of scheme relative urls.
607-
if url.startswith('//'):
608-
url = '/%%2F%s' % url[2:]
609-
return url
607+
return escape_leading_slashes(url)
610608
# lookup_view can be URL name or callable, but callables are not
611609
# friendly in error messages.
612610
m = getattr(lookup_view, '__module__', None)

django/utils/http.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,14 @@ def limited_parse_qsl(qs, keep_blank_values=False, encoding='utf-8',
437437
value = unquote(value, encoding=encoding, errors=errors)
438438
r.append((name, value))
439439
return r
440+
441+
442+
def escape_leading_slashes(url):
443+
"""
444+
If redirecting to an absolute path (two leading slashes), a slash must be
445+
escaped to prevent browsers from handling the path as schemaless and
446+
redirecting to another host.
447+
"""
448+
if url.startswith('//'):
449+
url = '/%2F{}'.format(url[2:])
450+
return url

docs/releases/1.11.15.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ Django 1.11.15 release notes
55
*August 1, 2018*
66

77
Django 1.11.15 fixes a security issue in 1.11.14.
8+
9+
CVE-2018-14574: Open redirect possibility in ``CommonMiddleware``
10+
=================================================================
11+
12+
If the :class:`~django.middleware.common.CommonMiddleware` and the
13+
:setting:`APPEND_SLASH` setting are both enabled, and if the project has a
14+
URL pattern that accepts any path ending in a slash (many content management
15+
systems have such a pattern), then a request to a maliciously crafted URL of
16+
that site could lead to a redirect to another site, enabling phishing and other
17+
attacks.
18+
19+
``CommonMiddleware`` now escapes leading slashes to prevent redirects to other
20+
domains.

docs/releases/2.0.8.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ Django 2.0.8 release notes
66

77
Django 2.0.8 fixes a security issue and several bugs in 2.0.7.
88

9+
CVE-2018-14574: Open redirect possibility in ``CommonMiddleware``
10+
=================================================================
11+
12+
If the :class:`~django.middleware.common.CommonMiddleware` and the
13+
:setting:`APPEND_SLASH` setting are both enabled, and if the project has a
14+
URL pattern that accepts any path ending in a slash (many content management
15+
systems have such a pattern), then a request to a maliciously crafted URL of
16+
that site could lead to a redirect to another site, enabling phishing and other
17+
attacks.
18+
19+
``CommonMiddleware`` now escapes leading slashes to prevent redirects to other
20+
domains.
21+
922
Bugfixes
1023
========
1124

tests/middleware/tests.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,25 @@ def test_append_slash_quoted(self):
133133
self.assertEqual(r.status_code, 301)
134134
self.assertEqual(r.url, '/needsquoting%23/')
135135

136+
@override_settings(APPEND_SLASH=True)
137+
def test_append_slash_leading_slashes(self):
138+
"""
139+
Paths starting with two slashes are escaped to prevent open redirects.
140+
If there's a URL pattern that allows paths to start with two slashes, a
141+
request with path //evil.com must not redirect to //evil.com/ (appended
142+
slash) which is a schemaless absolute URL. The browser would navigate
143+
to evil.com/.
144+
"""
145+
# Use 4 slashes because of RequestFactory behavior.
146+
request = self.rf.get('////evil.com/security')
147+
response = HttpResponseNotFound()
148+
r = CommonMiddleware().process_request(request)
149+
self.assertEqual(r.status_code, 301)
150+
self.assertEqual(r.url, '/%2Fevil.com/security/')
151+
r = CommonMiddleware().process_response(request, response)
152+
self.assertEqual(r.status_code, 301)
153+
self.assertEqual(r.url, '/%2Fevil.com/security/')
154+
136155
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
137156
def test_prepend_www(self):
138157
request = self.rf.get('/path/')

tests/middleware/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
url(r'^noslash$', views.empty_view),
77
url(r'^slash/$', views.empty_view),
88
url(r'^needsquoting#/$', views.empty_view),
9+
# Accepts paths with two leading slashes.
10+
url(r'^(.+)/security/$', views.empty_view),
911
]

tests/utils_tests/test_http.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from django.utils.datastructures import MultiValueDict
77
from django.utils.deprecation import RemovedInDjango21Warning
88
from django.utils.http import (
9-
base36_to_int, cookie_date, http_date, int_to_base36, is_safe_url,
10-
is_same_domain, parse_etags, parse_http_date, quote_etag, urlencode,
11-
urlquote, urlquote_plus, urlsafe_base64_decode, urlsafe_base64_encode,
12-
urlunquote, urlunquote_plus,
9+
base36_to_int, cookie_date, escape_leading_slashes, http_date,
10+
int_to_base36, is_safe_url, is_same_domain, parse_etags, parse_http_date,
11+
quote_etag, urlencode, urlquote, urlquote_plus, urlsafe_base64_decode,
12+
urlsafe_base64_encode, urlunquote, urlunquote_plus,
1313
)
1414

1515

@@ -275,3 +275,14 @@ def test_parsing_rfc850(self):
275275
def test_parsing_asctime(self):
276276
parsed = parse_http_date('Sun Nov 6 08:49:37 1994')
277277
self.assertEqual(datetime.utcfromtimestamp(parsed), datetime(1994, 11, 6, 8, 49, 37))
278+
279+
280+
class EscapeLeadingSlashesTests(unittest.TestCase):
281+
def test(self):
282+
tests = (
283+
('//example.com', '/%2Fexample.com'),
284+
('//', '/%2F'),
285+
)
286+
for url, expected in tests:
287+
with self.subTest(url=url):
288+
self.assertEqual(escape_leading_slashes(url), expected)

0 commit comments

Comments
 (0)