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

Skip to content

Commit d6eaee0

Browse files
Andreas Hugtimgraham
authored andcommitted
[1.11.x] Fixed CVE-2018-14574 -- Fixed open redirect possibility in CommonMiddleware.
1 parent 4fd1f67 commit d6eaee0

File tree

7 files changed

+62
-4
lines changed

7 files changed

+62
-4
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
)
1212
from django.utils.deprecation import MiddlewareMixin, RemovedInDjango21Warning
1313
from django.utils.encoding import force_text
14+
from django.utils.http import escape_leading_slashes
1415
from django.utils.six.moves.urllib.parse import urlparse
1516

1617

@@ -90,6 +91,8 @@ def get_full_path_with_slash(self, request):
9091
POST, PUT, or PATCH.
9192
"""
9293
new_path = request.get_full_path(force_append_slash=True)
94+
# Prevent construction of scheme relative urls.
95+
new_path = escape_leading_slashes(new_path)
9396
if settings.DEBUG and request.method in ('POST', 'PUT', 'PATCH'):
9497
raise RuntimeError(
9598
"You called this URL via %(method)s, but the URL doesn't end "

django/urls/resolvers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
from django.utils.datastructures import MultiValueDict
2121
from django.utils.encoding import force_str, force_text
2222
from django.utils.functional import cached_property
23-
from django.utils.http import RFC3986_SUBDELIMS, urlquote
23+
from django.utils.http import (
24+
RFC3986_SUBDELIMS, escape_leading_slashes, urlquote,
25+
)
2426
from django.utils.regex_helper import normalize
2527
from django.utils.translation import get_language
2628

@@ -465,9 +467,7 @@ def _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs):
465467
# safe characters from `pchar` definition of RFC 3986
466468
url = urlquote(candidate_pat % candidate_subs, safe=RFC3986_SUBDELIMS + str('/~:@'))
467469
# Don't allow construction of scheme relative urls.
468-
if url.startswith('//'):
469-
url = '/%%2F%s' % url[2:]
470-
return url
470+
return escape_leading_slashes(url)
471471
# lookup_view can be URL name or callable, but callables are not
472472
# friendly in error messages.
473473
m = getattr(lookup_view, '__module__', None)

django/utils/http.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,14 @@ def limited_parse_qsl(qs, keep_blank_values=False, encoding='utf-8',
466466
value = unquote(nv[1].replace(b'+', b' '))
467467
r.append((name, value))
468468
return r
469+
470+
471+
def escape_leading_slashes(url):
472+
"""
473+
If redirecting to an absolute path (two leading slashes), a slash must be
474+
escaped to prevent browsers from handling the path as schemaless and
475+
redirecting to another host.
476+
"""
477+
if url.startswith('//'):
478+
url = '/%2F{}'.format(url[2:])
479+
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.

tests/middleware/tests.py

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

140+
@override_settings(APPEND_SLASH=True)
141+
def test_append_slash_leading_slashes(self):
142+
"""
143+
Paths starting with two slashes are escaped to prevent open redirects.
144+
If there's a URL pattern that allows paths to start with two slashes, a
145+
request with path //evil.com must not redirect to //evil.com/ (appended
146+
slash) which is a schemaless absolute URL. The browser would navigate
147+
to evil.com/.
148+
"""
149+
# Use 4 slashes because of RequestFactory behavior.
150+
request = self.rf.get('////evil.com/security')
151+
response = HttpResponseNotFound()
152+
r = CommonMiddleware().process_request(request)
153+
self.assertEqual(r.status_code, 301)
154+
self.assertEqual(r.url, '/%2Fevil.com/security/')
155+
r = CommonMiddleware().process_response(request, response)
156+
self.assertEqual(r.status_code, 301)
157+
self.assertEqual(r.url, '/%2Fevil.com/security/')
158+
140159
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
141160
def test_prepend_www(self):
142161
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,13 @@ def test_parsing_rfc850(self):
248248
def test_parsing_asctime(self):
249249
parsed = http.parse_http_date('Sun Nov 6 08:49:37 1994')
250250
self.assertEqual(datetime.utcfromtimestamp(parsed), datetime(1994, 11, 6, 8, 49, 37))
251+
252+
253+
class EscapeLeadingSlashesTests(unittest.TestCase):
254+
def test(self):
255+
tests = (
256+
('//example.com', '/%2Fexample.com'),
257+
('//', '/%2F'),
258+
)
259+
for url, expected in tests:
260+
self.assertEqual(http.escape_leading_slashes(url), expected)

0 commit comments

Comments
 (0)