The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): never publish the dashboard session token on the gateway listener
Teknium9112cb59355f
diff --git a/gateway/run_api.py b/gateway/run_api.pyindex 5485b1a4435..c671a1ce1fb 100644--- a/gateway/run_api.py+++ b/gateway/run_api.py@@ -28,6 +28,10 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0) -if getattr(web.app.state, "gateway_runner", None) is not None:raise RuntimeError("gateway API already started")web._configure_auth_gate(host, False, None, None)+ # Local clients authenticate with tickets minted on the owner-only control socket. The SPA+ # session token is a bearer credential any loopback peer (any OS user) could read from+ # GET /, so the gateway-hosted listener never publishes it.+ web.app.state.withhold_session_token = Trueconfig, server = web._build_uvicorn_server(host, port)config.timeout_graceful_shutdown = 5family, kind, proto, _, address = socket.getaddrinfo(@@ -60,6 +64,7 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0) -listener.close()web.app.state.gateway_runner = Noneweb.app.state.session_authority = None+ web.app.state.withhold_session_token = Falseif hasattr(server, "lifespan") and not server.lifespan.should_exit:await server.lifespan.shutdown()raise@@ -74,6 +79,7 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0) -listener.close()web.app.state.gateway_runner = Noneweb.app.state.session_authority = None+ web.app.state.withhold_session_token = Falsetask = asyncio.create_task(serve(), name="gateway-api")origin_host = f"[{host}]" if ":" in host else hostdiff --git a/hermes_cli/web_server_dashboard.py b/hermes_cli/web_server_dashboard.pyindex d90600eabd4..d891844a7a5 100644--- a/hermes_cli/web_server_dashboard.py+++ b/hermes_cli/web_server_dashboard.py@@ -128,7 +128,8 @@ def mount_spa(application: FastAPI):# gate is off: on a gated serve the token must never be readable without auth.# See #94227, #95575.gated = bool(getattr(application.state, "auth_required", False))- if full_path == "" and not gated:+ withheld = gated or bool(getattr(app.state, "withhold_session_token", False))+ if full_path == "" and not withheld:return HTMLResponse("<!doctype html><html><head><script>"f"window.__HERMES_SESSION_TOKEN__={json.dumps(_server()._SESSION_TOKEN)};"@@ -162,7 +163,9 @@ def mount_spa(application: FastAPI):return JSONResponse({"error": "Frontend not built. Run: cd web && npm run build"}, status_code=404)chat_js = "true" if _DASHBOARD_EMBEDDED_CHAT_ENABLED else "false"gated = bool(getattr(app.state, "auth_required", False))- token_js = "" if gated else f'window.__HERMES_SESSION_TOKEN__="{_server()._SESSION_TOKEN}";'+ # The gateway-hosted listener (gateway/run_api.py) never publishes it: local clients use tickets.+ withheld = gated or bool(getattr(app.state, "withhold_session_token", False))+ token_js = "" if withheld else f'window.__HERMES_SESSION_TOKEN__="{_server()._SESSION_TOKEN}";'# Launcher-preselected profile (``--open-profile``): the SPA's fallback scope when the URL# omits ``?profile=`` (#73085). ``</`` escaped so a hostile name cannot close the script tag.initial_profile_js = json.dumps(str(getattr(application.state, "initial_profile", "") or "")).replace("</", "<\\/")diff --git a/tests/gateway/test_gateway_api_token_exposure.py b/tests/gateway/test_gateway_api_token_exposure.pynew file mode 100644index 00000000000..4bb41f0ad99--- /dev/null+++ b/tests/gateway/test_gateway_api_token_exposure.py@@ -0,0 +1,43 @@+"""The always-on gateway HTTP listener must not hand its bearer token to any loopback peer.++Local clients authenticate with single-use tickets minted on the owner-only control socket+(SO_PEERCRED). ``GET /`` on the dashboard SPA injects ``window.__HERMES_SESSION_TOKEN__``+whenever the auth gate is off, and the gateway listener runs gate-off on loopback, so any+other OS user on the host could read the token and use every ``/api/*`` route+(``/api/env/reveal``) and ``/api/ws?token=`` without ever touching the socket.+"""+import types++import httpx+import pytest+from fastapi import FastAPI+from starlette.testclient import TestClient+++async def test_gateway_listener_never_publishes_the_session_token(tmp_path, monkeypatch):+ from gateway.run_api import start_gateway_api, stop_gateway_api+ from hermes_cli import web_server++ dist = tmp_path / "web_dist"+ (dist / "assets").mkdir(parents=True)+ (dist / "index.html").write_text("<html><head></head><body>spa</body></html>", encoding="utf-8")+ monkeypatch.setattr(web_server, "WEB_DIST", dist)+ monkeypatch.delenv("HERMES_SERVE_HEADLESS", raising=False)+ # The process app mounted its SPA at import against the packaged dist; mount the same+ # routes over a built dist so GET / renders the bootstrap script the token rides in.+ spa = FastAPI()+ web_server.mount_spa(spa)+ assert web_server._SESSION_TOKEN in TestClient(spa).get("/").text # standalone dashboard: unchanged+ runner = types.SimpleNamespace(session_runtime_descriptor=None, session_authority=None)++ handle = await start_gateway_api(runner)+ try:+ async with httpx.AsyncClient(base_url=handle.api_origin, trust_env=False) as client:+ assert (await client.get("/api/status")).status_code != 500+ page = TestClient(spa).get("/")+ assert page.status_code == 200 and "spa" in page.text+ assert web_server._SESSION_TOKEN not in page.text+ finally:+ await stop_gateway_api(handle)+ assert web_server._SESSION_TOKEN in TestClient(spa).get("/").text