Skip to content

Commit 42ad410

Browse files
tirkarthilarryhastings
authored andcommitted
[3.4] bpo-35121: prefix dot in domain for proper subdomain validation (GH-10258) (#12279)
Don't send cookies of domain A without Domain attribute to domain B when domain A is a suffix match of domain B while using a cookiejar with `http.cookiejar.DefaultCookiePolicy` policy. Patch by Karthikeyan Singaravelan. (cherry picked from commit ca7fe50) Co-authored-by: Xtreak <tir.karthi@gmail.com>
1 parent e260f09 commit 42ad410

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Lib/http/cookiejar.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,11 @@ def return_ok_domain(self, cookie, request):
11451145
req_host, erhn = eff_request_host(request)
11461146
domain = cookie.domain
11471147

1148+
if domain and not domain.startswith("."):
1149+
dotdomain = "." + domain
1150+
else:
1151+
dotdomain = domain
1152+
11481153
# strict check of non-domain cookies: Mozilla does this, MSIE5 doesn't
11491154
if (cookie.version == 0 and
11501155
(self.strict_ns_domain & self.DomainStrictNonDomain) and
@@ -1157,7 +1162,7 @@ def return_ok_domain(self, cookie, request):
11571162
_debug(" effective request-host name %s does not domain-match "
11581163
"RFC 2965 cookie domain %s", erhn, domain)
11591164
return False
1160-
if cookie.version == 0 and not ("."+erhn).endswith(domain):
1165+
if cookie.version == 0 and not ("."+erhn).endswith(dotdomain):
11611166
_debug(" request-host %s does not match Netscape cookie domain "
11621167
"%s", req_host, domain)
11631168
return False
@@ -1171,7 +1176,11 @@ def domain_return_ok(self, domain, request):
11711176
req_host = "."+req_host
11721177
if not erhn.startswith("."):
11731178
erhn = "."+erhn
1174-
if not (req_host.endswith(domain) or erhn.endswith(domain)):
1179+
if domain and not domain.startswith("."):
1180+
dotdomain = "." + domain
1181+
else:
1182+
dotdomain = domain
1183+
if not (req_host.endswith(dotdomain) or erhn.endswith(dotdomain)):
11751184
#_debug(" request domain %s does not match cookie domain %s",
11761185
# req_host, domain)
11771186
return False

Lib/test/test_http_cookiejar.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ def test_domain_return_ok(self):
391391
("http://foo.bar.com/", ".foo.bar.com", True),
392392
("http://foo.bar.com/", "foo.bar.com", True),
393393
("http://foo.bar.com/", ".bar.com", True),
394+
("http://foo.bar.com/", "bar.com", True),
394395
("http://foo.bar.com/", "com", True),
395396
("http://foo.com/", "rhubarb.foo.com", False),
396397
("http://foo.com/", ".foo.com", True),
@@ -401,6 +402,8 @@ def test_domain_return_ok(self):
401402
("http://foo/", "foo", True),
402403
("http://foo/", "foo.local", True),
403404
("http://foo/", ".local", True),
405+
("http://barfoo.com", ".foo.com", False),
406+
("http://barfoo.com", "foo.com", False),
404407
]:
405408
request = urllib.request.Request(url)
406409
r = pol.domain_return_ok(domain, request)
@@ -961,6 +964,33 @@ def test_domain_block(self):
961964
c.add_cookie_header(req)
962965
self.assertFalse(req.has_header("Cookie"))
963966

967+
c.clear()
968+
969+
pol.set_blocked_domains([])
970+
req = urllib.request.Request("http://acme.com/")
971+
res = FakeResponse(headers, "http://acme.com/")
972+
cookies = c.make_cookies(res, req)
973+
c.extract_cookies(res, req)
974+
self.assertEqual(len(c), 1)
975+
976+
req = urllib.request.Request("http://acme.com/")
977+
c.add_cookie_header(req)
978+
self.assertTrue(req.has_header("Cookie"))
979+
980+
req = urllib.request.Request("http://badacme.com/")
981+
c.add_cookie_header(req)
982+
self.assertFalse(pol.return_ok(cookies[0], req))
983+
self.assertFalse(req.has_header("Cookie"))
984+
985+
p = pol.set_blocked_domains(["acme.com"])
986+
req = urllib.request.Request("http://acme.com/")
987+
c.add_cookie_header(req)
988+
self.assertFalse(req.has_header("Cookie"))
989+
990+
req = urllib.request.Request("http://badacme.com/")
991+
c.add_cookie_header(req)
992+
self.assertFalse(req.has_header("Cookie"))
993+
964994
def test_secure(self):
965995
for ns in True, False:
966996
for whitespace in " ", "":
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
2+
when domain A is a suffix match of domain B while using a cookiejar
3+
with :class:`http.cookiejar.DefaultCookiePolicy` policy. Patch by
4+
Karthikeyan Singaravelan.

0 commit comments

Comments
 (0)