Skip to content

Commit b8e2df1

Browse files
committed
Ensure return_ok_domain does proper validation
1 parent b2ab4a3 commit b8e2df1

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Lib/http/cookiejar.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,11 @@ def return_ok_domain(self, cookie, request):
11471147
req_host, erhn = eff_request_host(request)
11481148
domain = cookie.domain
11491149

1150+
if domain and not domain.startswith("."):
1151+
dotdomain = "." + domain
1152+
else:
1153+
dotdomain = domain
1154+
11501155
# strict check of non-domain cookies: Mozilla does this, MSIE5 doesn't
11511156
if (cookie.version == 0 and
11521157
(self.strict_ns_domain & self.DomainStrictNonDomain) and
@@ -1159,7 +1164,7 @@ def return_ok_domain(self, cookie, request):
11591164
_debug(" effective request-host name %s does not domain-match "
11601165
"RFC 2965 cookie domain %s", erhn, domain)
11611166
return False
1162-
if cookie.version == 0 and not ("."+erhn).endswith(domain):
1167+
if cookie.version == 0 and not ("."+erhn).endswith(dotdomain):
11631168
_debug(" request-host %s does not match Netscape cookie domain "
11641169
"%s", req_host, domain)
11651170
return False

Lib/test/test_http_cookiejar.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ def test_domain_block(self):
967967
pol.set_blocked_domains([])
968968
req = urllib.request.Request("http://acme.com/")
969969
res = FakeResponse(headers, "http://acme.com/")
970+
cookies = c.make_cookies(res, req)
970971
c.extract_cookies(res, req)
971972
self.assertEqual(len(c), 1)
972973

@@ -976,6 +977,7 @@ def test_domain_block(self):
976977

977978
req = urllib.request.Request("http://badacme.com/")
978979
c.add_cookie_header(req)
980+
self.assertFalse(pol.return_ok(cookies[0], req))
979981
self.assertFalse(req.has_header("Cookie"))
980982

981983
p = pol.set_blocked_domains(["acme.com"])

0 commit comments

Comments
 (0)