commit d6eaee092709aad477a9894598496c6deec532ff
Author: Andreas Hug <andreas.hug@moccu.com>
Date:   Tue Jul 24 16:18:17 2018 -0400

    [1.11.x] Fixed CVE-2018-14574 -- Fixed open redirect possibility in CommonMiddleware.

Index: Django-1.11.11/django/middleware/common.py
===================================================================
--- Django-1.11.11.orig/django/middleware/common.py
+++ Django-1.11.11/django/middleware/common.py
@@ -11,6 +11,7 @@ from django.utils.cache import (
 )
 from django.utils.deprecation import MiddlewareMixin, RemovedInDjango21Warning
 from django.utils.encoding import force_text
+from django.utils.http import escape_leading_slashes
 from django.utils.six.moves.urllib.parse import urlparse
 
 
@@ -90,6 +91,8 @@ class CommonMiddleware(MiddlewareMixin):
         POST, PUT, or PATCH.
         """
         new_path = request.get_full_path(force_append_slash=True)
+        # Prevent construction of scheme relative urls.
+        new_path = escape_leading_slashes(new_path)
         if settings.DEBUG and request.method in ('POST', 'PUT', 'PATCH'):
             raise RuntimeError(
                 "You called this URL via %(method)s, but the URL doesn't end "
Index: Django-1.11.11/django/urls/resolvers.py
===================================================================
--- Django-1.11.11.orig/django/urls/resolvers.py
+++ Django-1.11.11/django/urls/resolvers.py
@@ -20,7 +20,9 @@ from django.utils import lru_cache, six
 from django.utils.datastructures import MultiValueDict
 from django.utils.encoding import force_str, force_text
 from django.utils.functional import cached_property
-from django.utils.http import RFC3986_SUBDELIMS, urlquote
+from django.utils.http import (
+    RFC3986_SUBDELIMS, escape_leading_slashes, urlquote,
+)
 from django.utils.regex_helper import normalize
 from django.utils.translation import get_language
 
@@ -465,9 +467,7 @@ class RegexURLResolver(LocaleRegexProvid
                     # safe characters from `pchar` definition of RFC 3986
                     url = urlquote(candidate_pat % candidate_subs, safe=RFC3986_SUBDELIMS + str('/~:@'))
                     # Don't allow construction of scheme relative urls.
-                    if url.startswith('//'):
-                        url = '/%%2F%s' % url[2:]
-                    return url
+                    return escape_leading_slashes(url)
         # lookup_view can be URL name or callable, but callables are not
         # friendly in error messages.
         m = getattr(lookup_view, '__module__', None)
Index: Django-1.11.11/django/utils/http.py
===================================================================
--- Django-1.11.11.orig/django/utils/http.py
+++ Django-1.11.11/django/utils/http.py
@@ -466,3 +466,14 @@ def limited_parse_qsl(qs, keep_blank_val
                 value = unquote(nv[1].replace(b'+', b' '))
             r.append((name, value))
     return r
+
+
+def escape_leading_slashes(url):
+    """
+    If redirecting to an absolute path (two leading slashes), a slash must be
+    escaped to prevent browsers from handling the path as schemaless and
+    redirecting to another host.
+    """
+    if url.startswith('//'):
+        url = '/%2F{}'.format(url[2:])
+    return url
Index: Django-1.11.11/tests/middleware/tests.py
===================================================================
--- Django-1.11.11.orig/tests/middleware/tests.py
+++ Django-1.11.11/tests/middleware/tests.py
@@ -137,6 +137,25 @@ class CommonMiddlewareTest(SimpleTestCas
         self.assertEqual(r.status_code, 301)
         self.assertEqual(r.url, '/needsquoting%23/')
 
+    @override_settings(APPEND_SLASH=True)
+    def test_append_slash_leading_slashes(self):
+        """
+        Paths starting with two slashes are escaped to prevent open redirects.
+        If there's a URL pattern that allows paths to start with two slashes, a
+        request with path //evil.com must not redirect to //evil.com/ (appended
+        slash) which is a schemaless absolute URL. The browser would navigate
+        to evil.com/.
+        """
+        # Use 4 slashes because of RequestFactory behavior.
+        request = self.rf.get('////evil.com/security')
+        response = HttpResponseNotFound()
+        r = CommonMiddleware().process_request(request)
+        self.assertEqual(r.status_code, 301)
+        self.assertEqual(r.url, '/%2Fevil.com/security/')
+        r = CommonMiddleware().process_response(request, response)
+        self.assertEqual(r.status_code, 301)
+        self.assertEqual(r.url, '/%2Fevil.com/security/')
+
     @override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
     def test_prepend_www(self):
         request = self.rf.get('/path/')
Index: Django-1.11.11/tests/middleware/urls.py
===================================================================
--- Django-1.11.11.orig/tests/middleware/urls.py
+++ Django-1.11.11/tests/middleware/urls.py
@@ -6,4 +6,6 @@ urlpatterns = [
     url(r'^noslash$', views.empty_view),
     url(r'^slash/$', views.empty_view),
     url(r'^needsquoting#/$', views.empty_view),
+    # Accepts paths with two leading slashes.
+    url(r'^(.+)/security/$', views.empty_view),
 ]
Index: Django-1.11.11/tests/utils_tests/test_http.py
===================================================================
--- Django-1.11.11.orig/tests/utils_tests/test_http.py
+++ Django-1.11.11/tests/utils_tests/test_http.py
@@ -248,3 +248,13 @@ class HttpDateProcessingTests(unittest.T
     def test_parsing_asctime(self):
         parsed = http.parse_http_date('Sun Nov  6 08:49:37 1994')
         self.assertEqual(datetime.utcfromtimestamp(parsed), datetime(1994, 11, 6, 8, 49, 37))
+
+
+class EscapeLeadingSlashesTests(unittest.TestCase):
+    def test(self):
+        tests = (
+            ('//example.com', '/%2Fexample.com'),
+            ('//', '/%2F'),
+        )
+        for url, expected in tests:
+            self.assertEqual(http.escape_leading_slashes(url), expected)
