Files
hermes-agent/tests/hermes_cli/native_gateway_client.py
emozilla 3895357a6a fix(webapp): the Webapp's routes exist only on the surfaces that use them
The Web Access routes, the browser's file-upload staging route and the file-ticket route were
mounted on every app, so the standalone `hermes dashboard` and `hermes serve` answered them too:
any loopback dashboard tab could turn on Web Access for the gateway (writing its config and .env
and restarting it), and pastes on the dashboard met the Webapp's pre-parse body cap. Only native
Desktop's Web Access pane (on the gateway's own listener) and the Webapp's browser use them.

The surface policy gains web_access (the gateway's listener, private or Webapp) and browser_files
(the Webapp only); an app with no recorded surface reads as the gateway's listener when a gateway
runner is bound to it, else as the dashboard. Each of those routes answers 404 where its surface
does not use it, and the chat upload body cap applies only where the browser's file routes do.
Tests that reached these routes on the dashboard surface now reach them on the surface that serves
them; native Desktop's Web Access calls in tests carry a real one-use native grant per request.
2026-10-25 03:22:40 -07:00

43 lines
2.2 KiB
Python

"""A test client that reaches the dashboard app as native Desktop reaches the gateway's own listener.
Native Desktop's local connection is the gateway's listener, and Electron's main process mints one
``native-http`` grant per request over the gateway's private control socket and sends it as
``X-Hermes-Gateway-Ticket`` from a loopback peer, with no Origin
(``apps/desktop/electron/local-gateway.ts``, ``nativeGatewayHttpHeaders``). Here the grant comes from
the same ``TicketStore`` the control socket mints from, bound to a runner serving the test's home,
and the peer is recorded as ``GatewayRuntimeAPI`` records it, so the real
``hermes_cli.dashboard_auth.native_http`` gate admits each request.
"""
from __future__ import annotations
from types import SimpleNamespace
from fastapi.testclient import TestClient
def native_desktop_client(monkeypatch) -> TestClient:
"""A client for ``web_server.app`` bound to a gateway runner serving ``get_hermes_home()``."""
from gateway.runtime_bootstrap import TicketStore
from gateway.session_authorities import SessionAuthorities
from hermes_cli import web_server
from hermes_constants import get_hermes_home
home = get_hermes_home().resolve()
authorities = SessionAuthorities(home)
authorities.add(home, SimpleNamespace(profile_id=str(home), instance_id="daemon"))
store = TicketStore("daemon", authorities.profile_ids())
runner = SimpleNamespace(
session_authorities=authorities, session_ticket_store=store, _draining=False,
session_runtime_descriptor={"state": "ready", "instance_id": "daemon",
"served_profiles": authorities.served_profiles()})
monkeypatch.setattr(web_server.app.state, "gateway_runner", runner, raising=False)
async def native_desktop(scope, receive, send):
if scope["type"] == "http":
ticket = store.mint(profile_id=str(home), subject="desktop", purpose="native-http")
scope = {**scope, "headers": [*scope["headers"], (b"x-hermes-gateway-ticket", ticket.encode())],
"hermes.gateway_socket_peer": ("127.0.0.1", 50000)}
await web_server.app(scope, receive, send)
return TestClient(native_desktop, raise_server_exceptions=False)