Skip to content

Give your AI agent eyes to see the entire internet. Read & search Twitter, Reddit, YouTube, GitHub, Bilibili, XiaoHongShu — one CLI, zero API fees.

fix(security): honor HOME for credential paths

Pnantb2acd2a339ec
diff --git a/agent_reach/cli.py b/agent_reach/cli.py
index 7e662383..22519749 100644
--- a/agent_reach/cli.py
+++ b/agent_reach/cli.py
@@ -1482,17 +1482,13 @@ def _configure_xhs_cookies(value) -> bool:
docker = shutil.which("docker")
if not docker:
# No Docker - write to a local file for manual import.
- from pathlib import Path
-
from agent_reach.utils.paths import (
PrivatePathError,
atomic_write_private_text,
+ home_dir,
)
- cookie_path = (
- Path(os.path.expanduser("~/.agent-reach"))
- / "xhs-cookies.json"
- )
+ cookie_path = home_dir() / ".agent-reach" / "xhs-cookies.json"
try:
atomic_write_private_text(cookie_path, cookies_json)
except (OSError, PrivatePathError) as exc:
@@ -1600,6 +1596,8 @@ def _cmd_uninstall(args):
import shutil
import subprocess
+ from agent_reach.utils.paths import home_dir
+
dry_run = args.dry_run
keep_config = args.keep_config
@@ -1615,7 +1613,7 @@ def _cmd_uninstall(args):
mcporter_cleanup_skipped = False
# ── 1. Config directory (~/.agent-reach/) ──
- config_dir = os.path.expanduser("~/.agent-reach")
+ config_dir = home_dir() / ".agent-reach"
if not keep_config:
if os.path.isdir(config_dir):
if dry_run:
@@ -1637,8 +1635,8 @@ def _cmd_uninstall(args):
# provenance marker it would be unsafe to delete them automatically, so
# surface every exact path that may still contain Twitter credentials.
legacy_credential_paths = (
- os.path.expanduser("~/.config/xfetch/session.json"),
- os.path.expanduser("~/.config/bird/credentials.env"),
+ home_dir() / ".config" / "xfetch" / "session.json",
+ home_dir() / ".config" / "bird" / "credentials.env",
)
present_legacy_paths = [
path for path in legacy_credential_paths if os.path.lexists(path)
diff --git a/agent_reach/config.py b/agent_reach/config.py
index 879781c1..d6e51853 100644
--- a/agent_reach/config.py
+++ b/agent_reach/config.py
@@ -16,6 +16,7 @@ import yaml
from agent_reach.utils.paths import (
PrivatePathError,
ensure_no_symlink_path,
+ home_dir,
make_private_dir,
read_small_text_no_follow,
)
@@ -98,7 +99,7 @@ def _atomic_write_yaml(target: Path, data: dict) -> None:
class Config:
"""Manages Agent Reach configuration."""
- CONFIG_DIR = Path.home() / ".agent-reach"
+ CONFIG_DIR = home_dir() / ".agent-reach"
CONFIG_FILE = CONFIG_DIR / "config.yaml"
# Feature → required config keys
diff --git a/agent_reach/cookie_extract.py b/agent_reach/cookie_extract.py
index 89564d71..d2f65952 100644
--- a/agent_reach/cookie_extract.py
+++ b/agent_reach/cookie_extract.py
@@ -352,15 +352,15 @@ def _read_xfetch_session(path: Path) -> dict:
def _sync_xfetch_session(auth_token: str, ct0: str) -> bool:
"""Sync Twitter credentials to ~/.config/xfetch/session.json (legacy xreach compat)."""
import json
- import os
try:
from agent_reach.utils.paths import (
atomic_write_private_text,
+ home_dir,
make_private_dir,
)
- xfetch_dir = os.path.join(os.path.expanduser("~"), ".config", "xfetch")
+ xfetch_dir = home_dir() / ".config" / "xfetch"
make_private_dir(xfetch_dir)
session_path = Path(xfetch_dir) / "session.json"
session_data = _read_xfetch_session(session_path)
@@ -384,18 +384,18 @@ def _sync_bird_env(auth_token: str, ct0: str) -> bool:
Values are passed through shlex.quote so a token containing a quote, $, or
backtick cannot break out into shell syntax when the file is sourced.
"""
- import os
import shlex
try:
from agent_reach.utils.paths import (
atomic_write_private_text,
+ home_dir,
make_private_dir,
)
- bird_dir = os.path.join(os.path.expanduser("~"), ".config", "bird")
+ bird_dir = home_dir() / ".config" / "bird"
make_private_dir(bird_dir)
- env_path = os.path.join(bird_dir, "credentials.env")
+ env_path = bird_dir / "credentials.env"
atomic_write_private_text(
env_path,
f"AUTH_TOKEN={shlex.quote(auth_token)}\n"
diff --git a/agent_reach/utils/paths.py b/agent_reach/utils/paths.py
index bf0af68b..fb814ce3 100644
--- a/agent_reach/utils/paths.py
+++ b/agent_reach/utils/paths.py
@@ -14,6 +14,23 @@ class PrivatePathError(RuntimeError):
"""Raised when a private write could be redirected through a symlink."""
+def home_dir() -> Path:
+ """Return the explicit HOME first, including on Windows.
+
+ Windows' ``expanduser("~")`` ignores HOME and resolves USERPROFILE. Agent
+ Reach uses HOME as an isolation boundary in tests, containers and managed
+ agent environments, so credential paths must honor it consistently.
+ """
+ explicit_home = os.environ.get("HOME")
+ if explicit_home:
+ return Path(os.path.abspath(explicit_home))
+
+ expanded = os.path.expanduser("~")
+ if expanded and expanded != "~":
+ return Path(expanded)
+ return Path.home()
+
+
def ensure_no_symlink_path(path: str | Path, label: str = "路径") -> Path:
"""Reject any existing symlink component without resolving the path."""
target = Path(path)
diff --git a/tests/test_private_file_writes.py b/tests/test_private_file_writes.py
index eb6b72ed..ed0c46bc 100644
--- a/tests/test_private_file_writes.py
+++ b/tests/test_private_file_writes.py
@@ -92,6 +92,62 @@ def test_legacy_xfetch_sync_refuses_ancestor_symlink(tmp_path, monkeypatch):
assert config_dir.is_symlink()
+def test_credential_writes_honor_home_when_expanduser_disagrees(
+ tmp_path, monkeypatch
+):
+ """Windows expanduser ignores HOME; private writes must not escape it."""
+ intended_home = tmp_path / "isolated-home"
+ windows_profile = tmp_path / "windows-profile"
+ intended_home.mkdir()
+ windows_profile.mkdir()
+ monkeypatch.setenv("HOME", str(intended_home))
+
+ real_expanduser = os.path.expanduser
+
+ def windows_expanduser(value):
+ if value == "~":
+ return str(windows_profile)
+ if value.startswith("~/"):
+ return str(windows_profile / value[2:])
+ return real_expanduser(value)
+
+ monkeypatch.setattr(os.path, "expanduser", windows_expanduser)
+ monkeypatch.setattr("shutil.which", lambda _name: None)
+
+ assert _sync_xfetch_session("auth", "ct0") is True
+ assert _sync_bird_env("auth", "ct0") is True
+ assert cli._configure_xhs_cookies("web_session=xhs-secret") is True
+
+ assert (intended_home / ".config" / "xfetch" / "session.json").exists()
+ assert (intended_home / ".config" / "bird" / "credentials.env").exists()
+ assert (intended_home / ".agent-reach" / "xhs-cookies.json").exists()
+ assert list(windows_profile.rglob("*")) == []
+
+
+def test_expanduser_fallback_still_refuses_symlinked_profile(
+ tmp_path, monkeypatch
+):
+ """Without HOME, the USERPROFILE fallback remains guarded end to end."""
+ victim_dir = tmp_path / "victim-profile"
+ victim_dir.mkdir()
+ profile_link = tmp_path / "profile-link"
+ try:
+ profile_link.symlink_to(victim_dir, target_is_directory=True)
+ except OSError:
+ pytest.skip("symlinks are not supported on this platform")
+
+ monkeypatch.delenv("HOME", raising=False)
+ monkeypatch.setattr(
+ os.path,
+ "expanduser",
+ lambda value: str(profile_link) if value == "~" else value,
+ )
+
+ assert _sync_xfetch_session("auth", "ct0") is False
+ assert _sync_bird_env("auth", "ct0") is False
+ assert list(victim_dir.rglob("*")) == []
+
+
def test_legacy_xfetch_sync_refuses_oversized_existing_session(
tmp_path, monkeypatch
):