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

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