Skip to content

Commit 979daae

Browse files
tirkarthimiss-islington
authored andcommitted
[2.7] bpo-35121: prefix dot in domain for proper subdomain validation (GH-10258) (GH-13426)
This is a manual backport of ca7fe50 since 2.7 has `http.cookiejar` in `cookielib` https://bugs.python.org/issue35121
1 parent 2b57847 commit 979daae

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Lib/cookielib.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,11 @@ def return_ok_domain(self, cookie, request):
11391139
req_host, erhn = eff_request_host(request)
11401140
domain = cookie.domain
11411141

1142+
if domain and not domain.startswith("."):
1143+
dotdomain = "." + domain
1144+
else:
1145+
dotdomain = domain
1146+
11421147
# strict check of non-domain cookies: Mozilla does this, MSIE5 doesn't
11431148
if (cookie.version == 0 and
11441149
(self.strict_ns_domain & self.DomainStrictNonDomain) and
@@ -1151,7 +1156,7 @@ def return_ok_domain(self, cookie, request):
11511156
_debug(" effective request-host name %s does not domain-match "
11521157
"RFC 2965 cookie domain %s", erhn, domain)
11531158
return False
1154-
if cookie.version == 0 and not ("."+erhn).endswith(domain):
1159+
if cookie.version == 0 and not ("."+erhn).endswith(dotdomain):
11551160
_debug(" request-host %s does not match Netscape cookie domain "
11561161
"%s", req_host, domain)
11571162
return False
@@ -1165,7 +1170,11 @@ def domain_return_ok(self, domain, request):
11651170
req_host = "."+req_host
11661171
if not erhn.startswith("."):
11671172
erhn = "."+erhn
1168-
if not (req_host.endswith(domain) or erhn.endswith(domain)):
1173+
if domain and not domain.startswith("."):
1174+
dotdomain = "." + domain
1175+
else:
1176+
dotdomain = domain
1177+
if not (req_host.endswith(dotdomain) or erhn.endswith(dotdomain)):
11691178
#_debug(" request domain %s does not match cookie domain %s",
11701179
# req_host, domain)
11711180
return False

Lib/test/test_cookielib.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ def test_domain_return_ok(self):
368368
("http://foo.bar.com/", ".foo.bar.com", True),
369369
("http://foo.bar.com/", "foo.bar.com", True),
370370
("http://foo.bar.com/", ".bar.com", True),
371+
("http://foo.bar.com/", "bar.com", True),
371372
("http://foo.bar.com/", "com", True),
372373
("http://foo.com/", "rhubarb.foo.com", False),
373374
("http://foo.com/", ".foo.com", True),
@@ -378,6 +379,8 @@ def test_domain_return_ok(self):
378379
("http://foo/", "foo", True),
379380
("http://foo/", "foo.local", True),
380381
("http://foo/", ".local", True),
382+
("http://barfoo.com", ".foo.com", False),
383+
("http://barfoo.com", "foo.com", False),
381384
]:
382385
request = urllib2.Request(url)
383386
r = pol.domain_return_ok(domain, request)
@@ -938,6 +941,33 @@ def test_domain_block(self):
938941
c.add_cookie_header(req)
939942
self.assertFalse(req.has_header("Cookie"))
940943

944+
c.clear()
945+
946+
pol.set_blocked_domains([])
947+
req = Request("http://acme.com/")
948+
res = FakeResponse(headers, "http://acme.com/")
949+
cookies = c.make_cookies(res, req)
950+
c.extract_cookies(res, req)
951+
self.assertEqual(len(c), 1)
952+
953+
req = Request("http://acme.com/")
954+
c.add_cookie_header(req)
955+
self.assertTrue(req.has_header("Cookie"))
956+
957+
req = Request("http://badacme.com/")
958+
c.add_cookie_header(req)
959+
self.assertFalse(pol.return_ok(cookies[0], req))
960+
self.assertFalse(req.has_header("Cookie"))
961+
962+
p = pol.set_blocked_domains(["acme.com"])
963+
req = Request("http://acme.com/")
964+
c.add_cookie_header(req)
965+
self.assertFalse(req.has_header("Cookie"))
966+
967+
req = Request("http://badacme.com/")
968+
c.add_cookie_header(req)
969+
self.assertFalse(req.has_header("Cookie"))
970+
941971
def test_secure(self):
942972
from cookielib import CookieJar, DefaultCookiePolicy
943973

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Don't send cookies of domain A without Domain attribute to domain B when
2+
domain A is a suffix match of domain B while using a cookiejar with
3+
:class:`cookielib.DefaultCookiePolicy` policy. Patch by Karthikeyan
4+
Singaravelan.

0 commit comments

Comments
 (0)