Blame SOURCES/00330-CVE-2018-20852.patch

4f4dad
diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
4f4dad
index adf956d..97599d4 100644
4f4dad
--- a/Lib/http/cookiejar.py
4f4dad
+++ b/Lib/http/cookiejar.py
4f4dad
@@ -1148,6 +1148,11 @@ class DefaultCookiePolicy(CookiePolicy):
4f4dad
         req_host, erhn = eff_request_host(request)
4f4dad
         domain = cookie.domain
4f4dad
 
4f4dad
+        if domain and not domain.startswith("."):
4f4dad
+            dotdomain = "." + domain
4f4dad
+        else:
4f4dad
+            dotdomain = domain
4f4dad
+
4f4dad
         # strict check of non-domain cookies: Mozilla does this, MSIE5 doesn't
4f4dad
         if (cookie.version == 0 and
4f4dad
             (self.strict_ns_domain & self.DomainStrictNonDomain) and
4f4dad
@@ -1160,7 +1165,7 @@ class DefaultCookiePolicy(CookiePolicy):
4f4dad
             _debug("   effective request-host name %s does not domain-match "
4f4dad
                    "RFC 2965 cookie domain %s", erhn, domain)
4f4dad
             return False
4f4dad
-        if cookie.version == 0 and not ("."+erhn).endswith(domain):
4f4dad
+        if cookie.version == 0 and not ("."+erhn).endswith(dotdomain):
4f4dad
             _debug("   request-host %s does not match Netscape cookie domain "
4f4dad
                    "%s", req_host, domain)
4f4dad
             return False
4f4dad
@@ -1174,7 +1179,11 @@ class DefaultCookiePolicy(CookiePolicy):
4f4dad
             req_host = "."+req_host
4f4dad
         if not erhn.startswith("."):
4f4dad
             erhn = "."+erhn
4f4dad
-        if not (req_host.endswith(domain) or erhn.endswith(domain)):
4f4dad
+        if domain and not domain.startswith("."):
4f4dad
+            dotdomain = "." + domain
4f4dad
+        else:
4f4dad
+            dotdomain = domain
4f4dad
+        if not (req_host.endswith(dotdomain) or erhn.endswith(dotdomain)):
4f4dad
             #_debug("   request domain %s does not match cookie domain %s",
4f4dad
             #       req_host, domain)
4f4dad
             return False
4f4dad
diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py
4f4dad
index abc625d..6e1b308 100644
4f4dad
--- a/Lib/test/test_http_cookiejar.py
4f4dad
+++ b/Lib/test/test_http_cookiejar.py
4f4dad
@@ -415,6 +415,7 @@ class CookieTests(unittest.TestCase):
4f4dad
             ("http://foo.bar.com/", ".foo.bar.com", True),
4f4dad
             ("http://foo.bar.com/", "foo.bar.com", True),
4f4dad
             ("http://foo.bar.com/", ".bar.com", True),
4f4dad
+            ("http://foo.bar.com/", "bar.com", True),
4f4dad
             ("http://foo.bar.com/", "com", True),
4f4dad
             ("http://foo.com/", "rhubarb.foo.com", False),
4f4dad
             ("http://foo.com/", ".foo.com", True),
4f4dad
@@ -425,6 +426,8 @@ class CookieTests(unittest.TestCase):
4f4dad
             ("http://foo/", "foo", True),
4f4dad
             ("http://foo/", "foo.local", True),
4f4dad
             ("http://foo/", ".local", True),
4f4dad
+            ("http://barfoo.com", ".foo.com", False),
4f4dad
+            ("http://barfoo.com", "foo.com", False),
4f4dad
             ]:
4f4dad
             request = urllib.request.Request(url)
4f4dad
             r = pol.domain_return_ok(domain, request)
4f4dad
@@ -959,6 +962,33 @@ class CookieTests(unittest.TestCase):
4f4dad
         c.add_cookie_header(req)
4f4dad
         self.assertFalse(req.has_header("Cookie"))
4f4dad
 
4f4dad
+        c.clear()
4f4dad
+
4f4dad
+        pol.set_blocked_domains([])
4f4dad
+        req = urllib.request.Request("http://acme.com/")
4f4dad
+        res = FakeResponse(headers, "http://acme.com/")
4f4dad
+        cookies = c.make_cookies(res, req)
4f4dad
+        c.extract_cookies(res, req)
4f4dad
+        self.assertEqual(len(c), 1)
4f4dad
+
4f4dad
+        req = urllib.request.Request("http://acme.com/")
4f4dad
+        c.add_cookie_header(req)
4f4dad
+        self.assertTrue(req.has_header("Cookie"))
4f4dad
+
4f4dad
+        req = urllib.request.Request("http://badacme.com/")
4f4dad
+        c.add_cookie_header(req)
4f4dad
+        self.assertFalse(pol.return_ok(cookies[0], req))
4f4dad
+        self.assertFalse(req.has_header("Cookie"))
4f4dad
+
4f4dad
+        p = pol.set_blocked_domains(["acme.com"])
4f4dad
+        req = urllib.request.Request("http://acme.com/")
4f4dad
+        c.add_cookie_header(req)
4f4dad
+        self.assertFalse(req.has_header("Cookie"))
4f4dad
+
4f4dad
+        req = urllib.request.Request("http://badacme.com/")
4f4dad
+        c.add_cookie_header(req)
4f4dad
+        self.assertFalse(req.has_header("Cookie"))
4f4dad
+
4f4dad
     def test_secure(self):
4f4dad
         for ns in True, False:
4f4dad
             for whitespace in " ", "":