MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / _cookie_matches

Function _cookie_matches

py/selenium/webdriver/common/api_request_context.py:103–157  ·  view source on GitHub ↗

Check if a browser cookie should be sent with a request to the given URL. Evaluates expiry, domain, path, and secure attribute matching per RFC 6265. Args: cookie: A cookie dict from driver.get_cookies(). url: The target request URL. default_domain: Fallback domain

(cookie: Cookie, url: str, default_domain: str = "")

Source from the content-addressed store, hash-verified

101
102
103def _cookie_matches(cookie: Cookie, url: str, default_domain: str = "") -> bool:
104 """Check if a browser cookie should be sent with a request to the given URL.
105
106 Evaluates expiry, domain, path, and secure attribute matching per RFC 6265.
107
108 Args:
109 cookie: A cookie dict from driver.get_cookies().
110 url: The target request URL.
111 default_domain: Fallback domain for host-only cookies (no domain attribute).
112 When a cookie has no domain, it only matches if the request hostname
113 equals this value. If empty and cookie has no domain, the cookie is skipped.
114
115 Returns:
116 True if the cookie matches the URL.
117 """
118 # Expiry check — skip expired cookies
119 expiry = cookie.get("expiry")
120 if expiry is not None and expiry <= int(time.time()):
121 return False
122
123 parsed = urllib.parse.urlparse(url)
124 hostname = (parsed.hostname or "").lower()
125 path = parsed.path or "/"
126 scheme = parsed.scheme or "http"
127
128 # Domain matching (RFC 6265 section 5.1.3)
129 cookie_domain = (cookie.get("domain") or "").lower()
130 if not cookie_domain:
131 # Host-only cookie - must match the origin host exactly
132 if not default_domain or hostname != default_domain.lower():
133 return False
134 else:
135 # Strip optional leading dot per RFC 6265 section 5.1.3
136 norm_domain = cookie_domain.removeprefix(".")
137 if not (hostname == norm_domain or hostname.endswith("." + norm_domain)):
138 return False
139
140 # Path matching (RFC 6265 section 5.1.4)
141 cookie_path = cookie.get("path") or "/"
142 if cookie_path == "/":
143 pass # root path matches everything
144 elif path == cookie_path:
145 pass
146 elif cookie_path.endswith("/") and path.startswith(cookie_path):
147 pass
148 elif not cookie_path.endswith("/") and path.startswith(cookie_path + "/"):
149 pass
150 else:
151 return False
152
153 # Secure matching
154 if cookie.get("secure", False) and scheme != "https":
155 return False
156
157 return True
158
159
160def _parse_set_cookie(header_value: str) -> Cookie | None:

Calls 2

timeMethod · 0.80
getMethod · 0.65