Skip to content

The agent that grows with you

fix(gateway): block user unit install in containers

KoNit-K8386ef24a00f
diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py
index 3026d5012a5..649ae0334ad 100644
--- a/hermes_cli/gateway.py
+++ b/hermes_cli/gateway.py
@@ -6235,6 +6235,17 @@ def _cmd_install(args):
_no_backend_exit("install", "termux")
backend = _service_backend()
if backend == "systemd":
+ if is_container() and not system:
+ print_error(
+ "Refusing to install a user-scope systemd gateway service inside a container."
+ )
+ _print_info_lines(
+ "A bind-mounted home can make that unit visible to the host user manager.",
+ "If systemd manages this container, install an isolated system service instead:",
+ " sudo hermes gateway install --system",
+ "Otherwise run the gateway under the container runtime's supervisor.",
+ )
+ sys.exit(1)
_install_systemd_from_cli(args, force=force, system=system, run_as_user=run_as_user)
elif backend == "launchd":
launchd_install(force)
diff --git a/tests/hermes_cli/test_gateway_service.py b/tests/hermes_cli/test_gateway_service.py
index b9fce170de9..f7412fab82b 100644
--- a/tests/hermes_cli/test_gateway_service.py
+++ b/tests/hermes_cli/test_gateway_service.py
@@ -1865,6 +1865,45 @@ class TestDockerAwareGateway:
assert "Docker" in out or "docker" in out
assert "restart" in out.lower()
+ def test_install_in_systemd_container_refuses_user_scope(self, monkeypatch, capsys):
+ """A bind-mounted home must not receive a host-visible user unit."""
+ monkeypatch.setattr(gateway_cli, "is_managed", lambda: False)
+ monkeypatch.setattr(gateway_cli, "is_termux", lambda: False)
+ monkeypatch.setattr(gateway_cli, "supports_systemd_services", lambda: True)
+ monkeypatch.setattr(gateway_cli, "is_container", lambda: True)
+ monkeypatch.setattr(
+ gateway_cli,
+ "_install_systemd_from_cli",
+ lambda *args, **kwargs: pytest.fail("must not install a user unit in a container"),
+ )
+
+ args = SimpleNamespace(gateway_command="install", force=False, system=False, run_as_user=None)
+ with pytest.raises(SystemExit) as exc_info:
+ gateway_cli.gateway_command(args)
+
+ assert exc_info.value.code == 1
+ out = capsys.readouterr().out
+ assert "--system" in out
+ assert "user-scope" in out
+
+ def test_install_in_systemd_container_keeps_explicit_system_scope(self, monkeypatch):
+ """Explicit system installs stay available for systemd-managed containers."""
+ monkeypatch.setattr(gateway_cli, "is_managed", lambda: False)
+ monkeypatch.setattr(gateway_cli, "is_termux", lambda: False)
+ monkeypatch.setattr(gateway_cli, "supports_systemd_services", lambda: True)
+ monkeypatch.setattr(gateway_cli, "is_container", lambda: True)
+ calls = []
+ monkeypatch.setattr(
+ gateway_cli,
+ "_install_systemd_from_cli",
+ lambda *args, **kwargs: calls.append(kwargs),
+ )
+
+ args = SimpleNamespace(gateway_command="install", force=False, system=True, run_as_user=None)
+ gateway_cli.gateway_command(args)
+
+ assert calls == [{"force": False, "system": True, "run_as_user": None}]
+
class TestLegacyHermesUnitDetection:
"""Tests for _find_legacy_hermes_units / has_legacy_hermes_units.