Skip to content

The agent that grows with you

fix(webapp-access): only native Desktop on this computer can change Web Access

emozilla9c0f48e2f74d
diff --git a/hermes_cli/web_routers/webapp_access.py b/hermes_cli/web_routers/webapp_access.py
index c2c24bfb0c6..81eeeab643e 100644
--- a/hermes_cli/web_routers/webapp_access.py
+++ b/hermes_cli/web_routers/webapp_access.py
@@ -2,9 +2,9 @@
Thin HTTP over :mod:`hermes_cli.webapp_access`. Every handler acts on the home of the gateway that
serves the requested profile; start and stop write its ``webapp:`` config and restart that gateway,
-which reads the config only when it starts. Start, Stop and Restart run one at a time. Only the
-machine's owner reaches them (:func:`owner_on_this_machine`), never a browser signed in over the
-network.
+which reads the config only when it starts. Start, Stop and Restart run one at a time. Only native
+Desktop on this computer reaches them (:func:`owner_on_this_machine`), never a browser, and only on
+the gateway's own listener, where its pane calls them (``SurfacePolicy.web_access``; 404 elsewhere).
"""
from __future__ import annotations
@@ -35,13 +35,14 @@ _CHANGE_POLL_SECONDS = 0.05
def owner_on_this_machine(request: Request) -> None:
- """Web Access decides what this machine exposes, so only its owner here may use it: the native
- app (a same-user gateway ticket, gated or not) or, on a listener without sign-in, the loopback
- session token that ``auth_middleware`` has already checked. Behind the sign-in gate any other
- caller is a cookie or bearer login, which may come from another device."""
- if getattr(request.state, "native_http_principal", None):
- return
- if getattr(request.app.state, "auth_required", False):
+ """Web Access decides what this machine exposes, so only native Desktop on this computer may use
+ it: a request admitted with a native grant (``dashboard_auth.native_http``), a one-use ticket from
+ the gateway's private control socket, which answers only this OS user, sent from loopback with no
+ Origin. A browser tab can present neither. Every other credential is refused, the loopback session
+ token included: on the Webapp it is the browser credential, which a tab holds even when it reaches
+ the gateway from another machine (an SSH tunnel), and behind the sign-in gate a cookie or bearer
+ login may come from another device."""
+ if not getattr(request.state, "native_http_principal", None):
raise HTTPException(status_code=403, detail="Web Access can only be changed from the Hermes app on this computer.")
diff --git a/tests/hermes_cli/test_webapp_access.py b/tests/hermes_cli/test_webapp_access.py
index b0a7de3400e..2be241e1479 100644
--- a/tests/hermes_cli/test_webapp_access.py
+++ b/tests/hermes_cli/test_webapp_access.py
@@ -244,25 +244,46 @@ def test_a_browser_signed_in_over_the_network_cannot_use_web_access(lan_browser,
assert not _config()["webapp"]["enabled"]
-def test_web_access_answers_the_native_app_and_the_loopback_token_only():
- """The owner on this machine is the native app (its same-user gateway ticket, gated or not) or,
- on a listener without sign-in, the holder of the loopback session token, which the auth
- middleware has already checked. Behind the sign-in gate anything else is a remote login."""
- from types import SimpleNamespace
+def webapp_tab(monkeypatch):
+ """A tab of the default Webapp (loopback, no sign-in gate), which may be a browser on another
+ machine through ``ssh -L 9120:127.0.0.1:9120``: it holds the browser credential, which on this
+ listener is the loopback session token."""
+ from hermes_cli import web_server
- from fastapi import HTTPException
+ for key, value in (("ui_surface", "webapp"), ("auth_required", False), ("bound_host", "127.0.0.1"),
+ ("trusted_public_hosts", frozenset())):
+ monkeypatch.setattr(web_server.app.state, key, value, raising=False)
+ tab = TestClient(web_server.app, base_url="http://127.0.0.1:9120", raise_server_exceptions=False)
+ tab.headers[web_server._SESSION_HEADER_NAME] = web_server._SESSION_TOKEN
+ yield tab
+ tab.close()
+
+
+def test_a_webapp_tab_cannot_use_web_access(webapp_tab, restarts, nous):
+ """Web Access decides what this machine exposes, so only native Desktop on this computer may use
+ it: each of its requests carries a one-use grant from the gateway's private control socket, which
+ answers only this OS user, and no Origin, and a browser tab can present neither. The browser
+ credential is not that proof, even where it is the loopback session token: every Web Access
+ route answers a Webapp tab 403 and changes nothing (the pane being hidden in the browser is UI
+ only)."""
+ assert webapp_tab.get("/api/webapp-access/status").status_code == 403
+ assert webapp_tab.post("/api/webapp-access/start", json={"mode": "lan"}).status_code == 403
+ assert webapp_tab.post("/api/webapp-access/stop").status_code == 403
+ assert webapp_tab.post("/api/webapp-access/restart").status_code == 403
+ assert nous == []
+ assert restarts == []
+ assert not _config().get("webapp", {}).get("enabled")
- from hermes_cli.web_routers.webapp_access import owner_on_this_machine
- def request(*, gated: bool, native: bool):
- return SimpleNamespace(app=SimpleNamespace(state=SimpleNamespace(auth_required=gated)),
- state=SimpleNamespace(native_http_principal={"profile_id": "x"} if native else None))
+def test_native_desktop_uses_web_access_on_the_webapps_own_listener(client, monkeypatch):
+ """With the Webapp on, native Desktop dials the gateway's listener that serves it, and its Web
+ Access pane keeps working there."""
+ from hermes_cli import web_server
+
+ monkeypatch.setattr(web_server.app.state, "ui_surface", "webapp", raising=False)
- owner_on_this_machine(request(gated=True, native=True))
- owner_on_this_machine(request(gated=False, native=False))
- with pytest.raises(HTTPException) as refused:
- owner_on_this_machine(request(gated=True, native=False))
- assert refused.value.status_code == 403
+ assert client.get("/api/webapp-access/status").status_code == 200
def test_start_builds_the_webapp_bundle_before_turning_it_on(client, restarts, nous, builds):