The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): a signed-out /api/status on the Webapp shows no profile names or install id
Tekniumfd9ff2a0cdcb
diff --git a/hermes_cli/dashboard_auth/middleware.py b/hermes_cli/dashboard_auth/middleware.pyindex 654eb041cd0..ba3c9fab268 100644--- a/hermes_cli/dashboard_auth/middleware.py+++ b/hermes_cli/dashboard_auth/middleware.py@@ -154,6 +154,29 @@ def _verify_access_token(phase="verify" if audit else "bearer verify", log=_log, on_unreachable=_audit_unreachable)+def peek_session(request: Request):+ """The verified Session a request to a PUBLIC path carries, or ``None``, with the gate's own+ checks (Webapp sign-in binding, bearers only a native app was given) and none of its side+ effects: no refresh, no cookie writes, no audit records, no 401. A public route uses it to+ decide how much to show; an expired or unverifiable session reads as signed out."""+ bearer = _extract_bearer(request)+ if bearer:+ if not webapp_sign_in.token_works_alone(request, bearer):+ return None+ access_token, provider_hint = bearer, None+ else:+ gateway = webapp_sign_in.cookie_gateway(request)+ access_token, _rt = read_session_cookies(request, gateway)+ if not access_token or (webapp_sign_in.applies(request) and not webapp_sign_in.check(request)):+ return None+ provider_hint = read_session_provider(request, gateway)+ try:+ return _verify_access_token(+ request, access_token=access_token, provider_hint=provider_hint, audit=False)+ except ProviderError:+ return None++async def _serve_refreshed(request: Request, call_next, new_session, provider: str) -> Response:"""Serve the request under a just-rotated session and write the rotated cookies back. Writingthe ROTATED RT is mandatory: Portal runs reuse detection, so replaying the stale RT woulddiff --git a/hermes_cli/web_routers/status.py b/hermes_cli/web_routers/status.pyindex 833da73b66a..bef45c8d755 100644--- a/hermes_cli/web_routers/status.py+++ b/hermes_cli/web_routers/status.py@@ -497,8 +497,37 @@ def _fts_rebuild_status() -> Optional[dict[str, Any]]:_sdb.close()+#: What the gateway's Webapp answers a caller who has not signed in: liveness, version and the+#: sign-in shape (the native-app login probe and uptime checks read nothing else). Profile names,+#: the install id and host figures wait for sign-in, since a Webapp on a wildcard bind is reachable+#: by everyone on the network. The standalone dashboard keeps its public body: Hermes Cloud reads+#: the profile list there without a session (#60585).+_WEBAPP_SIGNED_OUT_STATUS_KEYS = frozenset({+ "version", "release_date", "gateway_running", "gateway_state", "overall", "components",+ "auth_required", "auth_providers", "auth_flows"})+++async def _webapp_caller_signed_out(request: Request) -> bool:+ """True when this request reached the gateway's Webapp behind its sign-in gate with no+ session the gate would accept. ``/api/status`` is public, so the gate never verified one."""+ from hermes_cli.web_server import _require_token+ from hermes_cli.web_server_surface import surface+ if request is None or surface(request.app.state) != "webapp" \+ or not getattr(request.app.state, "auth_required", False):+ return False+ if getattr(request.state, "session", None) is None:+ from hermes_cli.dashboard_auth.middleware import peek_session+ request.state.session = await run_in_threadpool(peek_session, request)+ try:+ _require_token(request)+ except HTTPException:+ return True+ return False++@router.get("/api/status")-async def get_status(profile: Optional[str] = None):+async def get_status(profile: Optional[str] = None,+ request: Request = None): # type: ignore[assignment] # None when awaited in-process"""Public machine-level liveness probe (``PUBLIC_API_PATHS``): version, gateway state,active session count and the auth-gate shape — no bodies, no session content, no secrets.@@ -507,8 +536,10 @@ async def get_status(profile: Optional[str] = None):process-global skills-module attributes a concurrent request would cross-restore."""from hermes_cli.web_server import _GATEWAY_HEALTH_URL+ signed_out = await _webapp_caller_signed_out(request)status_scope = None- requested_profile = (profile or "").strip()+ # A signed-out caller's ``?profile=`` is ignored: an unknown name's 404 would tell it which exist.+ requested_profile = "" if signed_out else (profile or "").strip()profile_dir: Optional[Path] = Noneif requested_profile and requested_profile.lower() != "current":profile_dir = _resolve_profile_dir(requested_profile)@@ -594,6 +625,8 @@ async def get_status(profile: Optional[str] = None):"env_path": str(get_env_path()), "gateway_pid": gateway["gateway_pid"],"gateway_health_url": _GATEWAY_HEALTH_URL, "gateways": topology["gateways"]})+ if signed_out:+ return {key: value for key, value in status.items() if key in _WEBAPP_SIGNED_OUT_STATUS_KEYS}return statusfinally:if status_scope is not None:diff --git a/tests/hermes_cli/test_webapp_status_signed_out.py b/tests/hermes_cli/test_webapp_status_signed_out.pynew file mode 100644index 00000000000..483cc402536--- /dev/null+++ b/tests/hermes_cli/test_webapp_status_signed_out.py@@ -0,0 +1,92 @@+"""The gateway's Webapp answers a signed-out ``/api/status`` with liveness and the sign-in shape only.++``/api/status`` is public (``PUBLIC_API_PATHS``): the native-app login probe and uptime checks read+it without a session. A Webapp on a wildcard bind is reachable by everyone on the network, so its+profile names, install id and host figures wait for sign-in. The standalone dashboard keeps its+public body (Hermes Cloud lists profiles from it without a session, #60585).+"""+from __future__ import annotations++import pytest+from fastapi.testclient import TestClient++import hermes_cli.web_server_gateway as _web_server_gateway+import plugins.dashboard_auth.basic as basic_plugin+from hermes_cli import web_server+from hermes_cli.dashboard_auth import clear_providers, register_provider+from hermes_cli.dashboard_auth.routes import _reset_password_rate_limit++LAN = "http://192.168.50.220:9130"+IDENTITY = ("profiles", "parked_profiles", "gateway_shared_with", "install_id", "memory", "disk",+ "nous_session_valid", "active_sessions", "gateway_mode")++[email protected](scope="module")+def password_hash():+ return basic_plugin.hash_password("hunter2")++[email protected](params=["dashboard", "webapp"])+def wildcard_gate(request, password_hash, monkeypatch):+ """An all-interfaces listener behind the bundled username/password sign-in: the standalone+ dashboard or the gateway's Webapp, with two profiles installed."""+ monkeypatch.delenv("HERMES_DASHBOARD_PUBLIC_URL", raising=False)+ for key, value in (("bound_host", "0.0.0.0"), ("bound_port", 9130), ("auth_required", True),+ ("trusted_public_hosts", frozenset())):+ monkeypatch.setattr(web_server.app.state, key, value, raising=False)+ for key in ("ui_surface", "listener_public_url", "webapp_home"):+ if hasattr(web_server.app.state, key):+ monkeypatch.delattr(web_server.app.state, key)+ if request.param == "webapp":+ monkeypatch.setattr(web_server.app.state, "ui_surface", "webapp", raising=False)+ monkeypatch.setattr(web_server.app.state, "listener_public_url", "", raising=False)+ monkeypatch.setattr(_web_server_gateway, "_collect_profile_gateway_topology_cached", lambda: {+ "profiles": ["default", "coder"], "parked_profiles": [], "gateway_mode": "multiplex",+ "gateways": []})+ clear_providers()+ register_provider(basic_plugin.BasicAuthProvider(+ username="admin", password_hash=password_hash, secret=b"s" * 32))+ _reset_password_rate_limit()+ yield request.param+ clear_providers()+++def test_signed_out_webapp_status_carries_no_profile_or_install_identity(wildcard_gate):+ client = TestClient(web_server.app, base_url=LAN)+ signed_out = client.get("/api/status")+ assert signed_out.status_code == 200+ body = signed_out.json()+ # What the native-app login probe and uptime checks read survives on every surface.+ assert body["auth_required"] is True and body["auth_providers"] == ["basic"]+ assert body["gateway_running"] in (True, False) and body["overall"] in ("ok", "degraded")+ assert body["version"]+ if wildcard_gate == "dashboard":+ assert body["profiles"] == ["default", "coder"] # Hermes Cloud's contract (#60585)+ return+ assert [key for key in IDENTITY if key in body] == []+ # ``?profile=`` is no profile-name oracle either: an unknown name answers like a real one.+ assert client.get("/api/status?profile=nope").status_code == 200+ assert "profiles" not in client.get("/api/status?profile=coder").json()++ login = client.post("/auth/password-login",+ json={"provider": "basic", "username": "admin", "password": "hunter2"})+ assert login.status_code == 200, login.text+ signed_in = client.get("/api/status").json()+ assert signed_in["profiles"] == ["default", "coder"]+ assert signed_in["install_id"]+ assert client.get("/api/status?profile=nope").status_code == 404+++def test_a_cookie_the_gate_would_refuse_reads_as_signed_out_on_the_webapp(wildcard_gate):+ if wildcard_gate != "webapp":+ pytest.skip("the dashboard's public body does not depend on the caller")+ client = TestClient(web_server.app, base_url=LAN)+ login = client.post("/auth/password-login",+ json={"provider": "basic", "username": "admin", "password": "hunter2"})+ assert login.status_code == 200, login.text+ forged = TestClient(web_server.app, base_url=LAN)+ for cookie in client.cookies.jar:+ value = (cookie.value or "") if "signin" in cookie.name else "forged"+ forged.cookies.set(cookie.name, value, domain=cookie.domain, path=cookie.path)+ assert "profiles" not in forged.get("/api/status").json()+ assert "profiles" not in client.get("/api/status", headers={"Authorization": "Bearer forged"}).json()