Skip to content

The agent that grows with you

test(webapp): a browser login's access token opens none of the Webapp's token sockets

emozilla4e54af5d8d01
diff --git a/hermes_cli/web_server_chat.py b/hermes_cli/web_server_chat.py
index 20efcbb12d8..04e81b109ea 100644
--- a/hermes_cli/web_server_chat.py
+++ b/hermes_cli/web_server_chat.py
@@ -184,6 +184,7 @@ def _ws_request_view(ws: WebSocket, app) -> Request:
``request.app.state``, so a lightweight stand-in carrying *app* avoids
constructing a real ASGI request inside the upgrade. ``scan_session_providers``
itself never sees the request — only the provider callbacks do.
+ ``webapp_sign_in.token_works_alone`` reads the listener's state from *app*.
"""
from fastapi import Request
@@ -219,7 +220,9 @@ def _ws_auth_reason(ws: WebSocket, *, allow_internal: bool = False,
child can reconnect; never injected into the SPA), 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). The legacy in-process
+ uses — e.g. a token-mode Remote desktop connection; on the gateway's Webapp
+ only a token that listener gave a native app's sign-in, as for the bearer
+ header, ``webapp_sign_in.token_works_alone``). The legacy in-process
``_SESSION_TOKEN`` is rejected in gated mode: a leaked ``_SESSION_TOKEN``
must not grant access.
@@ -288,7 +291,10 @@ def _ws_auth_reason(ws: WebSocket, *, allow_internal: bool = False,
# the WS upgrade 403'd forever. The legacy in-process
# ``_SESSION_TOKEN`` never reaches this check: gated mode falls
# through to the explicit rejection below (leaked-constant safety),
- # and a provider-verified token is accepted on identity merit.
+ # and a provider-verified token is accepted on identity merit. On the
+ # gateway's Webapp it follows the bearer header's rule: only a token this
+ # listener gave a native app's sign-in works on its own, so a browser
+ # login's tokens work only as its cookies, which sign-out ends.
token = ws.query_params.get("token", "")
if token:
from hermes_cli import webapp_sign_in
diff --git a/hermes_cli/webapp_sign_in.py b/hermes_cli/webapp_sign_in.py
index ce55d4ad493..27d4648b6c0 100644
--- a/hermes_cli/webapp_sign_in.py
+++ b/hermes_cli/webapp_sign_in.py
@@ -16,14 +16,14 @@ are tracked (``web_session_sockets.track``) and close at sign-out, and a ticket
opens nothing after it. The standalone dashboard never sets or checks the binding.
Such a login's cookies also hold the sign-in provider's access and refresh token. Presented on their own
-(``Authorization: Bearer``, ``/auth/native/refresh``) they would skip the binding, and a list of tokens
-to refuse cannot catch them: a provider may accept many spellings of one token (the bundled password
-provider decodes base64 leniently, so the quoted cookie value verifies too). So on the gateway's Webapp
-behind the gate a provider token works on its own only when this listener gave it to a native app's
-sign-in (RFC 8252: ``/auth/native/token``, then ``/auth/native/refresh``). The listener keeps a digest of
-each such token until it expires, in ``RECORD_PATH`` under the gateway home (``app.state.webapp_home``),
-so native apps stay signed in across a restart and a sign-out. A browser login's tokens therefore work
-only as that login's cookies and end with it.
+(``Authorization: Bearer``, a socket's ``?token=``, ``/auth/native/refresh``) they would skip the
+binding, and a list of tokens to refuse cannot catch them: a provider may accept many spellings of one
+token (the bundled password provider decodes base64 leniently, so the quoted cookie value verifies too).
+So on the gateway's Webapp behind the gate a provider token works on its own only when this listener
+gave it to a native app's sign-in (RFC 8252: ``/auth/native/token``, then ``/auth/native/refresh``). The
+listener keeps a digest of each such token until it expires, in ``RECORD_PATH`` under the gateway home
+(``app.state.webapp_home``), so native apps stay signed in across a restart and a sign-out. A browser
+login's tokens therefore work only as that login's cookies and end with it.
"""
from __future__ import annotations
@@ -116,9 +116,10 @@ def renew(response: Response, request: Request) -> None:
def token_works_alone(request: Request, token: str) -> bool:
- """Whether provider token *token* may be used on its own (a bearer header, the refresh route) on
- *request*'s listener: anywhere but the gateway's Webapp behind the gate, as on main; there, only
- when this listener gave it to a native app's sign-in and it has not expired."""
+ """Whether provider token *token* may be used on its own (a bearer header, a socket's
+ ``?token=``, the refresh route) on *request*'s listener: anywhere but the gateway's Webapp behind
+ the gate, as on main; there, only when this listener gave it to a native app's sign-in and it has
+ not expired."""
if not applies(request):
return True
with _records_lock:
diff --git a/tests/hermes_cli/test_webapp_gated_sign_out.py b/tests/hermes_cli/test_webapp_gated_sign_out.py
index e118d15cdc1..71d69ab2792 100644
--- a/tests/hermes_cli/test_webapp_gated_sign_out.py
+++ b/tests/hermes_cli/test_webapp_gated_sign_out.py
@@ -6,8 +6,9 @@ credential, so ``hermes webapp --sign-out``, which replaces that credential, end
the login's requests get 401 and the sign-in view, the sockets and host terminals it opened close with
4401, tickets it minted open nothing, and its cookies replayed from anywhere get nothing. The provider
tokens those cookies hold work only as those cookies: the Webapp's listener refuses them as a bearer
-token and on the refresh route, before and after sign-out and across a restart. Signing in again works,
-and a native app's own sign-in on the same listener stays signed in. The standalone dashboard has no
+token, on the refresh route and as a socket's ``?token=``, before and after sign-out and across a
+restart. Signing in again works, and a native app's own sign-in on the same listener (and the sockets
+it opens) stays signed in. The standalone dashboard has no
browser credential and keeps its logins as on main.
"""
import json
@@ -391,6 +392,7 @@ def test_off_the_webapp_any_provider_token_works_on_its_own_as_on_main(gated, mo
_sign_in_with_password(browser)
access, refresh = _tokens(browser)
assert gated.browser().get("/api/auth/me", headers=_bearer(access)).status_code == 200
+ assert _socket_close_code(gated.browser(), "/api/events?channel=tab-1", access) is None
assert _renew(gated, refresh).status_code == 200
@@ -406,6 +408,23 @@ def _gateway_socket_with(gated, access):
return closed.code
+# Sockets on the Webapp's listener that take a provider token as ``?token=`` (the host terminal takes
+# only its own ticket); ``/api/console`` runs the same gate as ``/api/pty``.
+TOKEN_SOCKETS = ("/api/ws", "/api/events?channel=tab-1", "/api/pub?channel=tab-1", "/api/pty",
+ "/api/audio/speak-stream", "/api/audio/transcribe-stream")
+
+
+def _socket_close_code(client, path, token):
+ """The code a dial of *path* presenting ``?token=`` *token* is closed with, or None while it
+ stays open."""
+ joiner = "&" if "?" in path else "?"
+ try:
+ with client.websocket_connect(f"ws://hermes.lan:9120{path}{joiner}token={token}") as ws:
+ return _close_code(ws, 0.5)
+ except WebSocketDisconnect as closed:
+ return closed.code
+
+
def test_a_logins_access_token_opens_no_socket_on_its_own(gated, monkeypatch):
"""The upgrade gate takes a provider token on its own on the same terms as the bearer header:
on the Webapp only one given to a native app's sign-in, anywhere else any, as on main."""
@@ -419,6 +438,29 @@ def test_a_logins_access_token_opens_no_socket_on_its_own(gated, monkeypatch):
assert _gateway_socket_with(gated, access) is None
[email protected]("path", TOKEN_SOCKETS)
+def test_a_logins_access_token_opens_none_of_the_token_sockets(gated, sign_out, path):
+ """A socket's ``?token=`` follows the bearer header's rule: a browser login's access token,
+ copied out of its cookie, opens no socket on the Webapp's listener, before sign-out or after it."""
+ browser = gated.browser()
+ _sign_in_with_password(browser)
+ access, _refresh = _tokens(browser)
+ assert _socket_close_code(gated.browser(), path, access) == 4401
+ sign_out()
+ assert _socket_close_code(gated.browser(), path, access) == 4401
+
+
+def test_a_native_apps_token_opens_a_socket_that_stays_open_across_sign_out(gated, sign_out):
+ """A token the listener gave a native app's sign-in opens a socket as ``?token=``; signing the
+ browsers out leaves that socket open and the token working."""
+ access, _refresh = _native_app_sign_in(gated)
+ with gated.browser().websocket_connect(f"ws://hermes.lan:9120/api/events?channel=desk&token={access}") as events:
+ assert _close_code(events, 0.2) is None
+ assert sign_out()["connections_closed"] == 0
+ assert _close_code(events, 0.5) is None
+ assert _socket_close_code(gated.browser(), "/api/events?channel=desk", access) is None
+
+
@pytest.mark.platforms("posix")
def test_the_record_of_native_sign_ins_is_user_only(gated, tmp_path):
from hermes_cli.webapp_sign_in import RECORD_PATH
diff --git a/website/docs/developer-guide/gateway-local-sessions.md b/website/docs/developer-guide/gateway-local-sessions.md
index f287493e8e2..9ec7b0e350c 100644
--- a/website/docs/developer-guide/gateway-local-sessions.md
+++ b/website/docs/developer-guide/gateway-local-sessions.md
@@ -57,8 +57,8 @@ The server issues `session:operator` only at these existing trusted boundaries:
gateways' Webapps on one hostname keep their own), so replacing it ends those logins too; sign-out closes
every socket the token or a bound login opened, Bot Desktop viewers included. The provider tokens
such a login keeps in its cookies never work there on their own: the Webapp's listener accepts a
- bearer token, and renews a refresh token, only when it gave that token to a native app's RFC 8252
- sign-in (digests kept in `webapp/native-sign-in-tokens.json` in the gateway home until they
+ bearer token or a socket's `?token=`, and renews a refresh token, only when it gave that token to a
+ native app's RFC 8252 sign-in (digests kept in `webapp/native-sign-in-tokens.json` in the gateway home until they
expire), so native apps stay signed in across a sign-out and a restart.
- The HTTP mutation gate, after a verified dashboard Session, native HTTP owner grant,
or permitted legacy session token has been accepted.
diff --git a/website/docs/user-guide/desktop.md b/website/docs/user-guide/desktop.md
index 72a9c435088..acb4abfc0b1 100644
--- a/website/docs/user-guide/desktop.md
+++ b/website/docs/user-guide/desktop.md
@@ -59,8 +59,8 @@ browser in as you, so keep it private. `hermes webapp --sign-out` signs out ever
tab at once: their open connections close, Bot Desktop viewers included, and each shows the sign-in
instructions. Behind the sign-in gate (below) it also signs out every browser that signed in with a
dashboard account; each must sign in again. The account tokens such a login keeps in its cookies work
-on the Webapp only as that login (the gateway refuses them as a bearer token and on its token refresh),
-so they end with it. A native Hermes app that signed in to the gateway on its own stays signed in.
+on the Webapp only as that login (the gateway refuses them as a bearer token, on a connection's URL and
+on its token refresh), so they end with it. A native Hermes app that signed in to the gateway on its own stays signed in.
For a named profile, `hermes -p work webapp` opens the Webapp on `work`. Profiles the default gateway
serves share its Webapp; a standalone profile's own gateway serves its own when its config turns it on.