The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): plugin API routes run in the request profile's scope (#120310, salvage #120332)
PRATHAMESH755c6ff927f5c1
diff --git a/hermes_cli/web_server_dashboard.py b/hermes_cli/web_server_dashboard.pyindex 7d988a42577..ae014fc5383 100644--- a/hermes_cli/web_server_dashboard.py+++ b/hermes_cli/web_server_dashboard.py@@ -9,7 +9,7 @@ import sysimport threadingimport timeimport hermes_yaml as yaml-from fastapi import FastAPI, Request+from fastapi import Depends, FastAPI, Requestfrom fastapi.responses import FileResponse, HTMLResponse, JSONResponse, Responsefrom fastapi.staticfiles import StaticFilesfrom pathlib import Path@@ -788,6 +788,20 @@ def _plugin_api_mount_skip_reason(plugin: Dict[str, Any], enabled_set: set, disareturn None+async def _plugin_route_secret_scope(profile: Optional[str] = None):+ """Home + secret scope for one ``/api/plugins/<name>/`` request: the launch profile's, or the+ ``?profile=``-requested one — the same ``_config_profile_scope`` seam the built-in routers use.+ Without it plugin handlers ran unscoped, so under multi-profile hosting every ``get_secret``+ / ``resolve_runtime_provider`` failed closed with ``UnscopedSecretError`` (#120310; the bundled+ kanban plugin's Decompose / Specify / Estimate aux-LLM calls, #123372). ``async`` on purpose:+ a sync yield-dependency's setup and teardown run on different threadpool threads, so the+ scope token would be reset in a foreign context; sync handlers still see the scope because+ ``run_in_threadpool`` copies the request context into the worker."""+ from hermes_cli.web_server_profiles import _config_profile_scope+ with _config_profile_scope(profile):+ yield++def _mount_plugin_api_routes():"""Import and mount backend API routes from plugins that declare them.@@ -861,7 +875,11 @@ def _mount_plugin_api_routes():if router is None:_log.warning("Plugin %s api file has no 'router' attribute", plugin["name"])continue- app.include_router(router, prefix=f"/api/plugins/{plugin['name']}")+ app.include_router(+ router,+ prefix=f"/api/plugins/{plugin['name']}",+ dependencies=[Depends(_plugin_route_secret_scope)],+ )_log.info("Mounted plugin API routes: /api/plugins/%s/", plugin["name"])except Exception as exc:_log.warning("Failed to load plugin %s API routes: %s", plugin["name"], exc)diff --git a/tests/fixtures/plugins/example-dashboard/dashboard/plugin_api.py b/tests/fixtures/plugins/example-dashboard/dashboard/plugin_api.pyindex 21c4b05d6a8..2cb209a0235 100644--- a/tests/fixtures/plugins/example-dashboard/dashboard/plugin_api.py+++ b/tests/fixtures/plugins/example-dashboard/dashboard/plugin_api.py@@ -22,3 +22,21 @@ router = APIRouter()async def hello():"""Simple greeting endpoint to demonstrate plugin API routes."""return {"message": "Hello from the example plugin!", "plugin": "example", "version": "1.0.0"}++[email protected]("/whoami")+async def whoami():+ """Side-effect-free credential probe: read a secret and fold any failure into the+ plugin "no data" contract (a plugin handler must never raise out to the client).++ Tests use this to prove the *production* mount path (discovery → import →+ ``_mount_plugin_api_routes`` → scoped handler) resolves the caller's profile+ credentials under multi-profile hosting (#120310). It reads a made-up key, so it+ never touches real credentials or the network.+ """+ from agent.secret_scope import get_secret++ try:+ return {"ok": True, "key": get_secret("EXAMPLE_PLUGIN_PROBE_KEY")}+ except Exception as exc: # plugin contract: never raise out of the handler+ return {"ok": False, "error": type(exc).__name__}diff --git a/tests/hermes_cli/test_web_server.py b/tests/hermes_cli/test_web_server.pyindex 4e14536bb80..a850466550a 100644--- a/tests/hermes_cli/test_web_server.py+++ b/tests/hermes_cli/test_web_server.py@@ -4443,6 +4443,92 @@ class TestPluginAPIAuth:assert resp.status_code == 401+class TestPluginAPISecretScopeProductionMount:+ """#120310: a plugin API handler's ``get_secret()`` must resolve the *requested*+ profile's credentials under multi-profile hosting, verified through the REAL mount+ path — discovery → import → ``_mount_plugin_api_routes()`` (which attaches+ ``_plugin_route_secret_scope``) → a live request against ``app`` — not a hand-built+ ``include_router``. The dedicated ``test_plugin_api_secret_scope.py`` suite proves the+ dependency in isolation; this closes the actual reported surface end-to-end and pins+ that the launch profile and a ``?profile=`` request read distinct secrets, that the+ request profile does not leak back into the launch profile, and that an unknown+ profile is rejected in the dependency before the handler runs.+ """++ _PROBE_KEY = "EXAMPLE_PLUGIN_PROBE_KEY"++ @pytest.fixture(autouse=True)+ def _setup(self, monkeypatch, _isolate_hermes_home, _install_example_plugin):+ try:+ from starlette.testclient import TestClient+ except ImportError:+ pytest.skip("fastapi/starlette not installed")++ import hermes_state+ from hermes_constants import get_hermes_home+ from hermes_cli import profiles+ from hermes_cli.web_server import app, _SESSION_HEADER_NAME, _SESSION_TOKEN++ default_home = get_hermes_home()+ monkeypatch.setattr(hermes_state, "DEFAULT_DB_PATH", default_home / "state.db")++ # Anchor the named-profiles root to the isolated home so ``?profile=workerb``+ # resolves inside the test sandbox (mirrors test_web_server_skills_profiles).+ profiles_root = default_home / "profiles"+ monkeypatch.setattr(profiles, "_get_default_hermes_home", lambda: default_home)+ monkeypatch.setattr(profiles, "_get_profiles_root", lambda: profiles_root)++ # Named profile B: a live profile (``.env`` is both an identity marker and the+ # secret source) with its OWN value for the probe key.+ worker_home = profiles_root / "workerb"+ worker_home.mkdir(parents=True, exist_ok=True)+ (worker_home / ".env").write_text(f"{self._PROBE_KEY}=sk-workerb\n", encoding="utf-8")++ # Launch profile A: an env-only credential (systemd ``Environment=`` style), frozen+ # into the launch scope when multi-profile hosting activates.+ monkeypatch.setenv(self._PROBE_KEY, "sk-launch-a")++ import tui_gateway.launch_profile_policy as lpp+ from agent.secret_scope import is_multiplex_active, set_multiplex_active++ monkeypatch.setattr(lpp, "_snapshot", None) # freeze the launch env fresh+ self._previous_multiplex = is_multiplex_active()+ lpp.activate_multi_profile_hosting() # freezes os.environ + flips multiplex on++ self.client = TestClient(app)+ self.client.headers[_SESSION_HEADER_NAME] = _SESSION_TOKEN+ try:+ yield+ finally:+ set_multiplex_active(self._previous_multiplex)++ def _probe(self, profile=None):+ params = {"profile": profile} if profile is not None else None+ return self.client.get("/api/plugins/example/whoami", params=params)++ def test_launch_and_requested_profile_read_distinct_secrets(self):+ # No ``?profile=`` → the launch profile's frozen env-only credential.+ resp = self._probe()+ assert resp.status_code == 200+ assert resp.json() == {"ok": True, "key": "sk-launch-a"}++ # ``?profile=workerb`` → that profile's own credential, through the real mount.+ resp = self._probe("workerb")+ assert resp.status_code == 200+ assert resp.json() == {"ok": True, "key": "sk-workerb"}++ # Back to the launch profile: the request scope reset, so B never leaks into A.+ resp = self._probe()+ assert resp.status_code == 200+ assert resp.json() == {"ok": True, "key": "sk-launch-a"}++ def test_unknown_profile_rejected_before_handler(self):+ # The dependency raises HTTPException(404) before the handler runs, so this is a+ # 404 — NOT the handler's folded ``{"ok": False}`` no-data contract.+ resp = self._probe("ghost")+ assert resp.status_code == 404++class TestDashboardPluginManifestExtensions:"""Tests for the extended plugin manifest fields (tab.override,tab.hidden, slots) read by _discover_dashboard_plugins()."""