The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-screen): ticket and desktop-gone refusals reach the viewer as close frames
teknium122bb5d805478
diff --git a/hermes_cli/web_routers/display.py b/hermes_cli/web_routers/display.pyindex 4ccb2721893..408490ce926 100644--- a/hermes_cli/web_routers/display.py+++ b/hermes_cli/web_routers/display.py@@ -65,9 +65,15 @@ def _consume_display_ticket(ws: WebSocket) -> Optional[dict]:@router.websocket("/api/display/ws")async def display_ws(ws: WebSocket) -> None:+ # Host/Origin/client-IP policy is refused BEFORE accept, like every other dashboard route: a+ # cross-origin page never gets a completed handshake. Everything after that (ticket, desktop+ # state) is refused AFTER accept so the code + reason arrive in a close frame — a close before+ # accept surfaces in the browser as an opaque HTTP 403 and the renderer cannot tell "re-observe"+ # (4401) from "screen is gone" (4001).if not _ws_request_is_allowed(ws):await ws.close(code=_CLOSE_NOT_ALLOWED)return+ await ws.accept()info = _consume_display_ticket(ws)if info is None:await ws.close(code=_CLOSE_BAD_TICKET, reason="display ticket missing, expired or used")@@ -76,7 +82,8 @@ async def display_ws(ws: WebSocket) -> None:async def _bridge(ws: WebSocket, info: dict) -> None:- """Pump RFB bytes between the viewer socket and THIS profile's Xvnc, gated by the lease."""+ """Pump RFB bytes between the viewer socket (already accepted) and THIS profile's Xvnc, gated by+ the lease."""from hermes_constants import hermes_home_keyfrom tools.bot_desktop import lease as _leasefrom tools.bot_desktop.rfb_filter import RfbClientFilter@@ -96,7 +103,6 @@ async def _bridge(ws: WebSocket, info: dict) -> None:await ws.close(code=_CLOSE_DESKTOP_GONE, reason="Bot Desktop socket unreachable")return- await ws.accept()loop = asyncio.get_running_loop()evicted = asyncio.Event()held = {"ever": _lease.viewer_may_send_input(viewer_id, profile_key=profile_home)}diff --git a/tests/hermes_cli/test_display_ws_ticket.py b/tests/hermes_cli/test_display_ws_ticket.pyindex fc072596b15..6cb2dd0613b 100644--- a/tests/hermes_cli/test_display_ws_ticket.py+++ b/tests/hermes_cli/test_display_ws_ticket.py@@ -25,3 +25,38 @@ def test_display_ticket_must_be_a_bot_desktop_ticket_pinned_to_a_profile_home(moinfo = display._consume_display_ticket(_Ws(display_ticket=good))assert info and info["hermes_home"] == "/srv/hermes/bot-a" and info["viewer_id"] == "v"assert display._consume_display_ticket(_Ws(display_ticket=good)) is None, "single use"+++def test_a_bad_ticket_is_refused_with_a_close_frame_the_renderer_can_read(monkeypatch):+ """Closing BEFORE accept surfaces to a browser as an HTTP 403 handshake failure with no close+ code; the renderer's close listener never learns WHY (4401 = ticket, 4001 = desktop gone) and+ cannot pick between re-observing and showing "not running". The handshake must complete and+ the reason travel in the close frame."""+ import pytest+ from starlette.testclient import TestClient+ from starlette.websockets import WebSocketDisconnect++ from hermes_cli import web_server++ ws_tickets._reset_for_tests()+ prev = {k: getattr(web_server.app.state, k, None) for k in ("auth_required", "bound_host")}+ web_server.app.state.auth_required = False+ web_server.app.state.bound_host = None+ client = TestClient(web_server.app)+ try:+ used = ws_tickets.mint_ticket(user_id="display:v", provider="bot-desktop",+ extra={"hermes_home": "/srv/hermes/bot-a", "viewer_id": "v"})+ ws_tickets.consume_ticket(used)+ with client.websocket_connect(f"/api/display/ws?display_ticket={used}") as conn: # handshake completes+ with pytest.raises(WebSocketDisconnect) as exc:+ conn.receive_bytes()+ assert exc.value.code == 4401+ finally:+ client.close()+ ws_tickets._reset_for_tests()+ for k, v in prev.items():+ if v is None:+ if hasattr(web_server.app.state, k):+ delattr(web_server.app.state, k)+ else:+ setattr(web_server.app.state, k, v)