Skip to content

The agent that grows with you

fix(gateway): consult the installed unit only when root

kshitijk4poor1ecdfd18db97
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 1b3df77395f..e8cf5415fb2 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -1977,19 +1977,22 @@ def _bare_unit_pinned_home() -> Path | None:
after it. The installed unit is the only basis that holds still, and it holds for every elevated
identity — ``sudo -i`` and cron included, where SUDO_USER is absent.
- Linux-gated because a systemd unit is not an identity authority for launchd labels, Windows
- scheduled tasks, or s6 slots, which share ``_profile_suffix()``. ``is_linux()`` is a plain
- ``sys.platform`` test; ``supports_systemd_services()`` would be wrong here, since it can shell out
- to ``systemctl is-system-running`` on WSL/containers and this runs on every name resolution.
+ Linux- and root-gated: a systemd unit is not an identity authority for launchd labels, Windows
+ scheduled tasks, or s6 slots, which share ``_profile_suffix()``, and only an elevated process ever
+ operates the system unit — an unprivileged user-scope command must keep naming its own units, or a
+ bare system unit pinning ``profiles/<name>`` would alias that profile onto the user's default unit.
+ ``is_linux()`` is a plain ``sys.platform`` test; ``supports_systemd_services()`` would be wrong here,
+ since it can shell out to ``systemctl is-system-running`` on WSL/containers and this runs on every
+ name resolution.
"""
- if not is_linux():
+ if not is_linux() or os.geteuid() != 0: # windows-footgun: ok — behind is_linux()
return None
pinned = _hermes_home_pinned_by_unit(_SYSTEM_UNIT_DIR / f"{_SERVICE_BASE}.service")
if not pinned:
return None
try:
return Path(pinned).expanduser().resolve()
- except (OSError, RuntimeError, ValueError):
+ except (RuntimeError, ValueError): # hand-edited unit: ``~nouser`` or an embedded NUL
return None
@@ -2288,7 +2291,7 @@ _LEGACY_UNIT_EXECSTART_MARKERS: tuple[str, ...] = (
def _legacy_unit_search_paths() -> list[tuple[bool, Path]]:
"""``[(is_system, base_dir), ...]`` to scan for legacy units; factored out so tests can monkeypatch."""
- return [(False, Path.home() / ".config" / "systemd" / "user"), (True, Path("/etc/systemd/system"))]
+ return [(False, Path.home() / ".config" / "systemd" / "user"), (True, _SYSTEM_UNIT_DIR)]
def _find_legacy_hermes_units() -> list[tuple[str, Path, bool]]:
diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py
index 8345b5e4dfa..6bd31ccf4c6 100644
--- a/tests/hermes_cli/test_gateway_service.py
+++ b/tests/hermes_cli/test_gateway_service.py
@@ -2698,8 +2698,9 @@ class TestUnitAnchoredServiceIdentity:
"""The installed ``hermes-gateway.service`` owns the bare name: under ``sudo`` the naming basis moves
mid-command when ``_sync_hermes_home_from_systemd_unit()`` adopts the unit's HERMES_HOME (#108674).
- ``linux_only`` because ``_bare_unit_pinned_home()`` is Linux-gated on purpose: a systemd unit is not
- an identity authority for launchd labels, Windows tasks, or s6 slots, which share the same resolver.
+ ``linux_only`` because ``_bare_unit_pinned_home()`` is Linux- and root-gated on purpose: a systemd unit
+ is not an identity authority for launchd labels, Windows tasks, or s6 slots, which share the same
+ resolver, and only an elevated process operates the system unit.
"""
@pytest.mark.linux_only
@@ -2722,6 +2723,23 @@ class TestUnitAnchoredServiceIdentity:
assert name != gateway_cli._SERVICE_BASE
assert name.startswith(gateway_cli._SERVICE_BASE + "-")
+ @pytest.mark.linux_only
+ def test_unprivileged_profile_command_ignores_the_system_unit(self, tmp_path, monkeypatch):
+ """A bare system unit pinning ``profiles/<name>`` must not alias that profile onto the user's
+ default unit when an unprivileged user-scope command resolves the name."""
+ profile_home = tmp_path / "alice" / ".hermes" / "profiles" / "kimi"
+ profile_home.mkdir(parents=True)
+ unit_dir = tmp_path / "systemd"
+ unit_dir.mkdir()
+ (unit_dir / f"{gateway_cli._SERVICE_BASE}.service").write_text(
+ f'[Service]\nEnvironment="HERMES_HOME={profile_home}"\n', encoding="utf-8"
+ )
+ monkeypatch.setattr(gateway_cli, "_SYSTEM_UNIT_DIR", unit_dir, raising=False)
+ monkeypatch.setattr(Path, "home", lambda: tmp_path / "alice")
+ monkeypatch.setattr(os, "geteuid", lambda: 1000)
+ monkeypatch.setenv("HERMES_HOME", str(profile_home))
+ assert gateway_cli.get_service_name() == "hermes-gateway-kimi"
+
@pytest.mark.linux_only
def test_bare_unit_pinning_a_named_profile_home_keeps_the_bare_name(self, tmp_path, monkeypatch):
"""``sudo ... install --system`` names the unit from root's default but pins the invoking user's
@@ -2737,6 +2755,7 @@ class TestUnitAnchoredServiceIdentity:
unit_path = unit_dir / f"{gateway_cli._SERVICE_BASE}.service"
unit_path.write_text(f'[Service]\nEnvironment="HERMES_HOME={profile_home}"\n', encoding="utf-8")
monkeypatch.setattr(gateway_cli, "_SYSTEM_UNIT_DIR", unit_dir, raising=False)
+ monkeypatch.setattr(os, "geteuid", lambda: 0)
monkeypatch.setattr(hermes_constants, "_get_platform_default_hermes_home", lambda: root_home)
monkeypatch.setenv("HERMES_HOME", str(profile_home))
assert gateway_cli.get_service_name() == gateway_cli._SERVICE_BASE
@@ -2758,6 +2777,7 @@ class TestUnitAnchoredServiceIdentity:
f'[Service]\nEnvironment="HERMES_HOME={alice_home}"\n', encoding="utf-8"
)
monkeypatch.setattr(gateway_cli, "_SYSTEM_UNIT_DIR", unit_dir, raising=False)
+ monkeypatch.setattr(os, "geteuid", lambda: 0)
monkeypatch.setattr(hermes_constants, "_get_platform_default_hermes_home", lambda: root_home)
monkeypatch.delenv("HERMES_HOME", raising=False)