Skip to content

Commit ddd54ad

Browse files
committed
Prefix dot in domain for proper domain validation in cookiejar
1 parent 000b809 commit ddd54ad

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Lib/cookielib.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,11 +1161,14 @@ def domain_return_ok(self, domain, request):
11611161
# Liberal check of. This is here as an optimization to avoid
11621162
# having to load lots of MSIE cookie files unless necessary.
11631163
req_host, erhn = eff_request_host(request)
1164+
suffix_check_domain = domain
11641165
if not req_host.startswith("."):
11651166
req_host = "."+req_host
11661167
if not erhn.startswith("."):
11671168
erhn = "."+erhn
1168-
if not (req_host.endswith(domain) or erhn.endswith(domain)):
1169+
if suffix_check_domain and not suffix_check_domain.startswith("."):
1170+
suffix_check_domain = "." + suffix_check_domain
1171+
if not (req_host.endswith(suffix_check_domain) or erhn.endswith(suffix_check_domain)):
11691172
#_debug(" request domain %s does not match cookie domain %s",
11701173
# req_host, domain)
11711174
return False

Lib/test/test_cookielib.py

Lines changed: 28 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,31 @@ 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+
c.extract_cookies(res, req)
950+
self.assertEqual(len(c), 1)
951+
952+
req = Request("http://acme.com/")
953+
c.add_cookie_header(req)
954+
self.assertTrue(req.has_header("Cookie"))
955+
956+
req = Request("http://badacme.com/")
957+
c.add_cookie_header(req)
958+
self.assertFalse(req.has_header("Cookie"))
959+
960+
p = pol.set_blocked_domains(["acme.com"])
961+
req = Request("http://acme.com/")
962+
c.add_cookie_header(req)
963+
self.assertFalse(req.has_header("Cookie"))
964+
965+
req = Request("http://badacme.com/")
966+
c.add_cookie_header(req)
967+
self.assertFalse(req.has_header("Cookie"))
968+
941969
def test_secure(self):
942970
from cookielib import CookieJar, DefaultCookiePolicy
943971

0 commit comments

Comments
 (0)