Skip to content

The agent that grows with you

fix(dashboard): sockets on the standalone dashboard and hermes serve authenticate as on main

emozillaaa34b22a750b
diff --git a/hermes_cli/web_server_chat.py b/hermes_cli/web_server_chat.py
index beb01b6bef4..73813367d38 100644
--- a/hermes_cli/web_server_chat.py
+++ b/hermes_cli/web_server_chat.py
@@ -214,10 +214,12 @@ def _ws_auth_reason(ws: WebSocket, *, allow_internal: bool = False,
``HOST_TERMINAL_SOCKET`` or one of ``AUDIO_SOCKETS``): a ticket minted for
one socket opens no other, and the host terminal opens only with its own ticket.
- Loopback / ``--insecure``: legacy ``?token=`` (constant-time compared).
+ Loopback / ``--insecure``: legacy ``?token=`` (constant-time compared); on the
+ gateway's Webapp a one-use socket ticket first (``/api/webapp/socket-ticket``).
Gated: ``?ticket=`` (browser-minted, single-use, 30s TTL) or ``?internal=``
(process-lifetime, multi-use, only for server-spawned WS clients so the PTY
- child can reconnect; never injected into the SPA), or ``?token=`` holding a
+ child can reconnect; never injected into the SPA; on the gateway's Webapp it
+ opens only the sockets that pass *allow_internal*), or ``?token=`` holding a
user session access token (verified against the dashboard auth session
providers — the same ``verify_session`` seam the native bearer REST leg
uses — e.g. a token-mode Remote desktop connection; on the gateway's Webapp
@@ -238,6 +240,9 @@ def _ws_auth_reason(ws: WebSocket, *, allow_internal: bool = False,
from hermes_cli.browser_credential import generation_of
from hermes_cli.web_session_sockets import OWN_TICKET_SOCKETS, track
auth_required = bool(getattr(app.state, "auth_required", False))
+ # The Webapp's own rules (socket tickets on the loopback gate, the server's credential only where
+ # its spawned clients dial) apply on its listener; the standalone dashboard and serve keep main's.
+ webapp = getattr(app.state, "ui_surface", None) == "webapp"
if socket in OWN_TICKET_SOCKETS:
# No standing credential (the session token, a native grant, a login's access token) opens it.
reject = functools.partial(_audit_ws_rejection, ws) if auth_required else _ignore_rejection
@@ -269,7 +274,7 @@ def _ws_auth_reason(ws: WebSocket, *, allow_internal: bool = False,
internal = ws.query_params.get("internal", "")
if internal:
- if not allow_internal:
+ if webapp and not allow_internal:
_reject("internal: endpoint not allowed")
return "internal_not_allowed", "internal"
try:
@@ -337,9 +342,9 @@ def _ws_auth_reason(ws: WebSocket, *, allow_internal: bool = False,
return "no_credential", "none"
- # Loopback: a one-use ticket ``/api/webapp/socket-ticket`` minted for the session-token owner
- # (the Webapp's live socket on this same origin), else the legacy ``?token=``.
- consumed = _consume_ws_ticket(ws, _ignore_rejection, socket)
+ # Loopback: on the Webapp a one-use ticket ``/api/webapp/socket-ticket`` minted for the
+ # session-token owner (its sockets on this same origin), else the legacy ``?token=``.
+ consumed = _consume_ws_ticket(ws, _ignore_rejection, socket) if webapp else None
if consumed is not None:
return consumed
token = ws.query_params.get("token", "")
diff --git a/tests/hermes_cli/test_dashboard_auth_ws_auth.py b/tests/hermes_cli/test_dashboard_auth_ws_auth.py
index bfbdc336656..17c1e4c39b1 100644
--- a/tests/hermes_cli/test_dashboard_auth_ws_auth.py
+++ b/tests/hermes_cli/test_dashboard_auth_ws_auth.py
@@ -234,11 +234,12 @@ class TestWsAuthOkLoopback:
ws = _fake_ws(query={"token": web_server._SESSION_TOKEN})
assert _web_server_chat._ws_auth_ok(ws) is True
- def test_webapp_socket_ticket_is_accepted_once(self, loopback_app):
+ def test_webapp_socket_ticket_is_accepted_once(self, loopback_app, monkeypatch):
"""The Webapp on a loopback bind mints the browser's one-use socket ticket
- (``POST /api/webapp/socket-ticket?socket=gateway``) for the session-token owner: the gate takes it once
- and selects only the public protocol. A Bot Desktop screen ticket is never a login here
- either."""
+ (``POST /api/webapp/socket-ticket?socket=gateway``) for the session-token owner: the gate
+ takes it once and selects only the public protocol. A Bot Desktop screen ticket is never a
+ login here either."""
+ monkeypatch.setattr(web_server.app.state, "ui_surface", "webapp", raising=False)
ticket = mint_ticket(user_id="legacy-token-owner", provider="session-token")
protocols = (
_web_server_chat._GATEWAY_WS_PROTOCOL,
@@ -253,6 +254,28 @@ class TestWsAuthOkLoopback:
assert _web_server_chat._ws_auth_ok(_fake_ws(query={"ticket": screen}, path="/api/ws")) is False
+ @pytest.mark.parametrize("surface", ["dashboard", "serve"])
+ def test_off_the_webapp_the_loopback_gate_takes_only_the_token_as_on_main(
+ self, loopback_app, monkeypatch, surface):
+ """The standalone dashboard and ``hermes serve`` mint no socket tickets, so their loopback
+ gate judges ``?token=`` alone, as on main: a ticket opens nothing there and is not spent, and
+ one riding beside the session token does not refuse it."""
+ from hermes_cli.dashboard_auth.ws_tickets import consume_ticket
+
+ monkeypatch.setattr(web_server.app.state, "ui_surface", surface, raising=False)
+ ticket = mint_ticket(user_id="legacy-token-owner", provider="session-token")
+ protocols = (
+ _web_server_chat._GATEWAY_WS_PROTOCOL,
+ f"{_web_server_chat._GATEWAY_WS_TICKET_PROTOCOL_PREFIX}{ticket}",
+ )
+ assert _web_server_chat._ws_auth_reason(_fake_ws(query={"ticket": ticket}, path="/api/ws")) == (
+ "no_credential", "none")
+ assert _web_server_chat._ws_auth_ok(_fake_ws(query={}, path="/api/ws", protocols=protocols)) is False
+ assert _web_server_chat._ws_auth_ok(
+ _fake_ws(query={"ticket": "never-minted", "token": web_server._SESSION_TOKEN}, path="/api/ws")) is True
+ assert consume_ticket(ticket)["user_id"] == "legacy-token-owner"
+
+
class TestWsAuthOkGated:
"""Gate ON — ticket path only."""
@@ -312,7 +335,10 @@ class TestWsAuthOkGated:
ws = _fake_ws(query={"token": web_server._SESSION_TOKEN})
assert _web_server_chat._ws_auth_ok(ws) is False
- def test_internal_credential_is_scoped_to_server_child_endpoints(self, gated_app):
+ def test_internal_credential_is_scoped_to_server_child_endpoints(self, gated_app, monkeypatch):
+ """On the gateway's Webapp the server's own credential opens only the sockets its spawned
+ clients dial (``/api/ws``, ``/api/pub``)."""
+ monkeypatch.setattr(web_server.app.state, "ui_surface", "webapp", raising=False)
credential = internal_ws_credential()
for path in (
@@ -332,6 +358,18 @@ class TestWsAuthOkGated:
assert _web_server_chat._ws_auth_ok(gateway, allow_internal=True) is True
assert _web_server_chat._ws_auth_ok(publisher, allow_internal=True) is True
+ @pytest.mark.parametrize("surface", ["dashboard", "serve"])
+ def test_off_the_webapp_internal_credential_opens_every_socket_as_on_main(
+ self, gated_app, monkeypatch, surface):
+ """The standalone dashboard and ``hermes serve`` accept the server's own credential on every
+ socket their gate judges, plugin sockets included, as on main."""
+ monkeypatch.setattr(web_server.app.state, "ui_surface", surface, raising=False)
+ credential = internal_ws_credential()
+ paths = ("/api/pty", "/api/console", "/api/events", "/api/audio/speak-stream", "/api/plugins/kanban/events")
+ for path in paths:
+ assert _web_server_chat._ws_auth_reason(_fake_ws(query={"internal": credential}, path=path)) == (
+ None, "internal"), path
+
def test_session_token_accepted_in_gated_mode(self, gated_app):
"""A provider-verified session access token (?token=) is accepted in
gated mode — the credential a token-mode Remote desktop connection