mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 03:54:08 +00:00
A bot running on a headless Linux gateway now gets its own desktop (TigerVNC Xvnc + a minimal Xfce session, one per profile) that Hermes Desktop streams live. The user can watch the bot work, take over to type a login / 2FA code / CAPTCHA, and hand control back; the bot refuses every computer_use action (screenshots included) while a human holds the screen, then resumes with the session cookies the human just created. Why this shape: - The screen lives on the machine Hermes runs on, not in a vendor cloud browser, so it works for any app the bot drives and keeps the session on the user's host. - Xfce components are launched individually (xfwm4, xfce4-panel, xfdesktop, xfsettingsd) under a private dbus session rather than xfce4-session/the metapackage: no screensaver, power manager or polkit agent to lock or prompt a headless desktop. - Transport is raw RFB over a WebSocket beside /api/ws, authenticated with a one-shot ticket minted through the already-authenticated RPC channel; noVNC runs in the Electron renderer. The bridge parses the RFB client stream and drops keyboard/pointer/clipboard (incl. QEMU Extended KeyEvent, which noVNC switches to once Xvnc advertises it) from any viewer that does not hold the lease, so viewOnly is enforced server-side, not by the client. - One lease per profile (agent | human viewer) is the single truth for the RFB bridge, the computer_use tool and the Desktop UI; taking control evicts other viewers' input with close code 4000 control-taken. - Auto-start happens only at the computer_use tool boundary (headless host, packages present, bot_desktop.auto_start=true); env builders stay pure so status probes and tests never spawn X servers. tests/tools/conftest.py pins the binaries to "missing" for the same reason the browser-use fixture does. Surfaces: Desktop (Bots → right-click → Open Screen; Take over / Hand back), CLI (`hermes computer-use screen status|start|stop|install-deps`), tool (`computer_use` actions request_handoff / wait_for_human), gateway RPCs (display.status/start/stop/observe/lease.acquire/lease.release + display.lease event), docs page user-guide/features/bot-screen.
99 lines
4.3 KiB
Python
99 lines
4.3 KiB
Python
"""WS-upgrade auth credentials for gated mode.
|
|
|
|
Browsers cannot set ``Authorization`` on a WebSocket upgrade, and gated mode has no token
|
|
injected into the SPA, so two credential shapes exist: (1) single-use browser tickets
|
|
(``mint_ticket`` / ``consume_ticket``) fetched via authenticated ``POST /api/auth/ws-ticket`` and
|
|
passed as ``?ticket=`` on the upgrade — 30 s TTL, a leak is uninteresting; (2) a process-lifetime
|
|
internal credential (``internal_ws_credential`` / ``consume_internal_credential``) for
|
|
*server-spawned* WS clients (the embedded-TUI PTY child on ``/api/ws`` + ``/api/pub``), which
|
|
reuse their attach URL on every reconnect, possibly >30 s after boot — minted once, never expires,
|
|
multi-use, never injected into any HTML/SPA (leaves the process only via the child's environment,
|
|
so browser XSS cannot read it; grants no more than a ticket). In-memory; ``time.time`` patchable.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import secrets
|
|
import threading
|
|
import time
|
|
from typing import Any, Dict, Optional, Tuple
|
|
|
|
#: Long enough for ``getWsTicket()`` -> open WS, short enough that a leaked ticket is uninteresting.
|
|
TTL_SECONDS = 30
|
|
|
|
_lock = threading.Lock()
|
|
_tickets: Dict[str, Tuple[int, Dict[str, Any]]] = {} # ticket -> (expires_at, info)
|
|
_internal_credential: Optional[str] = None # lazily minted; guarded by ``_lock``
|
|
|
|
#: Identity recorded for internal-credential connections (audit logs distinguish them from tickets).
|
|
INTERNAL_USER_ID = "server-internal"
|
|
INTERNAL_PROVIDER = "server-internal"
|
|
|
|
|
|
class TicketInvalid(Exception):
|
|
"""Ticket missing, expired, or already consumed."""
|
|
|
|
|
|
def mint_ticket(*, user_id: str, provider: str, extra: Optional[Dict[str, Any]] = None) -> str:
|
|
"""One-shot base64url ticket (32 random bytes) bound to this identity; ``consume_ticket``
|
|
hands the ``info`` dict back to the WS handler. ``extra`` rides along for routes that need
|
|
server-chosen context (the Bot Desktop bridge pins the RFB socket's profile home here so a
|
|
client can never pick another profile's screen)."""
|
|
ticket = secrets.token_urlsafe(32)
|
|
info = {"user_id": user_id, "provider": provider, "minted_at": int(time.time()), **(extra or {})}
|
|
with _lock:
|
|
_tickets[ticket] = (int(time.time()) + TTL_SECONDS, info)
|
|
_gc_expired_locked()
|
|
return ticket
|
|
|
|
|
|
def consume_ticket(ticket: str) -> Dict[str, Any]:
|
|
"""Validate and consume (single-use). Raises :class:`TicketInvalid` on missing/expired/used."""
|
|
now = int(time.time())
|
|
with _lock:
|
|
entry = _tickets.pop(ticket, None)
|
|
if entry is None:
|
|
# Truncated so misuse never logs the secret in full.
|
|
truncated = (ticket[:8] + "…") if ticket else "<empty>"
|
|
raise TicketInvalid(f"unknown ticket: {truncated}")
|
|
expires_at, info = entry
|
|
if expires_at < now:
|
|
raise TicketInvalid("expired")
|
|
return info
|
|
|
|
|
|
def _gc_expired_locked() -> None:
|
|
"""Drop expired tickets. Caller must hold ``_lock``."""
|
|
now = int(time.time())
|
|
for t in [t for t, (exp, _) in _tickets.items() if exp < now]:
|
|
_tickets.pop(t, None)
|
|
|
|
|
|
def internal_ws_credential() -> str:
|
|
"""Process-lifetime internal WS credential, minted once. Never injected into the SPA or
|
|
returned over REST — only passed to a spawned child via its environment."""
|
|
global _internal_credential
|
|
with _lock:
|
|
if _internal_credential is None:
|
|
_internal_credential = secrets.token_urlsafe(32)
|
|
return _internal_credential
|
|
|
|
|
|
def consume_internal_credential(value: str) -> Dict[str, Any]:
|
|
"""Validate an internal credential (NOT single-use); returns the fixed server-internal
|
|
``{user_id, provider}`` info dict, mirroring ``consume_ticket``. Constant-time compare; any
|
|
value is rejected until a credential has been minted."""
|
|
with _lock:
|
|
expected = _internal_credential
|
|
if not value or expected is None:
|
|
raise TicketInvalid("no internal credential")
|
|
if not secrets.compare_digest(value.encode(), expected.encode()):
|
|
raise TicketInvalid("internal credential mismatch")
|
|
return {"user_id": INTERNAL_USER_ID, "provider": INTERNAL_PROVIDER}
|
|
|
|
|
|
def _reset_for_tests() -> None:
|
|
"""Test-only: drop all tickets and the internal credential."""
|
|
global _internal_credential
|
|
with _lock:
|
|
_tickets.clear()
|
|
_internal_credential = None
|