The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(terminal): scope environment cache by session key to prevent cross-profile SSH leakage
carryzuo00a270c4adeab6
diff --git a/tests/tools/test_shared_container_task_id.py b/tests/tools/test_shared_container_task_id.pyindex 614b868c42e..0354cf02903 100644--- a/tests/tools/test_shared_container_task_id.py+++ b/tests/tools/test_shared_container_task_id.py@@ -67,3 +67,51 @@ def test_env_type_override_keeps_own_id():)finally:terminal_tool.clear_task_env_overrides("bench-env")+++# --- Cross-profile SSH-leak isolation (commit e00f940a9, re-applied) ---------+#+# When a session key is present (WebUI/gateway), each session must own its own+# slot in _active_environments so switching from profile A (ssh_host=10.0.0.1)+# to profile B (ssh_host=10.0.0.2) cannot reuse A's SSHEnvironment. Without this+# the shared "default" slot silently runs commands on the wrong remote host.+++def test_session_key_scopes_to_its_own_slot(monkeypatch):+ monkeypatch.setenv("HERMES_SESSION_KEY", "sess-A")+ assert terminal_tool._resolve_container_task_id(None) == "session:sess-A"+++def test_distinct_session_keys_get_distinct_slots(monkeypatch):+ monkeypatch.setenv("HERMES_SESSION_KEY", "sess-A")+ a = terminal_tool._resolve_container_task_id(None)+ monkeypatch.setenv("HERMES_SESSION_KEY", "sess-B")+ b = terminal_tool._resolve_container_task_id(None)+ assert a == "session:sess-A"+ assert b == "session:sess-B"+ assert a != b+++def test_subagent_collapses_onto_parent_session(monkeypatch):+ # Subagents inherit the parent's session key, so they share the parent's+ # container (the #16177 intent) rather than a global "default".+ monkeypatch.setenv("HERMES_SESSION_KEY", "sess-A")+ assert (+ terminal_tool._resolve_container_task_id("subagent-3-cafef00d")+ == "session:sess-A"+ )+++def test_rl_override_wins_over_session_key(monkeypatch):+ monkeypatch.setenv("HERMES_SESSION_KEY", "sess-A")+ terminal_tool.register_task_env_overrides("tb2-z", {"docker_image": "z:1"})+ try:+ assert terminal_tool._resolve_container_task_id("tb2-z") == "tb2-z"+ finally:+ terminal_tool.clear_task_env_overrides("tb2-z")+++def test_no_session_key_still_defaults(monkeypatch):+ # CLI mode: no session key -> unchanged "default" behaviour.+ monkeypatch.delenv("HERMES_SESSION_KEY", raising=False)+ assert terminal_tool._resolve_container_task_id(None) == "default"diff --git a/tools/terminal_tool.py b/tools/terminal_tool.pyindex 0d90fe55922..9f4c2375416 100644--- a/tools/terminal_tool.py+++ b/tools/terminal_tool.py@@ -1386,6 +1386,26 @@ def _resolve_container_task_id(task_id: Optional[str]) -> str:return task_idif task_id and _docker_session_isolation_enabled():return _resolve_container_alias(task_id)+ # Per-session isolation: when a session key is present (the WebUI streaming+ # layer sets it per-session, the gateway per-message via contextvars), scope+ # the container to it so switching profiles can't reuse a previous profile's+ # SSHEnvironment and silently run commands on the wrong remote host. Subagents+ # inherit the same session key, so they still collapse onto the parent's+ # container (the #16177 shared-container intent). CLI mode has no session key+ # and falls through to "default", behaviour unchanged. See commit e00f940a9.+ #+ # This runs *after* the isolation-override and docker/container_persistent+ # branches above: those paths already key containers per task_id, so they+ # stay authoritative where they apply and this only covers the cases that+ # would otherwise collapse to the shared "default" key (notably SSH).+ try:+ from gateway.session_context import get_session_env++ session_key = get_session_env("HERMES_SESSION_KEY", "")+ except Exception:+ session_key = os.getenv("HERMES_SESSION_KEY", "")+ if session_key:+ return f"session:{session_key}"return "default"