Skip to content

Commit b6d5e3c

Browse files
bpo-46075: Store localhost cookies in CookieJar (#30108)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 74e3192 commit b6d5e3c

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Lib/http/cookiejar.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,12 +1043,13 @@ def set_ok_domain(self, cookie, request):
10431043
else:
10441044
undotted_domain = domain
10451045
embedded_dots = (undotted_domain.find(".") >= 0)
1046-
if not embedded_dots and domain != ".local":
1046+
if not embedded_dots and not erhn.endswith(".local"):
10471047
_debug(" non-local domain %s contains no embedded dot",
10481048
domain)
10491049
return False
10501050
if cookie.version == 0:
1051-
if (not erhn.endswith(domain) and
1051+
if (not (erhn.endswith(domain) or
1052+
erhn.endswith(f"{undotted_domain}.local")) and
10521053
(not erhn.startswith(".") and
10531054
not ("."+erhn).endswith(domain))):
10541055
_debug(" effective request-host %s (even with added "

Lib/test/test_http_cookiejar.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,48 @@ def test_two_component_domain_ns(self):
920920
## self.assertEqual(len(c), 2)
921921
self.assertEqual(len(c), 4)
922922

923+
def test_localhost_domain(self):
924+
c = CookieJar()
925+
926+
interact_netscape(c, "http://localhost", "foo=bar; domain=localhost;")
927+
928+
self.assertEqual(len(c), 1)
929+
930+
def test_localhost_domain_contents(self):
931+
c = CookieJar()
932+
933+
interact_netscape(c, "http://localhost", "foo=bar; domain=localhost;")
934+
935+
self.assertEqual(c._cookies[".localhost"]["/"]["foo"].value, "bar")
936+
937+
def test_localhost_domain_contents_2(self):
938+
c = CookieJar()
939+
940+
interact_netscape(c, "http://localhost", "foo=bar;")
941+
942+
self.assertEqual(c._cookies["localhost.local"]["/"]["foo"].value, "bar")
943+
944+
def test_evil_nonlocal_domain(self):
945+
c = CookieJar()
946+
947+
interact_netscape(c, "http://evil.com", "foo=bar; domain=.localhost")
948+
949+
self.assertEqual(len(c), 0)
950+
951+
def test_evil_local_domain(self):
952+
c = CookieJar()
953+
954+
interact_netscape(c, "http://localhost", "foo=bar; domain=.evil.com")
955+
956+
self.assertEqual(len(c), 0)
957+
958+
def test_evil_local_domain_2(self):
959+
c = CookieJar()
960+
961+
interact_netscape(c, "http://localhost", "foo=bar; domain=.someother.local")
962+
963+
self.assertEqual(len(c), 0)
964+
923965
def test_two_component_domain_rfc2965(self):
924966
pol = DefaultCookiePolicy(rfc2965=True)
925967
c = CookieJar(pol)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``CookieJar`` with ``DefaultCookiePolicy`` now can process cookies from localhost with domain=localhost explicitly specified in Set-Cookie header.

0 commit comments

Comments
 (0)