Skip to content

Commit e21558f

Browse files
[py] Align _cookie_matches domain and path matching with RFC 6265 (#18238)
--------- Co-authored-by: Corey Goldberg <[email protected]>
1 parent 652351e commit e21558f

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

‎py/selenium/webdriver/common/api_request_context.py‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,33 @@ def _cookie_matches(cookie: dict, url: str, default_domain: str = "") -> bool:
119119
return False
120120

121121
parsed = urllib.parse.urlparse(url)
122-
hostname = parsed.hostname or ""
122+
hostname = (parsed.hostname or "").lower()
123123
path = parsed.path or "/"
124124
scheme = parsed.scheme or "http"
125125

126126
# Domain matching (RFC 6265 section 5.1.3)
127-
cookie_domain = cookie.get("domain", "")
127+
cookie_domain = (cookie.get("domain") or "").lower()
128128
if not cookie_domain:
129-
# Host-only cookie — must match the origin host exactly
130-
if not default_domain or hostname != default_domain:
131-
return False
132-
elif cookie_domain.startswith("."):
133-
# .example.com matches example.com and sub.example.com
134-
if not (hostname == cookie_domain[1:] or hostname.endswith(cookie_domain)):
129+
# Host-only cookie - must match the origin host exactly
130+
if not default_domain or hostname != default_domain.lower():
135131
return False
136132
else:
137-
if hostname != cookie_domain:
133+
# Strip optional leading dot per RFC 6265 section 5.1.3
134+
norm_domain = cookie_domain.removeprefix(".")
135+
if not (hostname == norm_domain or hostname.endswith("." + norm_domain)):
138136
return False
139137

140138
# Path matching (RFC 6265 section 5.1.4)
141-
cookie_path = cookie.get("path", "/")
139+
cookie_path = cookie.get("path") or "/"
142140
if cookie_path == "/":
143141
pass # root path matches everything
144-
elif path != cookie_path and not path.startswith(cookie_path + "/"):
142+
elif path == cookie_path:
143+
pass
144+
elif cookie_path.endswith("/") and path.startswith(cookie_path):
145+
pass
146+
elif not cookie_path.endswith("/") and path.startswith(cookie_path + "/"):
147+
pass
148+
else:
145149
return False
146150

147151
# Secure matching

‎py/test/unit/selenium/webdriver/common/api_request_context_tests.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,21 @@ def test_missing_path_defaults_root():
294294
assert _cookie_matches({"name": "a", "value": "1", "domain": "example.com"}, "http://example.com/anything")
295295

296296

297+
def test_domain_without_leading_dot_matches_subdomain():
298+
assert _cookie_matches({"name": "a", "value": "1", "domain": "example.com"}, "http://sub.example.com/")
299+
300+
301+
def test_domain_matching_is_case_insensitive():
302+
assert _cookie_matches({"name": "a", "value": "1", "domain": "EXAMPLE.COM"}, "http://example.com/")
303+
assert _cookie_matches({"name": "a", "value": "1", "domain": "example.com"}, "http://EXAMPLE.COM/")
304+
305+
306+
def test_path_with_trailing_slash_matches_subpath():
307+
assert _cookie_matches(
308+
{"name": "a", "value": "1", "domain": "example.com", "path": "/docs/"}, "http://example.com/docs/page"
309+
)
310+
311+
297312
# --- Secure ---
298313

299314

0 commit comments

Comments
 (0)