Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix(cookies): anchor Safari cookie host matching to the domain (#1118)

Ilia Alshanetsky6c0080454ef1
diff --git a/changelog.d/+safari-cookie-domain-match.security.md b/changelog.d/+safari-cookie-domain-match.security.md
new file mode 100644
index 00000000..41df8782
--- /dev/null
+++ b/changelog.d/+safari-cookie-domain-match.security.md
@@ -0,0 +1 @@
+Safari cookie extraction now matches the cookie host exactly or as a true subdomain, so a session cookie stored for an unrelated host such as `x.com.evil.tld` is no longer picked up for `x.com`.
diff --git a/skills/last30days/scripts/lib/safari_cookies.py b/skills/last30days/scripts/lib/safari_cookies.py
index 73aa4caa..4df80472 100644
--- a/skills/last30days/scripts/lib/safari_cookies.py
+++ b/skills/last30days/scripts/lib/safari_cookies.py
@@ -98,7 +98,10 @@ def extract_safari_cookies_macos(
Extract cookies from Safari on macOS.
Args:
- domain: Domain to match (substring match, e.g. "x.com")
+ domain: Registrable host to match, with or without a leading dot
+ (e.g. "x.com" or ".x.com"). A stored cookie host matches when it
+ equals the domain or is a subdomain of it; unrelated hosts that
+ merely contain the text (e.g. "x.com.evil.tld") do not.
cookie_names: List of cookie names to extract (e.g. ["auth_token", "ct0"])
Returns:
@@ -138,6 +141,19 @@ def extract_safari_cookies_macos(
return _parse_binary_cookies(raw, domain, cookie_names)
+def _host_matches(stored_host: str, domain: str) -> bool:
+ """True when stored_host equals domain or is a subdomain of it.
+
+ Safari stores domain cookies with a leading dot (".x.com") and host-only
+ cookies without one ("x.com"); both spellings are accepted on either side.
+ """
+ host = stored_host.strip().lstrip(".").lower()
+ wanted = domain.strip().lstrip(".").lower()
+ if not host or not wanted:
+ return False
+ return host == wanted or host.endswith("." + wanted)
+
+
def _parse_binary_cookies(
raw: bytes, domain: str, cookie_names: list[str]
) -> dict[str, str] | None:
@@ -182,8 +198,7 @@ def _parse_binary_cookies(
page_data = raw[offset : offset + ps]
cookies = _parse_page(page_data)
for c in cookies:
- # Substring match on domain (handles leading dots like ".x.com")
- if domain in c["url"] and c["name"] in names_set:
+ if _host_matches(c["url"], domain) and c["name"] in names_set:
result[c["name"]] = c["value"]
offset += ps
diff --git a/tests/test_safari_cookies.py b/tests/test_safari_cookies.py
index d11b4fcd..82bd702a 100644
--- a/tests/test_safari_cookies.py
+++ b/tests/test_safari_cookies.py
@@ -128,13 +128,70 @@ class TestParseValidCookies:
result = _parse_binary_cookies(x_cookies_file, "x.com", ["bogus"])
assert result is None
- def test_domain_substring_match_with_leading_dot(self, x_cookies_file: bytes):
+ def test_leading_dot_stored_host_matches_bare_domain(self, x_cookies_file: bytes):
"""Domain '.x.com' in cookie should match search for 'x.com'."""
result = _parse_binary_cookies(x_cookies_file, "x.com", ["auth_token"])
assert result is not None
assert result["auth_token"] == "test_auth_abc123"
+def _single_cookie_file(stored_host: str) -> bytes:
+ rec = _build_cookie_record(stored_host, "auth_token", "fake_value_for_" + stored_host)
+ return _build_binary_cookies_file([_build_page([rec])])
+
+
+class TestDomainMatching:
+ @pytest.mark.parametrize("wanted", ["x.com", ".x.com"])
+ def test_exact_host_match(self, wanted: str):
+ result = _parse_binary_cookies(_single_cookie_file("x.com"), wanted, ["auth_token"])
+ assert result == {"auth_token": "fake_value_for_x.com"}
+
+ @pytest.mark.parametrize("wanted", ["x.com", ".x.com"])
+ def test_leading_dot_stored_host_matches(self, wanted: str):
+ result = _parse_binary_cookies(_single_cookie_file(".x.com"), wanted, ["auth_token"])
+ assert result == {"auth_token": "fake_value_for_.x.com"}
+
+ @pytest.mark.parametrize("stored", ["api.x.com", ".api.x.com"])
+ @pytest.mark.parametrize("wanted", ["x.com", ".x.com"])
+ def test_subdomain_match(self, stored: str, wanted: str):
+ result = _parse_binary_cookies(_single_cookie_file(stored), wanted, ["auth_token"])
+ assert result == {"auth_token": "fake_value_for_" + stored}
+
+ def test_case_insensitive_host(self):
+ result = _parse_binary_cookies(_single_cookie_file(".X.com"), "x.com", ["auth_token"])
+ assert result == {"auth_token": "fake_value_for_.X.com"}
+
+ @pytest.mark.parametrize(
+ "stored",
+ [
+ "notx.com",
+ ".notx.com",
+ "evilx.com",
+ "x.com.evil.tld",
+ ".x.com.evil.tld",
+ "api.x.com.evil.tld",
+ "xcom",
+ "com",
+ ],
+ )
+ @pytest.mark.parametrize("wanted", ["x.com", ".x.com"])
+ def test_rejects_hosts_that_merely_contain_domain(self, stored: str, wanted: str):
+ result = _parse_binary_cookies(_single_cookie_file(stored), wanted, ["auth_token"])
+ assert result is None
+
+ def test_rejects_parent_domain_when_subdomain_requested(self):
+ result = _parse_binary_cookies(_single_cookie_file(".com"), "x.com", ["auth_token"])
+ assert result is None
+
+ def test_empty_stored_host_never_matches(self):
+ result = _parse_binary_cookies(_single_cookie_file(""), "x.com", ["auth_token"])
+ assert result is None
+
+ def test_empty_requested_domain_never_matches(self):
+ result = _parse_binary_cookies(_single_cookie_file(".x.com"), "", ["auth_token"])
+ assert result is None
+
+
class TestMultiplePages:
def test_cookies_across_pages(self):
rec1 = _build_cookie_record(".x.com", "auth_token", "page1_auth")