Skip to content

Commit c73f0ca

Browse files
committed
gh-150522: anchor suffix check in http.cookiejar.domain_match
1 parent d8ff4f8 commit c73f0ca

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

Lib/http/cookiejar.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,9 @@ def domain_match(A, B):
578578
if not is_HDN(A):
579579
return False
580580
i = A.rfind(B)
581-
if i == -1 or i == 0:
582-
# A does not have form NB, or N is the empty string
581+
if i == -1 or i == 0 or i + len(B) != len(A):
582+
# A does not have form NB, N is the empty string, or B is not a
583+
# suffix of A (so A only contains B as an interior substring)
583584
return False
584585
if not B.startswith("."):
585586
return False

Lib/test/test_http_cookiejar.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,9 @@ def test_domain_match(self):
888888
self.assertFalse(domain_match("blah.blah", ""))
889889
self.assertFalse(domain_match("", ".rhubarb.rhubarb"))
890890
self.assertTrue(domain_match("", ""))
891+
# B must be a suffix of A, not just an interior substring
892+
self.assertFalse(domain_match("www.acme.com.evil.org", ".acme.com"))
893+
self.assertFalse(domain_match("a.b.c.com.example.net", ".c.com"))
891894

892895
self.assertTrue(user_domain_match("acme.com", "acme.com"))
893896
self.assertFalse(user_domain_match("acme.com", ".acme.com"))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`http.cookiejar.domain_match` now requires the second domain to be a
2+
suffix of the first instead of merely an interior substring, so a host such
3+
as ``www.example.com.evil`` no longer domain-matches ``.example.com``.

0 commit comments

Comments
 (0)