Skip to content

The agent that grows with you

fix(webapp): a browser login's token opens no socket on its own

Tekniumf314a662e52d
diff --git a/hermes_cli/web_server_chat.py b/hermes_cli/web_server_chat.py
index 982edf74bfe..20efcbb12d8 100644
--- a/hermes_cli/web_server_chat.py
+++ b/hermes_cli/web_server_chat.py
@@ -175,12 +175,13 @@ from hermes_cli.web_server_boundary import (
)
-def _ws_request_view(ws: WebSocket) -> Request:
+def _ws_request_view(ws: WebSocket, app) -> Request:
"""A ``Request`` facade over a ``WebSocket`` for the auth helpers.
``_verify_access_token`` only touches ``request.headers`` (X-Forwarded-For
via ``client_ip``) and ``request.client`` on this path (``audit=False``,
- no cookie/redirect work), so a lightweight duck-typed stand-in avoids
+ no cookie/redirect work), and ``webapp_sign_in.token_works_alone`` reads
+ ``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.
"""
@@ -188,6 +189,7 @@ def _ws_request_view(ws: WebSocket) -> Request:
return Request({
"type": "http",
+ "app": app,
"headers": [(k.lower().encode("latin-1"), v.encode("latin-1"))
for k, v in getattr(ws.headers, "items", dict)()],
"client": (ws.client.host, 0) if ws.client else None,
@@ -289,12 +291,16 @@ def _ws_auth_reason(ws: WebSocket, *, allow_internal: bool = False,
# and a provider-verified token is accepted on identity merit.
token = ws.query_params.get("token", "")
if token:
+ from hermes_cli import webapp_sign_in
from hermes_cli.dashboard_auth.base import ProviderError
from hermes_cli.dashboard_auth.middleware import _verify_access_token
+ view = _ws_request_view(ws, app)
try:
- session = _verify_access_token(
- _ws_request_view(ws), access_token=token, audit=False)
+ # On the gateway's Webapp a browser login's tokens work only as its cookies, which
+ # sign-out ends; only a token this listener gave a native app's sign-in works alone.
+ session = (_verify_access_token(view, access_token=token, audit=False)
+ if webapp_sign_in.token_works_alone(view, token) else None)
except ProviderError as exc:
# All providers unreachable (IDP outage) — reject without
# crashing the upgrade; the REST bearer leg answers 503, and
diff --git a/tests/hermes_cli/test_webapp_gated_sign_out.py b/tests/hermes_cli/test_webapp_gated_sign_out.py
index 3231d3794a2..e118d15cdc1 100644
--- a/tests/hermes_cli/test_webapp_gated_sign_out.py
+++ b/tests/hermes_cli/test_webapp_gated_sign_out.py
@@ -394,6 +394,31 @@ def test_off_the_webapp_any_provider_token_works_on_its_own_as_on_main(gated, mo
assert _renew(gated, refresh).status_code == 200
+def _gateway_socket_with(gated, access):
+ """The ``/api/ws`` upgrade from a client holding nothing but *access* (``?token=``): the close
+ code the gate answers with, or None when it lets the socket open."""
+ from urllib.parse import quote
+
+ try:
+ with gated.browser().websocket_connect(f"ws://hermes.lan:9120/api/ws?token={quote(access)}") 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."""
+ browser = gated.browser()
+ _sign_in_with_password(browser)
+ access, _ = _tokens(browser)
+ native_access, _ = _native_app_sign_in(gated)
+ assert _gateway_socket_with(gated, access) == 4401
+ assert _gateway_socket_with(gated, native_access) is None
+ monkeypatch.setattr(gated.server.app.state, "ui_surface", "dashboard")
+ assert _gateway_socket_with(gated, 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