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(chrome): reuse profile cookie finder and keychain key

Dhilip Subramanian0b4d8b1bbdb9
diff --git a/skills/last30days/scripts/lib/chrome_cookies.py b/skills/last30days/scripts/lib/chrome_cookies.py
index c248496c..24af4dd2 100644
--- a/skills/last30days/scripts/lib/chrome_cookies.py
+++ b/skills/last30days/scripts/lib/chrome_cookies.py
@@ -204,6 +204,7 @@ def _extract_chromium_cookies_macos(
keychain_service: str,
domain: str,
cookie_names: list[str],
+ key_cache: Optional[dict[str, Optional[bytes]]] = None,
) -> Optional[dict[str, str]]:
"""Extract cookies from any Chromium-based browser on macOS.
@@ -277,8 +278,13 @@ def _extract_chromium_cookies_macos(
# Keychain prompt for browsers that don't hold the requested
# cookie, which matters for FROM_BROWSER=auto across several
# installed Chromium browsers.
- passphrase = _get_chromium_encryption_key(keychain_service)
- aes_key = _derive_aes_key(passphrase) if passphrase else None
+ if key_cache is not None and keychain_service in key_cache:
+ aes_key = key_cache[keychain_service]
+ else:
+ passphrase = _get_chromium_encryption_key(keychain_service)
+ aes_key = _derive_aes_key(passphrase) if passphrase else None
+ if key_cache is not None:
+ key_cache[keychain_service] = aes_key
key_fetched = True
if aes_key is None:
logger.debug("Skipping encrypted cookie %s — no Keychain access", name)
@@ -350,28 +356,12 @@ def _find_chromium_cookies_db(base_dir: Path) -> Optional[Path]:
recently used one is the likeliest to hold current cookies. Lexicographic
sort would visit "Profile 10" before "Profile 2", which can return the
wrong profile, so we sort by mtime.
- """
- found = _profile_cookie_db(base_dir / "Default")
- if found:
- return found
-
- found = _profile_cookie_db(base_dir)
- if found:
- return found
-
- try:
- candidates = [
- child for child in base_dir.iterdir()
- if child.is_dir() and child.name.startswith("Profile ")
- ]
- for child in sorted(candidates, key=lambda p: p.stat().st_mtime, reverse=True):
- found = _profile_cookie_db(child)
- if found:
- return found
- except OSError:
- pass
- return None
+ Kept for backward compatibility; new code should use
+ _find_all_chromium_cookies_dbs() to search across all profiles.
+ """
+ dbs = _find_all_chromium_cookies_dbs(base_dir)
+ return dbs[0] if dbs else None
def _find_all_chromium_cookies_dbs(base_dir: Path) -> list[Path]:
@@ -421,8 +411,11 @@ def _extract_chromium_cookies_any_profile(
logger.info("%s cookies database not found under %s", keychain_service, base_dir)
return None
best: Optional[dict[str, str]] = None
+ key_cache: dict[str, Optional[bytes]] = {}
for db_path in db_paths:
- got = _extract_chromium_cookies_macos(db_path, keychain_service, domain, cookie_names)
+ got = _extract_chromium_cookies_macos(
+ db_path, keychain_service, domain, cookie_names, key_cache=key_cache
+ )
if got:
if all(name in got for name in cookie_names):
logger.debug("Found complete cookie set for %s in %s", domain, db_path)
diff --git a/tests/test_chrome_cookies.py b/tests/test_chrome_cookies.py
index 3385eb26..8b417599 100644
--- a/tests/test_chrome_cookies.py
+++ b/tests/test_chrome_cookies.py
@@ -26,6 +26,7 @@ from lib.chrome_cookies import (
_remove_pkcs7_padding,
_extract_chromium_cookies_macos,
_decrypt_v10_value,
+ _find_chromium_cookies_db,
extract_chrome_cookies_macos,
)
@@ -207,12 +208,27 @@ class TestDecryption:
class TestChromeNotInstalled:
def test_db_not_found(self):
with mock.patch(
- "lib.chrome_cookies._find_chromium_cookies_db",
- return_value=None,
+ "lib.chrome_cookies._find_all_chromium_cookies_dbs",
+ return_value=[],
):
result = extract_chrome_cookies_macos(".x.com", ["auth_token"])
assert result is None
+
+class TestChromiumCookieDbFinder:
+ def test_legacy_single_db_finder_returns_first_all_profile_candidate(self, tmp_path):
+ first = tmp_path / "Default" / "Network" / "Cookies"
+ second = tmp_path / "Profile 1" / "Network" / "Cookies"
+
+ with mock.patch(
+ "lib.chrome_cookies._find_all_chromium_cookies_dbs",
+ return_value=[first, second],
+ ) as find_all:
+ result = _find_chromium_cookies_db(tmp_path)
+
+ assert result == first
+ find_all.assert_called_once_with(tmp_path)
+
# ---------------------------------------------------------------------------
# Keychain access denied → returns None
# ---------------------------------------------------------------------------
@@ -312,7 +328,10 @@ class TestUnencryptedCookies:
(".x.com", "ct0", "plain_ct0_value", b""),
])
- with mock.patch("lib.chrome_cookies._find_chromium_cookies_db", return_value=Path(db_path)):
+ with mock.patch(
+ "lib.chrome_cookies._find_all_chromium_cookies_dbs",
+ return_value=[Path(db_path)],
+ ):
# No keychain needed for unencrypted values
with mock.patch("lib.chrome_cookies._get_chromium_encryption_key", return_value=None):
result = extract_chrome_cookies_macos(".x.com", ["auth_token", "ct0"])
@@ -341,7 +360,10 @@ class TestFullExtraction:
(".other.com", "other", "", b""), # unrelated cookie
])
- with mock.patch("lib.chrome_cookies._find_chromium_cookies_db", return_value=Path(db_path)):
+ with mock.patch(
+ "lib.chrome_cookies._find_all_chromium_cookies_dbs",
+ return_value=[Path(db_path)],
+ ):
with mock.patch(
"lib.chrome_cookies._get_chromium_encryption_key",
return_value=KNOWN_PASSPHRASE,
@@ -358,7 +380,10 @@ class TestFullExtraction:
(".other.com", "session", "val", b""),
])
- with mock.patch("lib.chrome_cookies._find_chromium_cookies_db", return_value=Path(db_path)):
+ with mock.patch(
+ "lib.chrome_cookies._find_all_chromium_cookies_dbs",
+ return_value=[Path(db_path)],
+ ):
with mock.patch("lib.chrome_cookies._get_chromium_encryption_key", return_value=None):
result = extract_chrome_cookies_macos(".x.com", ["auth_token"])
@@ -375,7 +400,10 @@ class TestFullExtraction:
(".x.com", "auth_token", "", encrypted_auth),
], db_version=24)
- with mock.patch("lib.chrome_cookies._find_chromium_cookies_db", return_value=Path(db_path)):
+ with mock.patch(
+ "lib.chrome_cookies._find_all_chromium_cookies_dbs",
+ return_value=[Path(db_path)],
+ ):
with mock.patch(
"lib.chrome_cookies._get_chromium_encryption_key",
return_value=KNOWN_PASSPHRASE,
@@ -385,6 +413,36 @@ class TestFullExtraction:
assert result is not None
assert result["auth_token"] == auth_val
+ @pytest.mark.skipif(not OPENSSL_AVAILABLE, reason="openssl not installed")
+ def test_multi_profile_extraction_reuses_keychain_key(self, tmp_path):
+ """Profiles for one browser share a Keychain service; fetch it once."""
+ first_db = tmp_path / "Default.sqlite"
+ second_db = tmp_path / "Profile1.sqlite"
+ auth_val = "profile_auth_token"
+ ct0_val = "profile_ct0_value"
+
+ _create_chrome_cookies_db(str(first_db), [
+ (".x.com", "auth_token", "", _encrypt_value_v10(auth_val, KNOWN_AES_KEY)),
+ ])
+ _create_chrome_cookies_db(str(second_db), [
+ (".x.com", "auth_token", "", _encrypt_value_v10(auth_val, KNOWN_AES_KEY)),
+ (".x.com", "ct0", "", _encrypt_value_v10(ct0_val, KNOWN_AES_KEY)),
+ ])
+
+ with mock.patch(
+ "lib.chrome_cookies._find_all_chromium_cookies_dbs",
+ return_value=[first_db, second_db],
+ ):
+ with mock.patch(
+ "lib.chrome_cookies._get_chromium_encryption_key",
+ return_value=KNOWN_PASSPHRASE,
+ ) as get_key:
+ result = extract_chrome_cookies_macos(".x.com", ["auth_token", "ct0"])
+
+ assert result is not None
+ assert result["ct0"] == ct0_val
+ get_key.assert_called_once_with("Chrome Safe Storage")
+
# ---------------------------------------------------------------------------
# DB version detection
# ---------------------------------------------------------------------------