Skip to content

Commit 4749f1b

Browse files
tirkarthilarryhastings
authored andcommitted
[3.5] bpo-35121: prefix dot in domain for proper subdomain validation (GH-10258) (#12281)
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 382981b commit 4749f1b

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
@@ -1148,6 +1148,11 @@ def return_ok_domain(self, cookie, request):
11481148
req_host, erhn = eff_request_host(request)
11491149
domain = cookie.domain
11501150

1151+
if domain and not domain.startswith("."):
1152+
dotdomain = "." + domain
1153+
else:
1154+
dotdomain = domain
1155+
11511156
# strict check of non-domain cookies: Mozilla does this, MSIE5 doesn't
11521157
if (cookie.version == 0 and
11531158
(self.strict_ns_domain & self.DomainStrictNonDomain) and
@@ -1160,7 +1165,7 @@ def return_ok_domain(self, cookie, request):
11601165
_debug(" effective request-host name %s does not domain-match "
11611166
"RFC 2965 cookie domain %s", erhn, domain)
11621167
return False
1163-
if cookie.version == 0 and not ("."+erhn).endswith(domain):
1168+
if cookie.version == 0 and not ("."+erhn).endswith(dotdomain):
11641169
_debug(" request-host %s does not match Netscape cookie domain "
11651170
"%s", req_host, domain)
11661171
return False
@@ -1174,7 +1179,11 @@ def domain_return_ok(self, domain, request):
11741179
req_host = "."+req_host
11751180
if not erhn.startswith("."):
11761181
erhn = "."+erhn
1177-
if not (req_host.endswith(domain) or erhn.endswith(domain)):
1182+
if domain and not domain.startswith("."):
1183+
dotdomain = "." + domain
1184+
else:
1185+
dotdomain = domain
1186+
if not (req_host.endswith(dotdomain) or erhn.endswith(dotdomain)):
11781187
#_debug(" request domain %s does not match cookie domain %s",
11791188
# req_host, domain)
11801189
return False

Lib/test/test_http_cookiejar.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ def test_domain_return_ok(self):
417417
("http://foo.bar.com/", ".foo.bar.com", True),
418418
("http://foo.bar.com/", "foo.bar.com", True),
419419
("http://foo.bar.com/", ".bar.com", True),
420+
("http://foo.bar.com/", "bar.com", True),
420421
("http://foo.bar.com/", "com", True),
421422
("http://foo.com/", "rhubarb.foo.com", False),
422423
("http://foo.com/", ".foo.com", True),
@@ -427,6 +428,8 @@ def test_domain_return_ok(self):
427428
("http://foo/", "foo", True),
428429
("http://foo/", "foo.local", True),
429430
("http://foo/", ".local", True),
431+
("http://barfoo.com", ".foo.com", False),
432+
("http://barfoo.com", "foo.com", False),
430433
]:
431434
request = urllib.request.Request(url)
432435
r = pol.domain_return_ok(domain, request)
@@ -987,6 +990,33 @@ def test_domain_block(self):
987990
c.add_cookie_header(req)
988991
self.assertFalse(req.has_header("Cookie"))
989992

993+
c.clear()
994+
995+
pol.set_blocked_domains([])
996+
req = urllib.request.Request("http://acme.com/")
997+
res = FakeResponse(headers, "http://acme.com/")
998+
cookies = c.make_cookies(res, req)
999+
c.extract_cookies(res, req)
1000+
self.assertEqual(len(c), 1)
1001+
1002+
req = urllib.request.Request("http://acme.com/")
1003+
c.add_cookie_header(req)
1004+
self.assertTrue(req.has_header("Cookie"))
1005+
1006+
req = urllib.request.Request("http://badacme.com/")
1007+
c.add_cookie_header(req)
1008+
self.assertFalse(pol.return_ok(cookies[0], req))
1009+
self.assertFalse(req.has_header("Cookie"))
1010+
1011+
p = pol.set_blocked_domains(["acme.com"])
1012+
req = urllib.request.Request("http://acme.com/")
1013+
c.add_cookie_header(req)
1014+
self.assertFalse(req.has_header("Cookie"))
1015+
1016+
req = urllib.request.Request("http://badacme.com/")
1017+
c.add_cookie_header(req)
1018+
self.assertFalse(req.has_header("Cookie"))
1019+
9901020
def test_secure(self):
9911021
for ns in True, False:
9921022
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy