The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(security): secure HERMES_HOME when only an ancestor path is a symlink
KeyArgo56d2438a45fa
diff --git a/hermes_cli/config_home.py b/hermes_cli/config_home.pyindex 4caaa11d242..a08a85afc3b 100644--- a/hermes_cli/config_home.py+++ b/hermes_cli/config_home.py@@ -12,7 +12,19 @@ def _directory_links(path: Path) -> list[Path]:return [part for part in (*reversed(path.parents), path) if part.is_symlink()]-def _ensure_directory(path: Path, *, create: bool, secure: bool) -> None:+def _operator_owned_links(links: list[Path], home: Path) -> list[Path]:+ """Keep only the links that make the operator the owner of the home's permissions.++ That boundary is the home itself and anything under it. A link *above* the home+ (macOS ``/tmp`` -> ``/private/tmp``, ``/var`` -> ``/private/var``, or any aliased parent+ the user happens to live in) says nothing about who owns :data:`home`, and treating it as+ an operator-owned link left a fresh home and its ``cron``/``sessions``/``logs``/``memories``+ subdirectories at the default ``0o755`` instead of ``0o700``.+ """+ return [link for link in links if link == home or home in link.parents]+++def _ensure_directory(path: Path, *, create: bool, secure: bool, home: Path) -> None:from hermes_cli.config import _secure_dirdetail = ""@@ -28,7 +40,7 @@ def _ensure_directory(path: Path, *, create: bool, secure: bool) -> None:elif not path.is_dir():raise FileNotFoundError(f"Required directory does not exist: {path}")# The operator owns permissions beyond a link, including logs/curator.- if secure and not links:+ if secure and not _operator_owned_links(links, home):_secure_dir(path)except OSError as exc:raise HomeInitializationError(@@ -46,12 +58,12 @@ def initialize_home(home: Path, subdirs: tuple[str, ...], ensured: set[str]) ->managed = is_managed()old_umask = os.umask(0o007) if managed else Nonetry:- _ensure_directory(home, create=not managed, secure=not managed)+ _ensure_directory(home, create=not managed, secure=not managed, home=home)required = ("cron", "sessions", "logs", "memories") if managed else subdirsfor subdir in required:- _ensure_directory(home / subdir, create=not managed, secure=not managed)+ _ensure_directory(home / subdir, create=not managed, secure=not managed, home=home)if managed:- _ensure_directory(home / "logs" / "curator", create=True, secure=False)+ _ensure_directory(home / "logs" / "curator", create=True, secure=False, home=home)try:_ensure_default_soul_md(home)except OSError as exc:diff --git a/tests/hermes_cli/test_home_directory_diagnostics.py b/tests/hermes_cli/test_home_directory_diagnostics.pyindex 6483935be67..cb26ee4d11a 100644--- a/tests/hermes_cli/test_home_directory_diagnostics.py+++ b/tests/hermes_cli/test_home_directory_diagnostics.py@@ -1,4 +1,5 @@"""Home initialization must respect operator-owned links and diagnose storage."""+import statfrom pathlib import Pathimport pytest@@ -64,3 +65,88 @@ def test_initialization_preserves_external_directory_modes(tmp_path, monkeypatchassert curator.stat().st_mode & 0o777 == 0o750else:assert (home / "logs").stat().st_mode & 0o777 == 0o700++[email protected]_only[email protected]("existing", (False, True))+def test_symlinked_parent_above_home_is_not_an_operator_home_link(+ tmp_path, monkeypatch, existing+):+ """A link above HERMES_HOME is not an operator-owned home link.++ macOS aliases ``/tmp`` -> ``/private/tmp`` and ``/var`` -> ``/private/var``, so a home+ under the default temp root arrives with a symlinked parent; the same happens for any+ user whose own directory is aliased. The home and its subdirectories are still ours to+ secure: they must end up 0o700, both when created fresh and when a previous run left them+ at the 0o755 default.+ """+ real_root = tmp_path / "real"+ real_root.mkdir()+ alias = tmp_path / "alias"+ alias.symlink_to(real_root, target_is_directory=True)+ home = alias / "hermes"+ if existing:+ home.mkdir(parents=True, mode=0o755)+ for name in config._HERMES_HOME_SUBDIRS:+ (home / name).mkdir(mode=0o755)+ assert stat.S_IMODE(home.stat().st_mode) == 0o755++ monkeypatch.setattr(config, "get_hermes_home", lambda: home)+ monkeypatch.setattr(config, "is_managed", lambda: False)+ config._HERMES_HOME_ENSURED.discard(str(home))+ config._HERMES_HOME_ENSURED.discard(str(home.resolve()))++ config.ensure_hermes_home()++ assert stat.S_IMODE(home.stat().st_mode) == 0o700+ for name in config._HERMES_HOME_SUBDIRS:+ mode = stat.S_IMODE((home / name).stat().st_mode)+ assert mode == 0o700, f"{name} should be 0700, got 0o{mode:o}"++[email protected]_only+def test_aliased_parent_still_leaves_an_operator_home_link_alone(tmp_path, monkeypatch):+ """An operator-owned link at the home boundary keeps owning the mode, aliased parent or not."""+ real_root = tmp_path / "real"+ real_root.mkdir()+ alias = tmp_path / "alias"+ alias.symlink_to(real_root, target_is_directory=True)+ shared = real_root / "shared"+ shared.mkdir(mode=0o750)+ (shared / "curator").mkdir(mode=0o750)+ home = alias / "hermes"+ home.symlink_to(shared, target_is_directory=True)++ monkeypatch.setattr(config, "get_hermes_home", lambda: home)+ monkeypatch.setattr(config, "is_managed", lambda: False)+ config._HERMES_HOME_ENSURED.discard(str(home))+ config._HERMES_HOME_ENSURED.discard(str(home.resolve()))++ config.ensure_hermes_home()++ assert home.is_symlink() and home.readlink() == shared+ assert stat.S_IMODE(shared.stat().st_mode) == 0o750+ assert stat.S_IMODE((shared / "curator").stat().st_mode) == 0o750++[email protected]_only+def test_unavailable_symlinked_parent_is_still_diagnosed(tmp_path, monkeypatch):+ """A link above the home with a missing target is still refused instead of materialized."""+ from hermes_cli.config_home import HomeInitializationError++ real_root = tmp_path / "real"+ real_root.mkdir()+ missing = tmp_path / "unmounted"+ alias = real_root / "alias"+ alias.symlink_to(missing, target_is_directory=True)+ home = alias / "hermes"++ monkeypatch.setattr(config, "get_hermes_home", lambda: home)+ monkeypatch.setattr(config, "is_managed", lambda: False)+ config._HERMES_HOME_ENSURED.discard(str(home))++ with pytest.raises(HomeInitializationError):+ config.ensure_hermes_home()++ assert alias.is_symlink() and alias.readlink() == missing+ assert not missing.exists()