Skip to content

The agent that grows with you

security(gateway): re-resolve hooks directory per call to fix profile isolation

srojk340c74353c867f
diff --git a/gateway/hooks.py b/gateway/hooks.py
index c0a84aa67aa..34d8b9b8176 100644
--- a/gateway/hooks.py
+++ b/gateway/hooks.py
@@ -21,6 +21,17 @@ from hermes_cli.config import get_hermes_home
HOOKS_DIR = get_hermes_home() / "hooks"
+_HOOKS_DIR_AT_IMPORT = HOOKS_DIR
+
+
+def _resolve_hooks_dir() -> Path:
+ """Active profile's hooks dir at call time: the patched ``HOOKS_DIR`` when a test changed it,
+ else ``get_hermes_home()/hooks``. The import-time constant is the LAUNCH profile's; under
+ ``gateway.multiplex_profiles`` every served profile has its own ``hooks/``, and a registry
+ loaded from the launch home would run the default profile's handlers (arbitrary Python) on
+ every other profile's messages, responses and user ids."""
+ configured = Path(HOOKS_DIR)
+ return configured if configured != _HOOKS_DIR_AT_IMPORT else get_hermes_home() / "hooks"
def _skip(name: str, reason: str) -> None:
@@ -74,11 +85,12 @@ class HookRegistry:
"""Extension point for always-on built-in hooks; currently none shipped."""
def discover_and_load(self) -> None:
- """Register built-in hooks, then load every valid hook dir under HOOKS_DIR."""
+ """Register built-in hooks, then load every valid hook dir under the active profile's ``hooks/``."""
self._register_builtin_hooks()
- if not HOOKS_DIR.exists():
+ hooks_dir = _resolve_hooks_dir()
+ if not hooks_dir.exists():
return
- for hook_dir in sorted(HOOKS_DIR.iterdir()):
+ for hook_dir in sorted(hooks_dir.iterdir()):
if not hook_dir.is_dir():
continue
try:
diff --git a/tests/test_profile_isolation_runtime.py b/tests/test_profile_isolation_runtime.py
index ffa75d943f2..5e5417b2faa 100644
--- a/tests/test_profile_isolation_runtime.py
+++ b/tests/test_profile_isolation_runtime.py
@@ -111,6 +111,52 @@ class TestRichSentStorePathResolution:
assert b_seen.endswith("state/rich_sent_index.json")
+class TestGatewayHooksDirResolution:
+ """gateway/hooks.py's HookRegistry must discover hooks from the active
+ profile's directory — otherwise one profile's HookRegistry loads and
+ executes a DIFFERENT profile's hook handlers (arbitrary Python) against
+ its own live event context under the multiplexed gateway."""
+
+ def test_hooks_dir_follows_override(self, two_profiles):
+ prof_a, prof_b = two_profiles
+ import gateway.hooks as gh
+
+ a_seen = _under_override(prof_a, lambda: gh._resolve_hooks_dir())
+ b_seen = _under_override(prof_b, lambda: gh._resolve_hooks_dir())
+
+ assert a_seen == prof_a / "hooks"
+ assert b_seen == prof_b / "hooks"
+ assert a_seen != b_seen
+
+ def test_discover_and_load_uses_active_profile_hooks_dir(self, two_profiles):
+ """End-to-end: a hook that only exists under profile B's hooks dir
+ must not be discovered when profile A's override is active, and vice
+ versa — proving the registry doesn't fall back to a frozen profile."""
+ prof_a, prof_b = two_profiles
+ import gateway.hooks as gh
+
+ b_hook_dir = prof_b / "hooks" / "only-in-b"
+ b_hook_dir.mkdir(parents=True)
+ (b_hook_dir / "HOOK.yaml").write_text(
+ "name: only-in-b\nevents: [\"agent:start\"]\n", encoding="utf-8",
+ )
+ (b_hook_dir / "handler.py").write_text(
+ "async def handle(event_type, context):\n pass\n", encoding="utf-8",
+ )
+
+ def _load_and_names():
+ reg = gh.HookRegistry()
+ reg.discover_and_load()
+ return [h["name"] for h in reg.loaded_hooks]
+
+ a_hooks = _under_override(prof_a, _load_and_names)
+ b_hooks = _under_override(prof_b, _load_and_names)
+
+ assert "only-in-b" not in a_hooks
+ assert "only-in-b" in b_hooks
+
+
+
# ---------------------------------------------------------------------------
# M2 — thread / executor context propagation
# ---------------------------------------------------------------------------