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: report denied X browser cookie access (#1249)

Ilia Alshanetskya3b73fc5f3fb
diff --git a/CONFIGURATION.md b/CONFIGURATION.md
index 625c0a0d..2fdf7577 100644
--- a/CONFIGURATION.md
+++ b/CONFIGURATION.md
@@ -90,7 +90,7 @@ When no credential source shows completed setup, the skill runs consent-driven o
The Modal and Non-Modal flows share the same consent points:
1. **Browser cookies** - the model asks before reading anything and explains that permission covers later research runs. On yes it runs `setup --allow-browser-cookies`. On macOS, the scan checks Chrome and other Chromium browsers first, then Firefox and Safari, unless you already configured `FROM_BROWSER`. On Windows and Linux, this native cookie scan supports Firefox only; under WSL2, it also falls back to Windows Firefox. Unless you already configured `FROM_BROWSER`, setup saves the browser names that supplied complete cookies in `FROM_BROWSER` so later research can read those signed-in sessions again; it never saves the cookie values. If different services use different browsers, it saves their comma-separated names. Chromium reads on macOS may prompt for Keychain access again on later runs; choose **Always Allow** if you want to grant continuing OS access. Setup also installs yt-dlp + the keyless Digg CLI. On no it runs setup without `--allow-browser-cookies` (or with `FROM_BROWSER=off`), which skips all cookie reads and still installs the tools. The engine saves the decision as `BROWSER_CONSENT=true` or `false`; refusal blocks native browser and CDP reads on later runs, even if an older `FROM_BROWSER` selection remains. To disable these reads later, set `BROWSER_CONSENT=false` or `FROM_BROWSER=off` in `~/.config/last30days/.env`. A later consented setup can replace the refusal. An explicit `FROM_BROWSER=off` remains a separate denial until you change it. The `agentcookie` sidecar has its own `AGENTCOOKIE=off` control.
-2. **Full Disk Access (macOS)** - if a cookie read is permission-denied, the model surfaces the System Settings > Privacy & Security > Full Disk Access fix and offers one retry.
+2. **Browser-data permissions (macOS)** - if a consented cookie read is permission-denied, setup names the affected browser and explains that X cookies were unavailable. Check System Settings > Privacy & Security permissions for the terminal or agent host, then retry once. Full Disk Access can help with Safari and some host configurations, but it is not a guaranteed fix for another app's protected browser data. If access remains blocked, supply `AUTH_TOKEN` and `CT0` yourself or use a key-backed X source such as `XAI_API_KEY`. `doctor` reports a saved denial as a **last setup** observation without reading cookie values.
3. **ScrapeCreators GitHub signup** - offered on every first run (10,000 free calls). On consent it runs `setup --github`, which opens a browser for GitHub device-auth (or registers instantly via the `gh` CLI when installed) and, on success, **persists `SCRAPECREATORS_API_KEY` automatically** (0o600, masked in output). The key enables TikTok and Instagram, Reddit search backfill below 5 free results, YouTube search backfill below 3 yt-dlp videos, and YouTube transcript rescue; these spend credits. Set `LAST30DAYS_YT_SC_MIN_ITEMS=0` for empty-only YouTube search backfill. Decline anytime; you can run it later by asking to set up ScrapeCreators. The Step 5 opt-in has two tiers, both comment-enabled: **Recommended** (TikTok + Instagram posts AND top comments, plus YouTube comments — `INCLUDE_SOURCES=tiktok,instagram,youtube_comments,tiktok_comments,instagram_comments`) and **Everything**, which also adds Threads + Pinterest. Comments are on by default; Threads and Pinterest are the only opt-in extras.
To revisit setup, ask the skill to set up a source; deleting the global `.env` alone does not force onboarding when another credential source remains active. The mechanical work lives in `scripts/lib/setup_wizard.py`; the consent conversation and the host flows are specified in Step 0 of `skills/last30days/references/setup-wizard.md`, which the root `SKILL.md` routes to when needed. The original v3.0.0 wizard is captured at `docs/reference/old-nux-wizard-v3.0.0.md`.
@@ -558,6 +558,8 @@ python3 skills/last30days/scripts/last30days.py doctor --probe # bounded li
Slash-command form: `/last30days doctor`. Reporting problems is a successful run — the exit code is always 0, no browser cookies are read, and no secret values appear anywhere (key presence is booleans only). Backends within a chained source are probed sequentially with a 5-second budget per binary probe, so a chained source's worst-case check time is additive across its backends (only reached when several binaries hang at once).
+When a consented setup encounters a browser database permission error, it saves only the affected browser name. `doctor` labels this as a last-setup observation; it does not recheck browser access. A later setup refreshes the observation. Research runs also report a permission denial when X browser authentication fails, without printing cookie values or database paths.
+
`doctor --postmortem` reads the last run's `last-report.json` (any age, labeled) and reports what actually happened per source — Failed / Partial / Succeeded / Skipped, with details and fix hints — so a run that returned less than expected can be diagnosed after the fact. It makes no network calls.
**Network note:** plain `doctor` with a fresh run, `--cached`, and `--json` make **no** network calls. `doctor --probe` — and a plain `doctor` when there is **no** fresh run to learn from — run a **bounded** live test to verify WORKING instead of guessing. The probe is scoped to free HTTP endpoints (Reddit, Hacker News, Polymarket, GitHub) plus keyless CLIs; credit-gated sources (X, TikTok, Instagram, Threads, …) are never probed, so no ScrapeCreators credits are spent and no auth rate limits are tripped. Each source is probed concurrently under a per-source deadline so a slow source can never hang the command.
@@ -574,6 +576,7 @@ Every live run writes its JSON result to `~/.config/last30days/doctor-cache.json
| `LAST30DAYS_X_BACKEND` | Pins the X backend (`bird` / `xai` / `xurl` / `xquik` / `grok` / `xapi`); doctor renders the pin and predicts "will use" accordingly. The unpinned auto chain is bird → xai → xurl → xquik (grok and xapi are opt-in only). Pin `grok` to enable it; a leftover `~/.grok/auth.json` is never auto-selected. Pin `xapi` to use `X_BEARER_TOKEN` on an ordinary host. On a Grok Bot host the unpinned chain is the official chain (xapi → xai → xurl) and this pin is the only way to select a backend outside it; the pin keeps its exclusive, no-failover meaning there, and doctor names the pinned backend. |
| `AGENTCOOKIE` | `on` opts any host (incl. a MacBook) into the extra bird cookie lookups (agentcookie sidecar + live Chrome CDP); `off` disables the agentcookie sidecar reader. Unset uses host detection (Linux / Mac mini / Darwin sink get the extras). See "Extra bird cookie lookups" above. |
| `BROWSER_CONSENT` | Browser-cookie decision saved by setup: `true` permits consented browser discovery; `false` blocks native browser and CDP reads across later runs. Native extraction still requires `FROM_BROWSER`; CDP permits a prior explicit Chromium / `auto` selection when this marker is absent. `FROM_BROWSER=off` always blocks both readers. |
+| `LAST30DAYS_X_COOKIE_ACCESS_DENIED` | Setup-managed, non-secret last-scan observation: comma-separated browser names whose X cookie database read was denied, or `none` after a later setup without a denial. Doctor reads this marker without opening browser data. Do not set it manually; retry consented setup to refresh it. |
| `BROWSER_CDP_URL` | Explicit Chrome DevTools endpoint (e.g. `http://127.0.0.1:18800`) for the extra-host CDP cookie lookup. Preferred over the `18800` / `9222`+`$DISPLAY` defaults. Extra hosts only; setting an endpoint does not grant browser-cookie consent. |
| `LAST30DAYS_REDDIT_BACKEND` | `scrapecreators` makes ScrapeCreators the primary Reddit backend; doctor renders Reddit's conditional routing with the pin applied. |
| `LAST30DAYS_REDDIT_SC_MIN_ITEMS` | Integer thinness floor for ScrapeCreators Reddit **search** backfill. Default `5`: backfill when the free path returns fewer than 5 items, spending ScrapeCreators credits. Set another positive count to move the floor, or `0` to backfill only when the free path returns nothing. Unset or blank means `5`; a malformed value behaves like `0` (spends nothing extra). A run makes at most one ScrapeCreators Reddit backfill per distinct query, date window, and subreddit set, and each backfill is several API calls (searches plus comment fetches, roughly 10 at default depth). A failed backfill after free items only adds a detail note. Merged results dedupe by post id. Requires `SCRAPECREATORS_API_KEY`. Ignored when `LAST30DAYS_REDDIT_BACKEND=scrapecreators` (SC is already primary). |
diff --git a/changelog.d/1193.fixed.md b/changelog.d/1193.fixed.md
new file mode 100644
index 00000000..54d0344b
--- /dev/null
+++ b/changelog.d/1193.fixed.md
@@ -0,0 +1 @@
+Setup and research runs now explain when browser-data permission denial prevents X cookie authentication. Doctor shows the last setup denial without reading browser cookies and points to manual cookies or key-backed X access.
diff --git a/skills/last30days/references/setup-wizard.md b/skills/last30days/references/setup-wizard.md
index 969cc491..8f065ce8 100644
--- a/skills/last30days/references/setup-wizard.md
+++ b/skills/last30days/references/setup-wizard.md
@@ -67,9 +67,9 @@ The consented `setup --allow-browser-cookies` run extracts cookies using the pla
Setup also best-effort installs yt-dlp (YouTube), the free keyless Digg CLI (`digg-pp-cli` via `@mvanhorn/printing-press-library install digg --cli-only`; Digg activates only when the binary is on the **agent subprocess PATH**, typically `$HOME/.local/bin`; setup reports honestly if installed off-PATH; recommend-only if `npx` is unavailable), plus the free keyless arXiv and Techmeme CLIs. Show the user what was found and installed - including whether Digg landed on PATH (active) or off-PATH (installed but not yet active).
-**Extras-host X login (Linux / Mac mini / Darwin agentcookie sink — a MacBook SKIPS this, and so does a `LAST30DAYS_HOST=grok-bot` host, which never runs it).** On a MacBook the Keychain/Firefox/Safari extract above is all X needs. On an extras host the local Chrome store can't be decrypted, so that extract finds nothing and X stays empty unless you also capture a LIVE login over CDP. After the cookie-consent setup run: run `"${LAST30DAYS_PYTHON}" "${SKILL_DIR}/scripts/box_chrome_login.py"` — it prints the exact host-correct command (or launches it with `--exec`), and on a MacBook it prints "no launch needed" and spawns nothing. When `box-chrome` is on PATH it launches a throwaway profile on the last30days extras port **18800** (the last30days convention `SAND_CHROME_REMOTE_DEBUG_PORT=18800`, not box-chrome's default): `CHROME_USER_DATA_DIR=/tmp/last30days-x-chrome SAND_CHROME_REMOTE_DEBUG_PORT=18800 box-chrome --new-window https://x.com/login`. Wait for the x.com login page, then HAND THE DESKTOP to the human to type — do NOT fill or drive the form. After they sign in, append `BROWSER_CDP_URL=http://127.0.0.1:18800` to `.env` (never `AUTH_TOKEN`/`CT0`; keep `AGENTCOOKIE=off` during the harvest), then re-run `setup --allow-browser-cookies` so extras CDP reads the live pair. Full steps and the block/rate-limit stop rule are in **X on Linux / Mac mini** below. This extras-host login only runs on the consented "Yes - X cookies" path; the Research Continuation Override never skips it when the user said yes to X.
+**Extras-host X login (Linux / Mac mini / Darwin agentcookie sink — a MacBook SKIPS this, and so does a `LAST30DAYS_HOST=grok-bot` host, which never runs it).** On a MacBook the Keychain/Firefox/Safari extract above serves X when the OS permits browser-data reads. On an extras host the local Chrome store can't be decrypted, so that extract finds nothing and X stays empty unless you also capture a LIVE login over CDP. After the cookie-consent setup run: run `"${LAST30DAYS_PYTHON}" "${SKILL_DIR}/scripts/box_chrome_login.py"` — it prints the exact host-correct command (or launches it with `--exec`), and on a MacBook it prints "no launch needed" and spawns nothing. When `box-chrome` is on PATH it launches a throwaway profile on the last30days extras port **18800** (the last30days convention `SAND_CHROME_REMOTE_DEBUG_PORT=18800`, not box-chrome's default): `CHROME_USER_DATA_DIR=/tmp/last30days-x-chrome SAND_CHROME_REMOTE_DEBUG_PORT=18800 box-chrome --new-window https://x.com/login`. Wait for the x.com login page, then HAND THE DESKTOP to the human to type — do NOT fill or drive the form. After they sign in, append `BROWSER_CDP_URL=http://127.0.0.1:18800` to `.env` (never `AUTH_TOKEN`/`CT0`; keep `AGENTCOOKIE=off` during the harvest), then re-run `setup --allow-browser-cookies` so extras CDP reads the live pair. Full steps and the block/rate-limit stop rule are in **X on Linux / Mac mini** below. This extras-host login only runs on the consented "Yes - X cookies" path; the Research Continuation Override never skips it when the user said yes to X.
-**macOS Full Disk Access remediation (Safari fallback only).** Chrome and Firefox need no Full Disk Access; only the Safari fallback does. After the `setup` run, inspect its stderr. If it contains `Permission denied reading Cookies.binarycookies` and the platform is macOS, the OS blocked the Safari read - surface the fix instead of swallowing it: `macOS blocked the Safari cookie read. If your x.com login is in Chrome, you don't need this. To use Safari: System Settings > Privacy & Security > Full Disk Access > enable your terminal (or the Claude app), then I can retry.` Offer ONE retry only when no research topic is waiting. If a topic is already waiting or the user skips, continue immediately with available sources.
+**macOS browser-data permission remediation.** After consented `setup`, if stderr says `X cookie access was permission denied`, name the browser; if it says `Permission denied reading Cookies.binarycookies`, name Safari. Advise: `Check System Settings > Privacy & Security permissions for your terminal or agent app. Full Disk Access may help, but may not unlock another app's data. You can supply AUTH_TOKEN and CT0 manually or use XAI_API_KEY.` Offer ONE retry only when no research topic is waiting; otherwise continue with available sources.
**Step 4: ScrapeCreators offer (every first run unless the Research Continuation Override already started a waiting topic).** Show this as plain text, then a modal:
@@ -134,7 +134,7 @@ For hosts without interactive modal prompts (OpenClaw, Codex, Cursor, Gemini CLI
- **Extras hosts (Linux / Mac mini / Darwin agentcookie sink) — a MacBook SKIPS this, and so does a `LAST30DAYS_HOST=grok-bot` host.** On these hosts the Chrome cookie store can't be decrypted, so the extract above finds nothing and X stays empty unless you capture a LIVE login over CDP. Run `"${LAST30DAYS_PYTHON}" "${SKILL_DIR}/scripts/box_chrome_login.py"` (prints the host-correct command; `--exec` launches it; a MacBook prints "no launch needed" and spawns nothing). When `box-chrome` is on PATH it launches a throwaway profile on the last30days extras port **18800** (`SAND_CHROME_REMOTE_DEBUG_PORT=18800`, not box-chrome's default): `CHROME_USER_DATA_DIR=/tmp/last30days-x-chrome SAND_CHROME_REMOTE_DEBUG_PORT=18800 box-chrome --new-window https://x.com/login`. Wait for the x.com login page, then HAND THE DESKTOP to the human to type — do NOT drive the form. After they sign in, append `BROWSER_CDP_URL=http://127.0.0.1:18800` to `.env` (never `AUTH_TOKEN`/`CT0`; keep `AGENTCOOKIE=off` during the harvest) and re-run `setup --allow-browser-cookies`. Full steps and the block/rate-limit stop rule: **X on Linux / Mac mini** below. This extras-host login only runs on the consented **yes** path; a waiting topic never skips it when the user said yes to X.
- On **no** → run `FROM_BROWSER=off "${LAST30DAYS_PYTHON}" "${SKILL_DIR}/scripts/last30days.py" setup`. Skips all cookie reads; still installs yt-dlp (YouTube), Digg, arXiv, and Techmeme, still writes `SETUP_COMPLETE`. The engine saves `BROWSER_CONSENT=false`, so later runs also skip native browser and CDP reads. If the invocation already includes a topic, immediately research it with `--no-browser-cookies`, then resume the deferred onboarding in the same run after the findings: the ScrapeCreators offer (step 4) and source tier (step 4b) if a key is saved. Do not re-ask cookie consent as part of the resume.
-**3. Full Disk Access remediation (macOS only).** After `setup`, inspect stderr. If it contains `Permission denied reading Cookies.binarycookies` on macOS, surface: `macOS blocked the cookie read. To enable X/Twitter: System Settings > Privacy & Security > Full Disk Access > enable your terminal (or the Claude app), then I can retry.` Offer ONE retry only when no research topic is waiting. If a topic is already waiting or the user skips, continue immediately with available sources.
+**3. Browser-data permission remediation (macOS only).** After consented `setup`, if stderr says `X cookie access was permission denied`, name the browser; if it says `Permission denied reading Cookies.binarycookies`, name Safari. Advise: `Check System Settings > Privacy & Security permissions for your terminal or agent app. Full Disk Access may help, but may not unlock another app's data. You can supply AUTH_TOKEN and CT0 manually or use XAI_API_KEY.` Offer ONE retry only when no research topic is waiting; otherwise continue with available sources.
**4. ScrapeCreators offer (every first run; consent before browser launch).** Explain: the key grants 10,000 free calls for TikTok/Instagram and spends credits on Reddit search with fewer than 5 free items, YouTube search with fewer than 3 yt-dlp videos, and transcript rescue. `LAST30DAYS_REDDIT_SC_MIN_ITEMS=0` / `LAST30DAYS_YT_SC_MIN_ITEMS=0` make backfills empty-only; `LAST30DAYS_REDDIT_BACKEND=scrapecreators` pins Reddit to SC. A YouTube backfill may fetch transcripts and retry. GitHub signup grants more free calls than web signup and opens a page for a short code. Ask, e.g.: `Want to add TikTok and Instagram? Your key also spends credits on Reddit search below 5, YouTube search below 3, and transcripts. LAST30DAYS_YT_SC_MIN_ITEMS=0 makes YouTube search empty-only. GitHub signup grants 10,000 free calls and opens a browser for a code. (yes / no)` **Wait for the answer.**
- On **yes** → two commands. FIRST run `"${LAST30DAYS_PYTHON}" "${SKILL_DIR}/scripts/last30days.py" setup --github-start` in the FOREGROUND - it returns in ~1-2s with a `Your GitHub code: XXXX-XXXX` line plus a JSON blob, copies the code to the clipboard, and opens the browser. Read the `user_code` from that output and immediately tell the user: the code, that it's on their clipboard so they can just paste it (Cmd+V) on the GitHub page - do not make them hunt for it. (If `status == "already_registered"`, stop here - their existing key is active. If the output said the clipboard copy failed, tell them to type the code.) THEN run `"${LAST30DAYS_PYTHON}" "${SKILL_DIR}/scripts/last30days.py" setup --github-poll` (background with a 5-min timeout, or foreground) and parse the **LAST** JSON line of its stdout for the final status. On success the engine persists the key automatically and returns `"persisted": true` with a MASKED `api_key` (never ask for or echo the raw key). Confirm the paid sources are active.
diff --git a/skills/last30days/scripts/last30days.py b/skills/last30days/scripts/last30days.py
index 9cbcb0ca..1bdd8942 100644
--- a/skills/last30days/scripts/last30days.py
+++ b/skills/last30days/scripts/last30days.py
@@ -3415,9 +3415,19 @@ def _main(
for browser in results.get("cookies_found", {}).values()
)
from_browser = ",".join(found_browsers) or None
+ denied_browsers = (
+ results.get("x_cookie_access_denied", [])
+ if results.get("browser_cookie_scan_attempted") else []
+ )
+ prior_denial = config.get("LAST30DAYS_X_COOKIE_ACCESS_DENIED")
+ denial_marker = (
+ ",".join(denied_browsers) or ("none" if prior_denial else None)
+ if results.get("browser_cookie_scan_attempted") else None
+ )
results["env_written"] = setup_wizard.write_setup_config(
env.CONFIG_FILE,
from_browser=from_browser,
+ x_cookie_access_denied=denial_marker,
browser_consent=(
None if args.diagnose else _setup_allows_browser_cookies(args, extra_argv)
),
diff --git a/skills/last30days/scripts/lib/chrome_cookies.py b/skills/last30days/scripts/lib/chrome_cookies.py
index 24af4dd2..9289d804 100644
--- a/skills/last30days/scripts/lib/chrome_cookies.py
+++ b/skills/last30days/scripts/lib/chrome_cookies.py
@@ -21,6 +21,8 @@ import tempfile
from pathlib import Path
from typing import Optional
+from .cookie_paths import path_exists, path_is_dir
+
logger = logging.getLogger(__name__)
@@ -221,7 +223,11 @@ def _extract_chromium_cookies_macos(
Dict mapping cookie name to decrypted value, or None on failure.
Only includes cookies that were successfully found and decrypted.
"""
- if not db_path.exists():
+ try:
+ db_path.stat()
+ except PermissionError:
+ raise
+ except OSError:
logger.info("%s cookies database not found at %s", keychain_service, db_path)
return None
@@ -236,12 +242,18 @@ def _extract_chromium_cookies_macos(
shutil.copyfile(str(db_path), tmp_path)
_lock_temp_cookie_copy(tmp_path)
except Exception as e:
- logger.info("Failed to copy %s cookies database: %s", keychain_service, e)
+ browser_read_denied = isinstance(e, PermissionError) and e.filename == str(db_path)
+ if browser_read_denied:
+ logger.info("Permission denied reading %s cookies database", keychain_service)
+ else:
+ logger.info("Failed to copy %s cookies database: %s", keychain_service, e)
if tmp_path:
try:
Path(tmp_path).unlink(missing_ok=True)
except Exception:
pass
+ if browser_read_denied:
+ raise
return None
finally:
if tmp_fd is not None:
@@ -330,7 +342,9 @@ def extract_chrome_cookies_macos(domain: str, cookie_names: list[str]) -> Option
)
-def _profile_cookie_db(profile_dir: Path) -> Optional[Path]:
+def _profile_cookie_db(
+ profile_dir: Path, denials: Optional[list[PermissionError]] = None
+) -> Optional[Path]:
"""Return the Cookies DB inside a profile dir, or None.
Prefers the modern ``Network/Cookies`` location (Chromium >= 96 moved the
@@ -338,10 +352,20 @@ def _profile_cookie_db(profile_dir: Path) -> Optional[Path]:
to the legacy flat ``Cookies`` file. Different browsers and versions use
different layouts, so both are probed.
"""
+ denied: list[PermissionError] = []
for rel in ("Network/Cookies", "Cookies"):
candidate = profile_dir / rel
- if candidate.exists():
- return candidate
+ try:
+ if path_exists(candidate):
+ if denials is not None:
+ denials.extend(denied)
+ return candidate
+ except PermissionError as exc:
+ denied.append(exc)
+ if denials is not None:
+ denials.extend(denied)
+ elif denied:
+ raise denied[0]
return None
@@ -364,7 +388,9 @@ def _find_chromium_cookies_db(base_dir: Path) -> Optional[Path]:
return dbs[0] if dbs else None
-def _find_all_chromium_cookies_dbs(base_dir: Path) -> list[Path]:
+def _find_all_chromium_cookies_dbs(
+ base_dir: Path, denials: Optional[list[PermissionError]] = None
+) -> list[Path]:
"""Return ALL candidate Cookies DBs under base_dir, best-guess order first.
Order: Default, the base dir itself (Opera's flat layout), then numbered
@@ -376,23 +402,35 @@ def _find_all_chromium_cookies_dbs(base_dir: Path) -> list[Path]:
"""
paths: list[Path] = []
seen: set[Path] = set()
+ denied: list[PermissionError] = []
def add(p: Optional[Path]) -> None:
if p is not None and p not in seen:
seen.add(p)
paths.append(p)
- add(_profile_cookie_db(base_dir / "Default"))
- add(_profile_cookie_db(base_dir))
+ add(_profile_cookie_db(base_dir / "Default", denied))
+ add(_profile_cookie_db(base_dir, denied))
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):
- add(_profile_cookie_db(child))
+ candidates = []
+ for child in base_dir.iterdir():
+ if not child.name.startswith("Profile "):
+ continue
+ try:
+ if path_is_dir(child):
+ candidates.append((child.stat().st_mtime, child))
+ except PermissionError as exc:
+ denied.append(exc)
+ for _, child in sorted(candidates, reverse=True):
+ add(_profile_cookie_db(child, denied))
+ except PermissionError as exc:
+ denied.append(exc)
except OSError:
pass
+ if denials is not None:
+ denials.extend(denied)
+ elif denied and not paths:
+ raise denied[0]
return paths
@@ -406,22 +444,31 @@ def _extract_chromium_cookies_any_profile(
None if no profile yielded any. This fixes the single-profile limitation
where a guest-only Default profile shadowed a logged-in "Profile N".
"""
- db_paths = _find_all_chromium_cookies_dbs(base_dir)
+ denials: list[PermissionError] = []
+ db_paths = _find_all_chromium_cookies_dbs(base_dir, denials)
if not db_paths:
+ if denials:
+ raise denials[0]
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, key_cache=key_cache
- )
+ try:
+ got = _extract_chromium_cookies_macos(
+ db_path, keychain_service, domain, cookie_names, key_cache=key_cache
+ )
+ except PermissionError as exc:
+ denials.append(exc)
+ continue
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)
return got
if best is None:
best = got
+ if denials:
+ raise denials[0]
return best
diff --git a/skills/last30days/scripts/lib/cookie_extract.py b/skills/last30days/scripts/lib/cookie_extract.py
index b441f958..d12b1e1b 100644
--- a/skills/last30days/scripts/lib/cookie_extract.py
+++ b/skills/last30days/scripts/lib/cookie_extract.py
@@ -19,6 +19,8 @@ import tempfile
from pathlib import Path
from typing import Dict, List, Optional
+from .cookie_paths import path_is_dir, path_is_file
+
logger = logging.getLogger(__name__)
@@ -65,18 +67,30 @@ def _get_wsl_firefox_profiles_dir() -> Optional[Path]:
directories (skips Public, Default, etc.).
"""
mnt_users = Path("/mnt/c/Users")
- if not mnt_users.is_dir():
+ if not path_is_dir(mnt_users):
return None
skip = {"Public", "Default", "Default User", "All Users"}
+ denied = None
try:
for user_dir in sorted(mnt_users.iterdir()):
- if user_dir.name in skip or not user_dir.is_dir():
+ if user_dir.name in skip:
continue
- ff_dir = user_dir / "AppData" / "Roaming" / "Mozilla" / "Firefox"
- if ff_dir.is_dir():
- return ff_dir
+ try:
+ if not path_is_dir(user_dir):
+ continue
+ ff_dir = user_dir / "AppData" / "Roaming" / "Mozilla" / "Firefox"
+ if path_is_dir(ff_dir):
+ return ff_dir
+ except PermissionError as exc:
+ if denied is None:
+ denied = exc
+ except PermissionError as exc:
+ if denied is None:
+ denied = exc
except OSError:
pass
+ if denied is not None:
+ raise denied
return None
@@ -88,8 +102,12 @@ def _get_firefox_profiles_dir() -> Optional[Path]:
elif system == "Linux":
# Default location for most distros
path = Path.home() / ".mozilla" / "firefox"
- if path.is_dir():
- return path
+ denied = None
+ try:
+ if path_is_dir(path):
+ return path
+ except PermissionError as exc:
+ denied = exc
# Some distros (e.g. Fedora) honour $XDG_CONFIG_HOME
xdg_config = os.environ.get("XDG_CONFIG_HOME")
if xdg_config and os.path.isabs(xdg_config):
@@ -100,7 +118,11 @@ def _get_firefox_profiles_dir() -> Optional[Path]:
# Windows: %APPDATA%\Mozilla\Firefox — best-effort
appdata = Path.home() / "AppData" / "Roaming" / "Mozilla" / "Firefox"
path = appdata
- return path if path.is_dir() else None
+ if path_is_dir(path):
+ return path
+ if system == "Linux" and denied is not None:
+ raise denied
+ return None
def _load_profiles_ini(ini_path: Path) -> configparser.ConfigParser:
@@ -113,15 +135,23 @@ def _load_profiles_ini(ini_path: Path) -> configparser.ConfigParser:
return config
-def _find_default_profile(profiles_dir: Path) -> Optional[Path]:
+def _find_default_profile(
+ profiles_dir: Path, denials: Optional[list[PermissionError]] = None
+) -> Optional[Path]:
"""Parse profiles.ini to find the default profile directory.
Looks for a section with Default=1. Falls back to the first profile
directory found on disk if profiles.ini is missing or malformed.
"""
ini_path = profiles_dir / "profiles.ini"
+ denied: list[PermissionError] = []
- if ini_path.is_file():
+ try:
+ has_ini = path_is_file(ini_path)
+ except PermissionError as exc:
+ denied.append(exc)
+ has_ini = False
+ if has_ini:
try:
config = _load_profiles_ini(ini_path)
@@ -130,25 +160,53 @@ def _find_default_profile(profiles_dir: Path) -> Optional[Path]:
if section.startswith("Install") and config.has_option(section, "Default"):
raw = config.get(section, "Default")
candidate = profiles_dir / raw
- if candidate.is_dir():
+ try:
+ found = path_is_dir(candidate)
+ except PermissionError as exc:
+ denied.append(exc)
+ continue
+ if found:
+ if denials is not None:
+ denials.extend(denied)
return candidate
# Second pass: Profile section with Default=1
for section in config.sections():
if section.startswith("Profile") and config.has_option(section, "Default") and config.get(section, "Default") == "1":
- return _resolve_profile_path(profiles_dir, config, section)
+ try:
+ resolved = _resolve_profile_path(profiles_dir, config, section)
+ except PermissionError as exc:
+ denied.append(exc)
+ continue
+ if resolved is not None:
+ if denials is not None:
+ denials.extend(denied)
+ return resolved
# Third pass: first Profile section that exists on disk
for section in config.sections():
if section.startswith("Profile"):
- resolved = _resolve_profile_path(profiles_dir, config, section)
- if resolved and resolved.is_dir():
+ try:
+ resolved = _resolve_profile_path(profiles_dir, config, section)
+ except PermissionError as exc:
+ denied.append(exc)
+ continue
+ if resolved:
+ if denials is not None:
+ denials.extend(denied)
return resolved
+ except PermissionError as exc:
+ denied.append(exc)
except (configparser.Error, OSError, UnicodeDecodeError) as exc:
logger.debug("Failed to parse profiles.ini: %s", exc)
# Fallback: scan directory for anything that looks like a profile
- return _fallback_find_profile(profiles_dir)
+ profile = _fallback_find_profile(profiles_dir, denied)
+ if denials is not None:
+ denials.extend(denied)
+ elif denied and profile is None:
+ raise denied[0]
+ return profile
def _resolve_profile_path(
@@ -163,17 +221,31 @@ def _resolve_profile_path(
candidate = profiles_dir / raw_path
else:
candidate = Path(raw_path)
- return candidate if candidate.is_dir() else None
+ return candidate if path_is_dir(candidate) else None
-def _fallback_find_profile(profiles_dir: Path) -> Optional[Path]:
+def _fallback_find_profile(
+ profiles_dir: Path, denials: Optional[list[PermissionError]] = None
+) -> Optional[Path]:
"""Find the first directory that contains cookies.sqlite."""
+ denied: list[PermissionError] = []
try:
for child in sorted(profiles_dir.iterdir()):
- if child.is_dir() and (child / "cookies.sqlite").is_file():
- return child
+ try:
+ if path_is_dir(child) and path_is_file(child / "cookies.sqlite"):
+ if denials is not None:
+ denials.extend(denied)
+ return child
+ except PermissionError as exc:
+ denied.append(exc)
+ except PermissionError as exc:
+ denied.append(exc)
except OSError:
pass
+ if denials is not None:
+ denials.extend(denied)
+ elif denied:
+ raise denied[0]
return None
@@ -185,7 +257,7 @@ def _query_cookies_db(
Firefox locks cookies.sqlite while running, so we copy first.
Returns {name: value} dict or None if no matching cookies found.
"""
- if not db_path.is_file():
+ if not path_is_file(db_path):
return None
tmp_fd = None
@@ -222,6 +294,11 @@ def _query_cookies_db(
return None
return {name: value for name, value in rows}
+ except PermissionError as exc:
+ if exc.filename == str(db_path):
+ raise
+ logger.debug("Failed to query cookies database %s: %s", db_path, exc)
+ return None
except (sqlite3.Error, OSError) as exc:
logger.debug("Failed to query cookies database %s: %s", db_path, exc)
return None
@@ -248,11 +325,16 @@ def _try_firefox_dir(profiles_dir: Path, domain: str, cookie_names: List[str]) -
A complete match (all ``cookie_names``) always wins: a partial
default-profile result is kept only as a fallback.
"""
- default_profile = _find_default_profile(profiles_dir)
+ denials: list[PermissionError] = []
+ default_profile = _find_default_profile(profiles_dir, denials)
profiles_tried = 0
fallback: Optional[Dict[str, str]] = None
if default_profile is not None:
- result = _query_cookies_db(default_profile / "cookies.sqlite", domain, cookie_names)
+ try:
+ result = _query_cookies_db(default_profile / "cookies.sqlite", domain, cookie_names)
+ except PermissionError as exc:
+ denials.append(exc)
+ result = None
if result is not None:
if has_complete_pair(result, cookie_names):
return result
@@ -261,21 +343,28 @@ def _try_firefox_dir(profiles_dir: Path, domain: str, cookie_names: List[str]) -
# Fallback: scan every profile directory for matching cookies
try:
for child in sorted(profiles_dir.iterdir()):
- if not child.is_dir():
- continue
- if default_profile is not None and child == default_profile:
- continue
- db = child / "cookies.sqlite"
- if db.is_file():
- result = _query_cookies_db(db, domain, cookie_names)
- if result is not None:
- if has_complete_pair(result, cookie_names):
- return result
- if fallback is None:
- fallback = result
- profiles_tried += 1
+ try:
+ if not path_is_dir(child):
+ continue
+ if default_profile is not None and child == default_profile:
+ continue
+ db = child / "cookies.sqlite"
+ if path_is_file(db):
+ result = _query_cookies_db(db, domain, cookie_names)
+ if result is not None:
+ if has_complete_pair(result, cookie_names):
+ return result
+ if fallback is None:
+ fallback = result
+ profiles_tried += 1
+ except PermissionError as exc:
+ denials.append(exc)
+ except PermissionError as exc:
+ denials.append(exc)
except OSError:
pass
+ if denials:
+ raise denials[0]
if fallback is not None:
return fallback
logger.debug("No matching cookies found in %d Firefox profile(s)", profiles_tried)
@@ -301,18 +390,41 @@ def extract_firefox_cookies(
Returns:
Dict of {cookie_name: cookie_value} or None if extraction fails.
"""
- profiles_dir = _get_firefox_profiles_dir()
+ denied = None
+ try:
+ profiles_dir = _get_firefox_profiles_dir()
+ except PermissionError as exc:
+ denied = exc
+ profiles_dir = None
if profiles_dir is not None:
- result = _try_firefox_dir(profiles_dir, domain, cookie_names)
+ try:
+ result = _try_firefox_dir(profiles_dir, domain, cookie_names)
+ except PermissionError as exc:
+ denied = exc
+ result = None
if result is not None:
return result
if platform.system() == "Linux" and _is_wsl():
- wsl_dir = _get_wsl_firefox_profiles_dir()
+ try:
+ wsl_dir = _get_wsl_firefox_profiles_dir()
+ except PermissionError as exc:
+ if denied is None:
+ denied = exc
+ wsl_dir = None
if wsl_dir is not None:
logger.debug("Trying Windows Firefox via WSL: %s", wsl_dir)
- return _try_firefox_dir(wsl_dir, domain, cookie_names)
+ try:
+ result = _try_firefox_dir(wsl_dir, domain, cookie_names)
+ except PermissionError as exc:
+ if denied is None:
+ denied = exc
+ result = None
+ if result is not None:
+ return result
+ if denied is not None:
+ raise denied
if profiles_dir is None:
logger.debug("Firefox profiles directory not found")
return None
@@ -335,6 +447,8 @@ def extract_chrome_cookies(
try:
from .chrome_cookies import extract_chrome_cookies_macos
return extract_chrome_cookies_macos(domain, cookie_names)
+ except PermissionError:
+ raise
except Exception as exc:
logger.debug("Chrome cookie extraction failed: %s", exc)
return None
@@ -358,6 +472,8 @@ def extract_brave_cookies(
try:
from .chrome_cookies import extract_brave_cookies_macos
return extract_brave_cookies_macos(domain, cookie_names)
+ except PermissionError:
+ raise
except Exception as exc:
logger.debug("Brave cookie extraction failed: %s", exc)
return None
@@ -378,6 +494,8 @@ def _extract_chromium_family_cookies(
try:
from .chrome_cookies import extract_chromium_browser_cookies_macos
return extract_chromium_browser_cookies_macos(browser, domain, cookie_names)
+ except PermissionError:
+ raise
except Exception as exc:
logger.debug("%s cookie extraction failed: %s", browser, exc)
return None
@@ -424,6 +542,8 @@ def extract_safari_cookies(
try:
from .safari_cookies import extract_safari_cookies_macos
return extract_safari_cookies_macos(domain, cookie_names)
+ except PermissionError:
+ raise
except Exception as exc:
logger.debug("Safari cookie extraction failed: %s", exc)
return None
@@ -461,20 +581,41 @@ def _extract_firefox_with_source(
Returns (cookies, "firefox") for native Linux/macOS Firefox, or
(cookies, "firefox-wsl") for Windows Firefox accessed via WSL2.
"""
- profiles_dir = _get_firefox_profiles_dir()
+ denied = None
+ try:
+ profiles_dir = _get_firefox_profiles_dir()
+ except PermissionError as exc:
+ denied = exc
+ profiles_dir = None
if profiles_dir is not None:
- result = _try_firefox_dir(profiles_dir, domain, cookie_names)
+ try:
+ result = _try_firefox_dir(profiles_dir, domain, cookie_names)
+ except PermissionError as exc:
+ denied = exc
+ result = None
if result is not None:
return (result, "firefox")
if platform.system() == "Linux" and _is_wsl():
- wsl_dir = _get_wsl_firefox_profiles_dir()
+ try:
+ wsl_dir = _get_wsl_firefox_profiles_dir()
+ except PermissionError as exc:
+ if denied is None:
+ denied = exc
+ wsl_dir = None
if wsl_dir is not None:
logger.debug("Trying Windows Firefox via WSL: %s", wsl_dir)
- result = _try_firefox_dir(wsl_dir, domain, cookie_names)
+ try:
+ result = _try_firefox_dir(wsl_dir, domain, cookie_names)
+ except PermissionError as exc:
+ if denied is None:
+ denied = exc
+ result = None
if result is not None:
return (result, "firefox-wsl")
+ if denied is not None:
+ raise denied
return None
diff --git a/skills/last30days/scripts/lib/cookie_paths.py b/skills/last30days/scripts/lib/cookie_paths.py
new file mode 100644
index 00000000..884747d4
--- /dev/null
+++ b/skills/last30days/scripts/lib/cookie_paths.py
@@ -0,0 +1,27 @@
+"""Path probes that preserve browser-data permission failures."""
+
+import stat
+from pathlib import Path
+
+
+def _mode(path: Path) -> int | None:
+ try:
+ return path.stat().st_mode
+ except PermissionError:
+ raise
+ except OSError:
+ return None
+
+
+def path_exists(path: Path) -> bool:
+ return _mode(path) is not None
+
+
+def path_is_file(path: Path) -> bool:
+ mode = _mode(path)
+ return mode is not None and stat.S_ISREG(mode)
+
+
+def path_is_dir(path: Path) -> bool:
+ mode = _mode(path)
+ return mode is not None and stat.S_ISDIR(mode)
diff --git a/skills/last30days/scripts/lib/doctor.py b/skills/last30days/scripts/lib/doctor.py
index 928ce0a6..9aac1ef3 100644
--- a/skills/last30days/scripts/lib/doctor.py
+++ b/skills/last30days/scripts/lib/doctor.py
@@ -490,8 +490,8 @@ def _x_record(config):
# Policy-gated: on an official-only host no run-time cookie source
# exists unless bird is pinned, and then the note names only the pin.
#
- # This check MUST come before grok normalization: a pending bird path takes
- # precedence over marking X as unconfigured due to an unused grok store.
+ # This check MUST come before grok normalization: a pending bird path or
+ # a recorded setup denial takes precedence over an unused grok store.
# Handle both "unconfigured" (all backends missing) and "error" (grok present
# but opt-in, no auto-chain backend usable) when pending bird applies.
#
@@ -502,7 +502,24 @@ def _x_record(config):
pending_bird = policy.cookie_discovery and env.x_pending_browser_auth(
config, local_only=True
)
- if pending_bird and record["status"] in ("unconfigured", health.ERROR):
+ reported_denials = set((config.get("LAST30DAYS_X_COOKIE_ACCESS_DENIED") or "").split(","))
+ known_browsers = set(env.COOKIE_BROWSER_NAMES)
+ if (
+ policy.cookie_discovery
+ and str(config.get("BROWSER_CONSENT") or "").lower() in {"1", "true", "yes", "on"}
+ and str(config.get("FROM_BROWSER") or "").strip().lower() != "off"
+ ):
+ denied_browsers = sorted(reported_denials & known_browsers)
+ else:
+ denied_browsers = []
+ if denied_browsers and record.get("pinned") and env.x_backend_pin(config) != "bird":
+ observation = (
+ "Last setup: permission denied reading X cookies from "
+ f"{', '.join(denied_browsers)}; current access not checked"
+ )
+ record["note"] = f"{record['note']}; {observation}" if record["note"] else observation
+ return record
+ if (pending_bird or denied_browsers) and record["status"] in ("unconfigured", health.ERROR):
backends_list = record.get("backends", [])
auto_backends = [b for b in backends_list if b.get("name") in auto_chain_names]
# Only apply pending-bird upgrade if ALL auto-chain backends are MISSING.
@@ -511,6 +528,15 @@ def _x_record(config):
b.get("status") == health.MISSING for b in auto_backends
)
if all_auto_missing:
+ if denied_browsers:
+ record["status"] = health.ERROR
+ record["tier"] = TIER_BY_STATUS[health.ERROR]
+ record["note"] = (
+ "Last setup: permission denied reading X cookies from "
+ f"{', '.join(denied_browsers)}; current access not checked"
+ )
+ record["fix"] = env.X_COOKIE_ACCESS_FIX
+ return record
record["status"] = health.OK
record["tier"] = TIER_BY_STATUS[health.OK]
if policy.official_only:
@@ -1690,6 +1716,9 @@ def _config_fingerprint(config: Dict[str, Any]) -> str:
"keys_present": _setup_block(config)["keys_present"],
"pins": {var: str(config.get(var) or "") for var in _FINGERPRINT_PIN_VARS},
"include_sources": str(config.get("INCLUDE_SOURCES") or ""),
+ "x_cookie_access_denied": str(config.get("LAST30DAYS_X_COOKIE_ACCESS_DENIED") or ""),
+ "browser_consent": str(config.get("BROWSER_CONSENT") or ""),
+ "from_browser": str(config.get("FROM_BROWSER") or ""),
}
canonical = json.dumps(signals, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
diff --git a/skills/last30days/scripts/lib/env.py b/skills/last30days/scripts/lib/env.py
index 5d04e675..6cd92748 100644
--- a/skills/last30days/scripts/lib/env.py
+++ b/skills/last30days/scripts/lib/env.py
@@ -59,6 +59,11 @@ KEYCHAIN_ALIASES_ENV = "LAST30DAYS_KEYCHAIN_ALIASES"
# a stored key can silently satisfy a lookup the test meant to see fail.
KEYCHAIN_DISABLE_ENV = "LAST30DAYS_SKIP_KEYCHAIN"
+X_COOKIE_ACCESS_FIX = (
+ "Check the browser-data permissions for the terminal or agent host and retry setup. "
+ "If access remains blocked, set AUTH_TOKEN and CT0 manually or use XAI_API_KEY."
+)
+
# Single source of truth for which credentials the Keychain loader looks up.
# The setup-keychain.sh helper mirrors this list and is held in sync via
# tests/test_env_keychain.py::test_keychain_keys_match_setup_script.
@@ -711,6 +716,7 @@ def get_config(policy: ConfigLoadPolicy | None = None) -> dict[str, Any]:
('LAST30DAYS_TRUSTPILOT_NO_BROWSER', None),
('FROM_BROWSER', None),
('BROWSER_CONSENT', None),
+ ('LAST30DAYS_X_COOKIE_ACCESS_DENIED', None),
# agentcookie sidecar: soft-dep X cookie source (lib/agentcookie.py),
# active only on extra hosts (Linux / Mac mini / Darwin sink) or when
# set to "on". "off" disables the sidecar reader.
@@ -1069,6 +1075,10 @@ COOKIE_DOMAINS: dict[str, dict[str, Any]] = {
},
}
+COOKIE_BROWSER_NAMES = (
+ "firefox", "safari", "chrome", "brave", "edge", "vivaldi", "opera", "arc", "chromium"
+)
+
def cookie_extraction_browsers(config: dict[str, Any]) -> list[str]:
"""Browsers to try for cookie extraction, honoring FROM_BROWSER.
@@ -1098,9 +1108,9 @@ def cookie_extraction_browsers(config: dict[str, Any]) -> list[str]:
consent = config.get("BROWSER_CONSENT")
if consent is not None and str(consent).strip().lower() not in {"1", "true", "yes", "on"}:
return []
- silent_browsers = ["firefox", "safari"]
- chromium_browsers = ["chrome", "brave", "edge", "vivaldi", "opera", "arc", "chromium"]
- known_browsers = silent_browsers + chromium_browsers
+ silent_browsers = list(COOKIE_BROWSER_NAMES[:2])
+ chromium_browsers = list(COOKIE_BROWSER_NAMES[2:])
+ known_browsers = list(COOKIE_BROWSER_NAMES)
from_browser = (config.get("FROM_BROWSER") or "").strip().lower()
if not from_browser:
return []
@@ -1150,16 +1160,31 @@ def extract_browser_credentials(config: dict[str, Any]) -> dict[str, str]:
return {}
extracted: dict[str, str] = {}
for _service, spec in COOKIE_DOMAINS.items():
- if all(config.get(env_key) for env_key in spec["mapping"].values()):
+ missing_cookies = [
+ name for name in spec["cookies"] if not config.get(spec["mapping"][name])
+ ]
+ if not missing_cookies:
continue
# Cookies from different browsers can belong to different sessions,
# so values are never combined across browsers: a complete set from
# one browser wins, else the first browser's partial set is kept.
chosen: dict[str, str] | None = None
fallback: dict[str, str] | None = None
+ denied_browsers: list[str] = []
for browser in browsers:
try:
cookies = cookie_extract.extract_cookies(browser, spec["domain"], spec["cookies"])
+ except PermissionError:
+ denied_browsers.append(browser)
+ if len(missing_cookies) == len(spec["cookies"]):
+ continue
+ # Keep full-pair profile preference unless a denied profile blocks it.
+ try:
+ cookies = cookie_extract.extract_cookies(
+ browser, spec["domain"], missing_cookies
+ )
+ except Exception:
+ continue
except Exception:
continue
if not cookies:
@@ -1171,6 +1196,13 @@ def extract_browser_credentials(config: dict[str, Any]) -> dict[str, str]:
fallback = cookies
if chosen is None:
chosen = fallback or {}
+ if _service == "x" and denied_browsers and not cookie_extract.has_complete_pair(
+ chosen, spec["cookies"]
+ ):
+ sys.stderr.write(
+ "[last30days] X browser cookie access permission denied in "
+ f"{', '.join(denied_browsers)}. {X_COOKIE_ACCESS_FIX}\n"
+ )
for cookie_name, env_key in spec["mapping"].items():
if chosen.get(cookie_name) and not config.get(env_key):
extracted[env_key] = chosen[cookie_name]
diff --git a/skills/last30days/scripts/lib/safari_cookies.py b/skills/last30days/scripts/lib/safari_cookies.py
index 4df80472..3389db2a 100644
--- a/skills/last30days/scripts/lib/safari_cookies.py
+++ b/skills/last30days/scripts/lib/safari_cookies.py
@@ -14,6 +14,8 @@ import struct
import sys
from pathlib import Path
+from .cookie_paths import path_exists
+
# Mac epoch: 2001-01-01 00:00:00 UTC (not used for filtering, but documented)
_MAC_EPOCH_OFFSET = 978307200 # seconds between Unix epoch and Mac epoch
@@ -121,24 +123,34 @@ def extract_safari_cookies_macos(
/ "Cookies.binarycookies",
Path.home() / "Library" / "Cookies" / "Cookies.binarycookies",
]
- cookie_path = next((path for path in cookie_paths if path.exists()), cookie_paths[0])
+ denied = None
+ partial = None
+ for cookie_path in cookie_paths:
+ try:
+ if not path_exists(cookie_path):
+ continue
+ raw = cookie_path.read_bytes()
+ except PermissionError as exc:
+ if denied is None:
+ denied = exc
+ continue
+ except OSError:
+ continue
+ result = _parse_binary_cookies(raw, domain, cookie_names)
+ if result and all(result.get(name) for name in cookie_names):
+ return result
+ if result and partial is None:
+ partial = result
- try:
- raw = cookie_path.read_bytes()
- except FileNotFoundError:
- return None
- except PermissionError:
+ if denied is not None:
print(
"[safari] Permission denied reading Cookies.binarycookies. "
- "Enable Full Disk Access for Terminal in System Settings > "
- "Privacy & Security > Full Disk Access.",
+ "Check the invoking app's browser-data permissions in System Settings > "
+ "Privacy & Security, then retry setup.",
file=sys.stderr,
)
- return None
- except OSError:
- return None
-
- return _parse_binary_cookies(raw, domain, cookie_names)
+ raise denied
+ return partial
def _host_matches(stored_host: str, domain: str) -> bool:
diff --git a/skills/last30days/scripts/lib/setup_wizard.py b/skills/last30days/scripts/lib/setup_wizard.py
index c2e17fb2..245ef499 100644
--- a/skills/last30days/scripts/lib/setup_wizard.py
+++ b/skills/last30days/scripts/lib/setup_wizard.py
@@ -80,7 +80,9 @@ def run_auto_setup(config: Dict[str, Any], *, allow_browser_cookies: bool = Fals
Dict with keys:
cookies_found: {source_name: browser_name} for each source where cookies were found
browser_cookie_scan_attempted: bool (True only after explicit consent
- AND on a host whose X policy permits cookie discovery)
+ and at least one browser is eligible for extraction)
+ x_cookie_access_denied: browser names denied during an attempted X
+ cookie read when no other browser supplied a complete X pair
cookie_note: present only on an official-only host, where the scan
is skipped for every domain (neutral, relayable text)
ytdlp_installed: bool
@@ -95,7 +97,9 @@ def run_auto_setup(config: Dict[str, Any], *, allow_browser_cookies: bool = Fals
from .env import COOKIE_DOMAINS, cookie_extraction_browsers, x_policy
cookies_found: Dict[str, str] = {}
+ access_denied: dict[str, set[str]] = {}
cookie_note: Optional[str] = None
+ browsers: list[str] = []
# Official-only host (LAST30DAYS_HOST=grok-bot): the consented
# cookie scan is skipped for EVERY domain (X and Truth Social alike) and
@@ -112,9 +116,9 @@ def run_auto_setup(config: Dict[str, Any], *, allow_browser_cookies: bool = Fals
cookie_config = dict(config)
cookie_config["BROWSER_CONSENT"] = "true"
if not (cookie_config.get("FROM_BROWSER") or "").strip():
- # Chromium-first: Chrome/Brave/etc. read cookies via the Keychain
- # with no Full Disk Access, so try them before Safari, whose
- # binarycookies read requires FDA (the dead-end most users hit).
+ # Chromium-first avoids Safari's usual Full Disk Access prompt.
+ # Browser-data permissions can still deny Chromium DB reads before
+ # the Keychain step, so a failed scan must report that separately.
# firefox/safari stay as the silent fallbacks. Note: an explicit
# comma list preserves this order (cookie_extraction_browsers);
# "auto" would put the silent browsers first, so do not use it here.
@@ -128,6 +132,9 @@ def run_auto_setup(config: Dict[str, Any], *, allow_browser_cookies: bool = Fals
for browser in browsers:
try:
result = cookie_extract.extract_cookies_with_source(browser, domain, cookie_names)
+ except PermissionError:
+ access_denied.setdefault(source_name, set()).add(browser)
+ continue
except Exception as exc:
logger.debug("Cookie extraction failed for %s via %s: %s", source_name, browser, exc)
continue
@@ -173,9 +180,14 @@ def run_auto_setup(config: Dict[str, Any], *, allow_browser_cookies: bool = Fals
digg_installed, digg_action, digg_stderr, digg_path = _install_digg_cli()
pp_sources = install_default_pp_sources()
+ scan_attempted = allow_browser_cookies and bool(browsers)
results: Dict[str, Any] = {
"cookies_found": cookies_found,
- "browser_cookie_scan_attempted": allow_browser_cookies,
+ "x_cookie_access_denied": (
+ [browser for browser in browsers if browser in access_denied.get("x", set())]
+ if scan_attempted and "x" not in cookies_found else []
+ ),
+ "browser_cookie_scan_attempted": scan_attempted,
"ytdlp_installed": ytdlp_installed,
"ytdlp_action": ytdlp_action,
"digg_installed": digg_installed,
@@ -607,6 +619,7 @@ def write_setup_config(
from_browser: str | None = None,
*,
browser_consent: bool | None = None,
+ x_cookie_access_denied: str | None = None,
) -> bool:
"""Write setup completion, browser selection, and consent to the .env file.
@@ -623,6 +636,8 @@ def write_setup_config(
browsers that did not supply cookies during setup.
browser_consent: Record the user's current cookie-access decision.
None preserves any previous decision.
+ x_cookie_access_denied: Setup's non-secret last X denial observation.
+ Pass "none" to clear a prior denial after a new setup run.
Returns:
True if config was written successfully, False on error.
@@ -636,6 +651,12 @@ def write_setup_config(
key_name="BROWSER_CONSENT", replace=True,
):
return False
+ if x_cookie_access_denied is not None:
+ if not write_api_key(
+ env_path, x_cookie_access_denied,
+ key_name="LAST30DAYS_X_COOKIE_ACCESS_DENIED", replace=True,
+ ):
+ return False
# Read existing content to avoid overwriting keys
existing_keys: set = set()
@@ -748,6 +769,8 @@ def get_setup_status_text(results: Dict[str, Any]) -> str:
Returns:
Multi-line status text.
"""
+ from .env import X_COOKIE_ACCESS_FIX
+
lines = []
lines.append("Setup complete! Here's what I found:")
lines.append("")
@@ -756,6 +779,15 @@ def get_setup_status_text(results: Dict[str, Any]) -> str:
if results.get("browser_cookie_scan_attempted") and cookies_found:
for source, browser in cookies_found.items():
lines.append(f" - {source.upper()} cookies found in {browser}")
+ denied_browsers = (
+ results.get("x_cookie_access_denied") or []
+ if results.get("browser_cookie_scan_attempted") else []
+ )
+ if denied_browsers:
+ lines.append(
+ " - X cookie access was permission denied in "
+ f"{', '.join(denied_browsers)}. {X_COOKIE_ACCESS_FIX}"
+ )
ytdlp_action = results.get("ytdlp_action", "")
if ytdlp_action == "installed":
@@ -876,6 +908,8 @@ def get_setup_status_text(results: Dict[str, Any]) -> str:
lines.append("")
if cookie_note:
lines.append("Configuration saved.")
+ elif denied_browsers:
+ lines.append("Configuration saved. X browser cookie access remains unverified.")
else:
lines.append("Configuration saved. Future runs will auto-detect your browsers.")
diff --git a/tests/test_chrome_cookies.py b/tests/test_chrome_cookies.py
index c97b6e34..b0a83eca 100644
--- a/tests/test_chrome_cookies.py
+++ b/tests/test_chrome_cookies.py
@@ -30,6 +30,85 @@ from lib.chrome_cookies import (
extract_chrome_cookies_macos,
)
+
+def test_cookie_copy_permission_denial_is_not_reported_as_missing(tmp_path):
+ db = tmp_path / "Cookies"
+ db.touch()
+ with mock.patch(
+ "lib.chrome_cookies.shutil.copyfile",
+ side_effect=PermissionError(1, "Operation not permitted", str(db)),
+ ):
+ with pytest.raises(PermissionError):
+ _extract_chromium_cookies_macos(
+ db, "Microsoft Edge Safe Storage", ".x.com", ["auth_token", "ct0"]
+ )
+
+
+def test_temporary_storage_denial_is_not_blamed_on_browser_access(tmp_path):
+ db = tmp_path / "Cookies"
+ db.touch()
+ denied_temp = tmp_path / "temporary.sqlite"
+ with mock.patch(
+ "lib.chrome_cookies.shutil.copyfile",
+ side_effect=PermissionError(13, "Permission denied", str(denied_temp)),
+ ):
+ assert _extract_chromium_cookies_macos(
+ db, "Microsoft Edge Safe Storage", ".x.com", ["auth_token", "ct0"]
+ ) is None
+
+
+def test_keychain_permission_denial_is_not_blamed_on_browser_database():
+ with mock.patch(
+ "lib.chrome_cookies.subprocess.run",
+ side_effect=PermissionError(1, "Operation not permitted", "security"),
+ ):
+ assert chrome_cookies._get_chromium_encryption_key("Microsoft Edge Safe Storage") is None
+
+
+def test_profile_discovery_preserves_permission_denial_when_stat_fails(tmp_path):
+ blocked = tmp_path / "Default" / "Network" / "Cookies"
+ real_stat = Path.stat
+ real_exists = Path.exists
+
+ def guarded_stat(path, *args, **kwargs):
+ if path == blocked:
+ raise PermissionError(1, "Operation not permitted", str(path))
+ return real_stat(path, *args, **kwargs)
+
+ def simulated_py314_exists(path, *args, **kwargs):
+ if path == blocked:
+ return False
+ return real_exists(path, *args, **kwargs)
+
+ with mock.patch.object(Path, "stat", guarded_stat), mock.patch.object(
+ Path, "exists", simulated_py314_exists
+ ):
+ with pytest.raises(PermissionError):
+ chrome_cookies._profile_cookie_db(tmp_path / "Default")
+
+
+def test_profile_directory_discovery_preserves_permission_denial(tmp_path):
+ blocked = tmp_path / "Profile 1"
+ blocked.mkdir()
+ real_stat = Path.stat
+ real_is_dir = Path.is_dir
+
+ def guarded_stat(path, *args, **kwargs):
+ if path == blocked:
+ raise PermissionError(1, "Operation not permitted", str(path))
+ return real_stat(path, *args, **kwargs)
+
+ def simulated_py314_is_dir(path, *args, **kwargs):
+ if path == blocked:
+ return False
+ return real_is_dir(path, *args, **kwargs)
+
+ with mock.patch.object(Path, "stat", guarded_stat), mock.patch.object(
+ Path, "is_dir", simulated_py314_is_dir
+ ):
+ with pytest.raises(PermissionError):
+ chrome_cookies._find_all_chromium_cookies_dbs(tmp_path)
+
# ---------------------------------------------------------------------------
# Helpers — create real encrypted cookie values using known key + system openssl
# ---------------------------------------------------------------------------
@@ -121,6 +200,49 @@ def _create_chrome_cookies_db(path: str, cookies: list[tuple], db_version: int =
conn.commit()
conn.close()
+
+def test_denied_modern_path_uses_accessible_legacy_cookie_database(tmp_path):
+ profile = tmp_path / "Default"
+ profile.mkdir()
+ legacy = profile / "Cookies"
+ legacy.touch()
+ blocked = profile / "Network" / "Cookies"
+ real_stat = Path.stat
+
+ def guarded_stat(path, *args, **kwargs):
+ if path == blocked:
+ raise PermissionError(1, "Operation not permitted", str(path))
+ return real_stat(path, *args, **kwargs)
+
+ with mock.patch.object(Path, "stat", guarded_stat):
+ assert chrome_cookies._profile_cookie_db(profile) == legacy
+
+
+def test_denied_default_copy_uses_complete_pair_in_later_profile(tmp_path):
+ default = tmp_path / "Default" / "Network"
+ alternate = tmp_path / "Profile 1" / "Network"
+ default.mkdir(parents=True)
+ alternate.mkdir(parents=True)
+ blocked = default / "Cookies"
+ blocked.touch()
+ available = alternate / "Cookies"
+ _create_chrome_cookies_db(str(available), [
+ (".x.com", "auth_token", "dummy-auth", b""),
+ (".x.com", "ct0", "dummy-ct0", b""),
+ ])
+ real_copyfile = shutil.copyfile
+
+ def guarded_copyfile(source, target):
+ if source == str(blocked):
+ raise PermissionError(1, "Operation not permitted", str(blocked))
+ return real_copyfile(source, target)
+
+ with mock.patch("lib.chrome_cookies.shutil.copyfile", side_effect=guarded_copyfile):
+ found = chrome_cookies._extract_chromium_cookies_any_profile(
+ tmp_path, "Chrome Safe Storage", ".x.com", ["auth_token", "ct0"]
+ )
+ assert found == {"auth_token": "dummy-auth", "ct0": "dummy-ct0"}
+
# ---------------------------------------------------------------------------
# PKCS7 padding tests
# ---------------------------------------------------------------------------
diff --git a/tests/test_cookie_extract.py b/tests/test_cookie_extract.py
index 5a65e1f6..78f1f20e 100644
--- a/tests/test_cookie_extract.py
+++ b/tests/test_cookie_extract.py
@@ -10,7 +10,7 @@ from unittest.mock import patch
import pytest
-from lib import cookie_extract
+from lib import cookie_extract, env
from lib.cookie_extract import (
extract_cookies,
extract_firefox_cookies,
@@ -20,6 +20,92 @@ from lib.cookie_extract import (
_get_firefox_profiles_dir,
)
+
+def test_firefox_database_permission_denial_survives_extractor(tmp_path):
+ db = tmp_path / "cookies.sqlite"
+ db.touch()
+ with patch(
+ "lib.cookie_extract.shutil.copyfile",
+ side_effect=PermissionError(1, "Operation not permitted", str(db)),
+ ):
+ with pytest.raises(PermissionError):
+ _query_cookies_db(db, ".x.com", ["auth_token", "ct0"])
+
+
+def test_firefox_database_discovery_preserves_stat_permission_denial(tmp_path):
+ db = tmp_path / "cookies.sqlite"
+ real_stat = Path.stat
+ real_is_file = Path.is_file
+
+ def guarded_stat(path, *args, **kwargs):
+ if path == db:
+ raise PermissionError(1, "Operation not permitted", str(path))
+ return real_stat(path, *args, **kwargs)
+
+ def simulated_py314_is_file(path, *args, **kwargs):
+ if path == db:
+ return False
+ return real_is_file(path, *args, **kwargs)
+
+ with patch.object(Path, "stat", guarded_stat), patch.object(
+ Path, "is_file", simulated_py314_is_file
+ ):
+ with pytest.raises(PermissionError):
+ _query_cookies_db(db, ".x.com", ["auth_token", "ct0"])
+
+
+def test_wsl_firefox_directory_skips_denied_user_for_accessible_user(tmp_path):
+ users = tmp_path / "Users"
+ denied_user = users / "aaa"
+ available = users / "bbb" / "AppData" / "Roaming" / "Mozilla" / "Firefox"
+ denied_user.mkdir(parents=True)
+ available.mkdir(parents=True)
+ real_stat = Path.stat
+ real_is_dir = Path.is_dir
+
+ def guarded_stat(path, *args, **kwargs):
+ if path == denied_user:
+ raise PermissionError(1, "Operation not permitted", str(path))
+ return real_stat(path, *args, **kwargs)
+
+ def simulated_py314_is_dir(path, *args, **kwargs):
+ if path == denied_user:
+ return False
+ return real_is_dir(path, *args, **kwargs)
+
+ with patch("lib.cookie_extract.Path", return_value=users), patch.object(
+ Path, "stat", guarded_stat
+ ), patch.object(Path, "is_dir", simulated_py314_is_dir):
+ found = cookie_extract._get_wsl_firefox_profiles_dir()
+ assert found == available
+
+
+def test_linux_firefox_directory_tries_xdg_after_default_denial(tmp_path, monkeypatch):
+ blocked = tmp_path / ".mozilla" / "firefox"
+ available = tmp_path / "xdg" / "mozilla" / "firefox"
+ available.mkdir(parents=True)
+ monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "xdg"))
+ real_stat = Path.stat
+ real_is_dir = Path.is_dir
+
+ def guarded_stat(path, *args, **kwargs):
+ if path == blocked:
+ raise PermissionError(1, "Operation not permitted", str(path))
+ return real_stat(path, *args, **kwargs)
+
+ def simulated_py314_is_dir(path, *args, **kwargs):
+ if path == blocked:
+ return False
+ return real_is_dir(path, *args, **kwargs)
+
+ with patch("lib.cookie_extract.Path.home", return_value=tmp_path), patch(
+ "lib.cookie_extract.platform.system", return_value="Linux"
+ ), patch.object(Path, "stat", guarded_stat), patch.object(
+ Path, "is_dir", simulated_py314_is_dir
+ ):
+ found = _get_firefox_profiles_dir()
+ assert found == available
+
@pytest.fixture
def mock_firefox_env(tmp_path):
"""Create a mock Firefox profiles directory with cookies.sqlite.
@@ -100,6 +186,107 @@ def mock_firefox_env(tmp_path):
class TestExtractFirefoxCookies:
"""Tests for extract_firefox_cookies."""
+ def test_partial_cookie_survives_later_profile_denial(self, mock_firefox_env, capsys):
+ profiles_dir = mock_firefox_env(
+ profiles={
+ "abc123.default-release": [(".x.com", "ct0", "dummy-ct0")],
+ "xyz789.other": [],
+ }
+ )
+ blocked = profiles_dir / "xyz789.other" / "cookies.sqlite"
+ real_copyfile = cookie_extract.shutil.copyfile
+ requested = []
+
+ def guarded_copyfile(source, target):
+ if source == str(blocked):
+ raise PermissionError(1, "Operation not permitted", str(blocked))
+ return real_copyfile(source, target)
+
+ def extract_requested(_browser, domain, cookie_names):
+ if domain == ".x.com":
+ requested.append(cookie_names)
+ return cookie_extract._try_firefox_dir(profiles_dir, domain, cookie_names)
+
+ with patch("lib.cookie_extract.shutil.copyfile", side_effect=guarded_copyfile), patch(
+ "lib.cookie_extract.extract_cookies",
+ side_effect=extract_requested,
+ ):
+ found = env.extract_browser_credentials({
+ "FROM_BROWSER": "firefox", "BROWSER_CONSENT": "true",
+ "AUTH_TOKEN": "dummy-auth",
+ })
+ assert found == {"CT0": "dummy-ct0"}
+ assert requested == [["auth_token", "ct0"], ["ct0"]]
+ assert "permission denied" in capsys.readouterr().err.lower()
+
+ def test_manual_token_keeps_complete_profile_preference_without_denial(self, mock_firefox_env):
+ profiles_dir = mock_firefox_env(
+ profiles={
+ "abc123.default-release": [(".x.com", "ct0", "stale-ct0")],
+ "xyz789.other": [
+ (".x.com", "auth_token", "dummy-auth"),
+ (".x.com", "ct0", "matching-ct0"),
+ ],
+ }
+ )
+ requested = []
+
+ def extract_requested(_browser, domain, cookie_names):
+ if domain == ".x.com":
+ requested.append(cookie_names)
+ return cookie_extract._try_firefox_dir(profiles_dir, domain, cookie_names)
+
+ with patch("lib.cookie_extract.extract_cookies", side_effect=extract_requested):
+ found = env.extract_browser_credentials({
+ "FROM_BROWSER": "firefox", "BROWSER_CONSENT": "true",
+ "AUTH_TOKEN": "dummy-auth",
+ })
+ assert found == {"CT0": "matching-ct0"}
+ assert requested == [["auth_token", "ct0"]]
+
+ def test_later_install_entry_outside_profiles_survives_first_denial(self, tmp_path):
+ profiles_dir = tmp_path / "Firefox"
+ profiles_dir.mkdir()
+ blocked = tmp_path / "blocked"
+ available = tmp_path / "external-profile"
+ available.mkdir()
+ (profiles_dir / "profiles.ini").write_text(
+ f"[InstallA]\nDefault={blocked}\n[InstallB]\nDefault={available}\n"
+ )
+ real_stat = Path.stat
+
+ def guarded_stat(path, *args, **kwargs):
+ if path == blocked:
+ raise PermissionError(1, "Operation not permitted", str(path))
+ return real_stat(path, *args, **kwargs)
+
+ with patch.object(Path, "stat", guarded_stat):
+ assert _find_default_profile(profiles_dir) == available
+
+ def test_denied_default_copy_uses_complete_pair_in_alternate_profile(self, mock_firefox_env):
+ profiles_dir = mock_firefox_env(
+ profiles={
+ "abc123.default-release": [],
+ "xyz789.other": [
+ (".x.com", "auth_token", "dummy-auth"),
+ (".x.com", "ct0", "dummy-ct0"),
+ ],
+ }
+ )
+ blocked = profiles_dir / "abc123.default-release" / "cookies.sqlite"
+ real_copyfile = cookie_extract.shutil.copyfile
+
+ def guarded_copyfile(source, target):
+ if source == str(blocked):
+ raise PermissionError(1, "Operation not permitted", str(blocked))
+ return real_copyfile(source, target)
+
+ with patch("lib.cookie_extract.shutil.copyfile", side_effect=guarded_copyfile):
+ found = cookie_extract._try_firefox_dir(
+ profiles_dir, ".x.com", ["auth_token", "ct0"]
+ )
+ assert found == {"auth_token": "dummy-auth", "ct0": "dummy-ct0"}
+
@pytest.mark.skipif(os.name == "nt", reason="POSIX permission bits are not reliable on Windows")
def test_temp_cookie_db_copy_is_owner_only(self, tmp_path):
"""Copied cookie DB temp files are chmodded owner-only before read."""
diff --git a/tests/test_doctor.py b/tests/test_doctor.py
index 90eb8ff5..caf2f5c4 100644
--- a/tests/test_doctor.py
+++ b/tests/test_doctor.py
@@ -30,7 +30,7 @@ from pathlib import Path
from unittest import mock
import last30days as cli
-from lib import backends, doctor, env, grok_x, health, http, prescriptions, reddit_search
+from lib import backends, doctor, env, grok_x, health, http, prescriptions, reddit_search, setup_wizard
BIRD_STATUS_OFF = {
"installed": False,
@@ -284,6 +284,134 @@ class CookieBackedXReadiness(unittest.TestCase):
self.assertIn("not verified", note)
self.assertIn("xai_api_key", note)
+ def test_last_setup_permission_denial_overrides_unverified_cookie_prediction(self):
+ config = {
+ "FROM_BROWSER": "edge", "BROWSER_CONSENT": "true",
+ "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge",
+ }
+ with _Hermetic(), mock.patch(
+ "lib.bird_x.is_bird_installed", return_value=True
+ ), mock.patch(
+ "lib.cookie_extract.extract_cookies",
+ side_effect=AssertionError("doctor read cookies"),
+ ):
+ record = doctor._x_record(config)
+ self.assertEqual("error", record["tier"])
+ self.assertIn("Last setup", record["note"])
+ self.assertIn("permission denied", record["note"])
+ self.assertIn("AUTH_TOKEN", record["fix"])
+
+ def test_last_setup_denial_preserves_exclusive_xurl_pin_repair(self):
+ config = {
+ "LAST30DAYS_X_BACKEND": "xurl",
+ "FROM_BROWSER": "edge", "BROWSER_CONSENT": "true",
+ "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge",
+ }
+ with _Hermetic():
+ record = doctor._x_record(config)
+ self.assertTrue(record["pinned"])
+ self.assertIn("xurl", record["fix"].lower())
+ self.assertIn("Last setup", record["note"])
+
+ def test_discovery_stat_denial_reaches_setup_and_no_read_doctor(self):
+ with tempfile.TemporaryDirectory() as temp_dir:
+ profiles = Path(temp_dir)
+ profile = profiles / "default"
+ profile.mkdir()
+ blocked = profile / "cookies.sqlite"
+ blocked.touch()
+ real_stat = Path.stat
+ real_is_file = Path.is_file
+
+ def guarded_stat(path, *args, **kwargs):
+ if path == blocked:
+ raise PermissionError(1, "Operation not permitted", str(path))
+ return real_stat(path, *args, **kwargs)
+
+ def simulated_py314_is_file(path, *args, **kwargs):
+ if path == blocked:
+ return False
+ return real_is_file(path, *args, **kwargs)
+
+ with mock.patch("lib.cookie_extract._get_firefox_profiles_dir", return_value=profiles), mock.patch(
+ "lib.cookie_extract._is_wsl", return_value=False
+ ), mock.patch.object(
+ Path, "stat", guarded_stat
+ ), mock.patch.object(Path, "is_file", simulated_py314_is_file), mock.patch(
+ "lib.setup_wizard.shutil.which", return_value="/usr/bin/fake"
+ ), mock.patch("lib.setup_wizard._install_digg_cli", return_value=(True, "already_installed", "", "")), mock.patch(
+ "lib.setup_wizard.install_default_pp_sources", return_value={}
+ ), mock.patch("lib.setup_wizard.brightdata_status", return_value={}):
+ results = setup_wizard.run_auto_setup(
+ {"FROM_BROWSER": "firefox"}, allow_browser_cookies=True
+ )
+ self.assertEqual(["firefox"], results["x_cookie_access_denied"], results)
+ self.assertIn("permission denied", setup_wizard.get_setup_status_text(results).lower())
+ config = {
+ "FROM_BROWSER": "firefox",
+ "BROWSER_CONSENT": "true",
+ "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "firefox",
+ }
+ with _Hermetic(), mock.patch(
+ "lib.cookie_extract.extract_cookies",
+ side_effect=AssertionError("doctor read cookies"),
+ ):
+ record = doctor._x_record(config)
+ self.assertEqual("error", record["tier"])
+ self.assertIn("Last setup", record["note"])
+
+ def test_manual_cookie_pair_supersedes_last_setup_denial(self):
+ config = {
+ "FROM_BROWSER": "edge", "BROWSER_CONSENT": "true",
+ "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge",
+ "AUTH_TOKEN": "dummy-auth-token", "CT0": "dummy-ct0",
+ }
+ with _Hermetic(probe_map={"node": health.OK}), mock.patch(
+ "lib.bird_x.is_bird_installed", return_value=True
+ ):
+ record = doctor._x_record(config)
+ self.assertEqual("ok", record["tier"])
+ self.assertNotIn("permission denied", record["note"])
+
+ def test_denial_after_auto_setup_is_visible_without_from_browser(self):
+ config = {
+ "BROWSER_CONSENT": "true",
+ "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge",
+ }
+ with _Hermetic(), mock.patch(
+ "lib.cookie_extract.extract_cookies",
+ side_effect=AssertionError("doctor read cookies"),
+ ):
+ record = doctor._x_record(config)
+ self.assertEqual("error", record["tier"])
+ self.assertIn("Last setup", record["note"])
+
+ def test_other_source_browser_selection_does_not_hide_x_denial(self):
+ config = {
+ "FROM_BROWSER": "chrome", "BROWSER_CONSENT": "true",
+ "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge",
+ }
+ with _Hermetic(), mock.patch("lib.bird_x.is_bird_installed", return_value=True):
+ record = doctor._x_record(config)
+ self.assertEqual("error", record["tier"])
+ self.assertIn("edge", record["note"])
+
+ def test_refused_browser_access_suppresses_old_denial_marker(self):
+ config = {
+ "FROM_BROWSER": "off", "BROWSER_CONSENT": "false",
+ "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge",
+ }
+ with _Hermetic():
+ record = doctor._x_record(config)
+ self.assertNotIn("permission denied", record.get("note", ""))
+
+ def test_denial_marker_invalidates_cached_doctor_result(self):
+ before = doctor._config_fingerprint({"FROM_BROWSER": "edge"})
+ after = doctor._config_fingerprint({
+ "FROM_BROWSER": "edge", "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge",
+ })
+ assert before != after
+
def test_x_stays_off_when_bird_installed_but_no_consent(self):
# bird installed but FROM_BROWSER=off -> no cookie path -> genuinely off.
with _Hermetic(), mock.patch("lib.bird_x.is_bird_installed", return_value=True):
@@ -977,6 +1105,23 @@ class JsonContract(unittest.TestCase):
self.assertIn("audit_state", served["sources"]["reddit"])
self.assertIn("WORKING", doctor.render_text(served))
+ def test_cached_denial_invalidated_by_browser_gate_changes(self):
+ tmp = Path(tempfile.mkdtemp()) / "doctor-cache.json"
+ config = {
+ "FROM_BROWSER": "edge",
+ "BROWSER_CONSENT": "true",
+ "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge",
+ }
+ with _Hermetic(), mock.patch("lib.doctor.cache_path", return_value=tmp):
+ report = doctor.build_report(config)
+ report["generated_at"] = datetime.datetime.now(
+ datetime.timezone.utc
+ ).isoformat()
+ self.assertTrue(doctor._write_cache(report, config))
+ self.assertIsNotNone(doctor.read_cached_report(config))
+ self.assertIsNone(doctor.read_cached_report({**config, "BROWSER_CONSENT": "false"}))
+ self.assertIsNone(doctor.read_cached_report({**config, "FROM_BROWSER": "off"}))
+
class LiveProbe(unittest.TestCase):
"""U5: bounded live probe (--probe / no-fresh-run auto-fallback)."""
diff --git a/tests/test_env_cookies.py b/tests/test_env_cookies.py
index 896b1781..7b1dfb06 100644
--- a/tests/test_env_cookies.py
+++ b/tests/test_env_cookies.py
@@ -8,6 +8,22 @@ import pytest
from lib.env import ConfigLoadPolicy, extract_browser_credentials, COOKIE_DOMAINS
+def test_research_reports_permission_denied_without_cookie_values(capsys):
+ with patch(
+ "lib.cookie_extract.extract_cookies",
+ side_effect=PermissionError(1, "Operation not permitted", "/private/secret/Cookies"),
+ ):
+ extracted = extract_browser_credentials(
+ _base_config(FROM_BROWSER="edge", BROWSER_CONSENT="true")
+ )
+ assert extracted == {}
+ notice = capsys.readouterr().err
+ assert "permission denied" in notice.lower()
+ assert "edge" in notice
+ assert "AUTH_TOKEN" in notice and "CT0" in notice
+ assert "/private/secret" not in notice
+
+
def _base_config(**overrides):
"""Return a minimal config dict with common defaults."""
cfg = {
diff --git a/tests/test_grok_bot_host_contract.py b/tests/test_grok_bot_host_contract.py
index 72bf502e..8d2b2b58 100644
--- a/tests/test_grok_bot_host_contract.py
+++ b/tests/test_grok_bot_host_contract.py
@@ -95,7 +95,7 @@ def _extras_passages(text: str) -> dict[str, str]:
modal = _slice_between(step0, "### Claude Code Modal Flow", "### Non-Modal Prose Flow")
prose = _slice_between(step0, "### Non-Modal Prose Flow", FLOW_HEADING)
manual = step0[step0.index("### Manual Setup Guide") :]
- modal_extras = _slice_between(modal, "**Extras-host X login", "**macOS Full Disk Access")
+ modal_extras = _slice_between(modal, "**Extras-host X login", "**macOS browser-data permission remediation")
prose_extras = _slice_between(prose, "**Extras hosts", " - On **no**")
manual_extras = _slice_between(manual, "**X on Linux / Mac mini (repair).**", "**Reddit (free")
return {"modal": modal_extras, "prose": prose_extras, "manual": manual_extras}
diff --git a/tests/test_onboarding_contract.py b/tests/test_onboarding_contract.py
index ab2e5c1e..a28c5540 100644
--- a/tests/test_onboarding_contract.py
+++ b/tests/test_onboarding_contract.py
@@ -388,9 +388,12 @@ class TestOnboardingContract(unittest.TestCase):
self.assertNotIn("only a Firefox/Safari winner", flow)
self.assertNotIn("Chrome never re-", flow)
- def test_fda_reframed_as_safari_fallback(self):
- """Full Disk Access is framed as Safari-only, not the default path."""
+ def test_browser_permission_remediation_keeps_chromium_first(self):
+ """Browser permission guidance does not change consented scan order."""
self.assertNotIn("scan your browser (Firefox/Safari)", self.modal)
+ self.assertIn("X cookie access was permission denied", self.modal)
+ self.assertIn("X cookie access was permission denied", self.prose)
+ self.assertIn("AUTH_TOKEN and CT0", self.modal)
def test_welcome_embedded_in_modal(self):
"""The welcome pitch lives INSIDE the setup modal (the only always-visible
diff --git a/tests/test_safari_cookies.py b/tests/test_safari_cookies.py
index 82bd702a..1b47deb3 100644
--- a/tests/test_safari_cookies.py
+++ b/tests/test_safari_cookies.py
@@ -301,10 +301,33 @@ class TestErrorPaths:
):
mock_sys.platform = "darwin"
mock_sys.stderr = sys.stderr
- result = extract_safari_cookies_macos("x.com", ["auth_token"])
- assert result is None
+ with pytest.raises(PermissionError):
+ extract_safari_cookies_macos("x.com", ["auth_token"])
captured = capsys.readouterr()
- assert "Full Disk Access" in captured.err
+ assert "browser-data permissions" in captured.err
+
+ def test_denied_container_uses_accessible_legacy_file(self, tmp_path, x_cookies_file):
+ container = (
+ tmp_path / "Library" / "Containers" / "com.apple.Safari" / "Data"
+ / "Library" / "Cookies" / "Cookies.binarycookies"
+ )
+ legacy = tmp_path / "Library" / "Cookies" / "Cookies.binarycookies"
+ container.parent.mkdir(parents=True)
+ legacy.parent.mkdir(parents=True)
+ container.write_bytes(b"cook")
+ legacy.write_bytes(x_cookies_file)
+ real_read_bytes = Path.read_bytes
+
+ def guarded_read_bytes(path):
+ if path == container:
+ raise PermissionError(1, "Operation not permitted", str(container))
+ return real_read_bytes(path)
+
+ with patch("lib.safari_cookies.Path.home", return_value=tmp_path), patch(
+ "lib.safari_cookies.sys.platform", "darwin"
+ ), patch.object(Path, "read_bytes", guarded_read_bytes):
+ found = extract_safari_cookies_macos("x.com", ["auth_token", "ct0"])
+ assert found == {"auth_token": "test_auth_abc123", "ct0": "test_ct0_xyz789"}
def test_truncated_magic_only(self):
result = _parse_binary_cookies(b"cook", "x.com", ["auth_token"])
diff --git a/tests/test_setup_wizard.py b/tests/test_setup_wizard.py
index 592b2635..dc69ce29 100644
--- a/tests/test_setup_wizard.py
+++ b/tests/test_setup_wizard.py
@@ -819,6 +819,76 @@ class TestGetSetupStatusText:
assert "browser cookies" not in text.lower()
assert "X/Twitter" not in text
+ def test_consented_permission_denial_reports_x_and_safe_alternatives(self):
+ with patch(
+ "lib.cookie_extract.extract_cookies_with_source",
+ side_effect=PermissionError(1, "Operation not permitted", "/private/secret/Cookies"),
+ ), patch("shutil.which", return_value=None):
+ results = setup_wizard.run_auto_setup(
+ {"FROM_BROWSER": "edge"}, allow_browser_cookies=True
+ )
+ assert results["cookies_found"] == {}
+ assert results["x_cookie_access_denied"] == ["edge"]
+ text = setup_wizard.get_setup_status_text(results)
+ assert "permission denied" in text.lower()
+ assert "edge" in text
+ assert "AUTH_TOKEN" in text and "CT0" in text
+ assert "XAI_API_KEY" in text
+ assert "/private/secret" not in text
+
+ def test_missing_cookies_do_not_record_permission_denial(self):
+ with patch(
+ "lib.cookie_extract.extract_cookies_with_source", return_value=None
+ ), patch("shutil.which", return_value=None):
+ results = setup_wizard.run_auto_setup(
+ {"FROM_BROWSER": "edge"}, allow_browser_cookies=True
+ )
+ assert results["x_cookie_access_denied"] == []
+
+ def test_complete_pair_from_another_browser_supersedes_denial(self):
+ def extract(browser, domain, _cookie_names):
+ if browser == "edge":
+ raise PermissionError(1, "Operation not permitted", "/private/Cookies")
+ if browser == "firefox" and domain == ".x.com":
+ return ({"auth_token": "dummy-auth", "ct0": "dummy-ct0"}, "firefox")
+ return None
+
+ with patch(
+ "lib.cookie_extract.extract_cookies_with_source", side_effect=extract
+ ), patch("shutil.which", return_value=None):
+ results = setup_wizard.run_auto_setup(
+ {"FROM_BROWSER": "edge,firefox"}, allow_browser_cookies=True
+ )
+ assert results["cookies_found"]["x"] == "firefox"
+ assert results["x_cookie_access_denied"] == []
+
+ def test_other_source_success_does_not_clear_x_denial(self):
+ def extract(browser, domain, _cookie_names):
+ if domain == ".x.com" and browser == "edge":
+ raise PermissionError(1, "Operation not permitted", "/private/Cookies")
+ if domain == ".truthsocial.com" and browser == "chrome":
+ return ({"_session_id": "dummy-session"}, "chrome")
+ return None
+
+ with patch(
+ "lib.cookie_extract.extract_cookies_with_source", side_effect=extract
+ ), patch("shutil.which", return_value=None):
+ results = setup_wizard.run_auto_setup({}, allow_browser_cookies=True)
+ assert results["cookies_found"] == {"truthsocial": "chrome"}
+ assert results["x_cookie_access_denied"] == ["edge"]
+
+ def test_last_setup_denial_marker_is_replaced_after_success(self, tmp_path):
+ env_path = tmp_path / ".env"
+ assert setup_wizard.write_setup_config(
+ env_path, browser_consent=True, x_cookie_access_denied="edge"
+ )
+ assert setup_wizard.write_setup_config(
+ env_path, browser_consent=True, x_cookie_access_denied="none"
+ )
+ content = env_path.read_text()
+ assert "LAST30DAYS_X_COOKIE_ACCESS_DENIED=none" in content
+ assert "LAST30DAYS_X_COOKIE_ACCESS_DENIED=edge" not in content
+
def test_status_text_installed(self):
"""Status text for freshly installed yt-dlp."""
results = {
@@ -925,6 +995,60 @@ class TestGetSetupStatusText:
class TestSetupSubcommand:
"""Tests for setup dispatch through the production entrypoint."""
+ def test_permission_denial_reaches_cli_and_saved_doctor_marker(self, tmp_path, capsys):
+ env_path = tmp_path / ".env"
+ results = {
+ "cookies_found": {},
+ "x_cookie_access_denied": ["edge"],
+ "browser_cookie_scan_attempted": True,
+ "ytdlp_action": "already_installed",
+ }
+ with patch.object(sys, "argv", ["last30days", "setup", "--allow-browser-cookies"]), patch.object(
+ cli.env, "get_config", return_value={"FROM_BROWSER": "edge"}
+ ), patch.object(cli.env, "CONFIG_FILE", env_path), patch.object(
+ setup_wizard, "run_auto_setup", return_value=results
+ ):
+ assert cli.main() == 0
+ notice = capsys.readouterr().err
+ assert "permission denied" in notice.lower()
+ assert "edge" in notice
+ assert "LAST30DAYS_X_COOKIE_ACCESS_DENIED=edge" in env_path.read_text()
+
+ def test_no_scan_preserves_last_consented_denial(self, tmp_path):
+ env_path = tmp_path / ".env"
+ env_path.write_text("LAST30DAYS_X_COOKIE_ACCESS_DENIED=edge\n")
+ results = {
+ "cookies_found": {},
+ "x_cookie_access_denied": [],
+ "browser_cookie_scan_attempted": False,
+ "ytdlp_action": "already_installed",
+ }
+ with patch.object(sys, "argv", ["last30days", "setup"]), patch.object(
+ cli.env, "get_config",
+ return_value={"LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge"},
+ ), patch.object(cli.env, "CONFIG_FILE", env_path), patch.object(
+ setup_wizard, "run_auto_setup", return_value=results
+ ):
+ assert cli.main() == 0
+ assert "LAST30DAYS_X_COOKIE_ACCESS_DENIED=edge" in env_path.read_text()
+
+ def test_explicit_browser_off_with_consent_does_not_clear_last_denial(self, tmp_path):
+ env_path = tmp_path / ".env"
+ env_path.write_text("LAST30DAYS_X_COOKIE_ACCESS_DENIED=edge\nFROM_BROWSER=off\n")
+ with patch.object(sys, "argv", ["last30days", "setup", "--allow-browser-cookies"]), patch.object(
+ cli.env, "get_config",
+ return_value={"FROM_BROWSER": "off", "LAST30DAYS_X_COOKIE_ACCESS_DENIED": "edge"},
+ ), patch.object(cli.env, "CONFIG_FILE", env_path), patch(
+ "lib.cookie_extract.extract_cookies_with_source",
+ side_effect=AssertionError("browser read despite FROM_BROWSER=off"),
+ ), patch.object(setup_wizard, "_install_digg_cli", return_value=(False, "no_npx", "", None)), patch.object(
+ setup_wizard, "install_default_pp_sources", return_value={}
+ ), patch("shutil.which", return_value=None):
+ assert cli.main() == 0
+ content = env_path.read_text()
+ assert "LAST30DAYS_X_COOKIE_ACCESS_DENIED=edge" in content
+ assert "BROWSER_CONSENT=true" in content
+
def test_setup_detected_as_topic(self, capsys):
with patch.object(sys, "argv", ["last30days", "setup"]), patch.object(
cli.env, "get_config", return_value={}