fix(chrome_cookies): sort Brave profiles by mtime, not alphabetically

This commit is contained in:
Trevin Chow
2026-05-17 00:40:10 -07:00
parent 65313ce542
commit 8f565ee241
@@ -285,19 +285,24 @@ def _find_brave_cookies_db() -> Optional[Path]:
"""Find Brave's Cookies database on macOS. """Find Brave's Cookies database on macOS.
Tries the Default profile first, then scans numbered Profile directories Tries the Default profile first, then scans numbered Profile directories
in creation order. Brave creates extra profiles as "Profile 1", "Profile 2", by most-recently-modified. Brave creates extra profiles as "Profile 1",
etc. alongside Default. "Profile 2", etc. alongside Default; the most 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.
""" """
default = BRAVE_BASE_DIR / "Default" / "Cookies" default = BRAVE_BASE_DIR / "Default" / "Cookies"
if default.exists(): if default.exists():
return default return default
try: try:
for child in sorted(BRAVE_BASE_DIR.iterdir()): candidates = [
if child.is_dir() and child.name.startswith("Profile "): child for child in BRAVE_BASE_DIR.iterdir()
candidate = child / "Cookies" if child.is_dir() and child.name.startswith("Profile ")
if candidate.exists(): ]
return candidate for child in sorted(candidates, key=lambda p: p.stat().st_mtime, reverse=True):
candidate = child / "Cookies"
if candidate.exists():
return candidate
except OSError: except OSError:
pass pass