Skip to content

The agent that grows with you

fix(webapp): hermes webapp under WSL prints a launch link the Windows browser can use

emozillab70215d46ad6
diff --git a/gateway/webapp_listener.py b/gateway/webapp_listener.py
index de42576ec3f..44bdd2e86d0 100644
--- a/gateway/webapp_listener.py
+++ b/gateway/webapp_listener.py
@@ -247,9 +247,13 @@ def _one_use_links_work(platform: str = sys.platform) -> bool:
"""Whether a browser the server opens can redeem a one-use launch link here. Redemption needs
proof that the browser's connection belongs to the gateway's OS user
(``web_routers.webapp._peer_is_server_user``) on Linux, or a browser launcher whose arguments other
- users cannot read (Windows). macOS and the BSDs offer neither, so there the link is printed for
- the user to open by hand and spends without that proof."""
- return platform in ("linux", "win32")
+ users cannot read (Windows: no connection is checked there). macOS and the BSDs offer neither, and
+ neither does WSL: the Windows browser reaches the port through WSL's localhost relay, whose end of
+ the connection belongs to root, and so can any Windows account. There the link is printed for the
+ user to open by hand and spends without that proof."""
+ from hermes_constants import is_wsl
+
+ return platform == "win32" or (platform == "linux" and not is_wsl())
#: How every missing-bundle reason starts; ``hermes webapp --status`` asks for no restart after it.
@@ -283,8 +287,8 @@ def _bundle_missing(web) -> str | None:
def register_control_verbs(runner, home) -> None:
"""The private control-socket verbs ``hermes webapp`` uses; only an authenticated same-user peer
reaches them. ``webapp-launch`` returns a one-use launch link, so the credential crosses neither
- the socket nor a browser's argv; where nothing can check who redeems such a link (macOS) it is
- marked private, for the user to open by hand. ``webapp-sign-out`` replaces the
+ the socket nor a browser's argv; where nothing can check who redeems such a link (macOS, the BSDs,
+ WSL) it is marked private, for the user to open by hand. ``webapp-sign-out`` replaces the
browser credential, which also ends every login bound to it behind the sign-in gate, closes the
browser sockets those opened, and reports how many it closed."""
server = getattr(runner, "session_control_server", None)
@@ -336,8 +340,8 @@ def register_control_verbs(runner, home) -> None:
address = (getattr(web.app.state, "listener_public_url", "")
or registered_lan_origin(home, urlsplit(status["url"]).port) or status["url"])
return {"url": f"{address}/{query}", "private": False, "expires_in_seconds": None, "sign_in": True}
- # Where the browser's connection cannot be checked (macOS, the BSDs), the link is for the user to
- # open by hand ("private": printed, never passed to a browser launcher), and its ticket spends
+ # Where the browser's connection cannot be checked (macOS, the BSDs, WSL), the link is for the user
+ # to open by hand ("private": printed, never passed to a browser launcher), and its ticket spends
# without that check. Either way it works once, for 60 seconds, and carries no lasting credential.
checked = _one_use_links_work()
ticket = mint_launch_ticket(web.app.state) if checked else mint_printed_launch_ticket(web.app.state)
diff --git a/hermes_cli/web_routers/webapp.py b/hermes_cli/web_routers/webapp.py
index cccab773a76..d381ceec498 100644
--- a/hermes_cli/web_routers/webapp.py
+++ b/hermes_cli/web_routers/webapp.py
@@ -85,7 +85,7 @@ def mint_launch_ticket(state) -> str:
def mint_printed_launch_ticket(state) -> str:
"""Ticket for a launch link the user opens by hand where nothing identifies the redeeming
- connection's owner (macOS, the BSDs): it spends without that check, as a window ticket does, so
+ connection's owner (macOS, the BSDs, WSL): it spends without that check, as a window ticket does, so
``hermes webapp`` prints it and never passes it to a browser launcher."""
return _mint_handoff_ticket(state, LAUNCH_TICKET_TTL_SECONDS, launch=False)
diff --git a/hermes_cli/webapp_launcher.py b/hermes_cli/webapp_launcher.py
index e5e09316285..46b12a2d230 100644
--- a/hermes_cli/webapp_launcher.py
+++ b/hermes_cli/webapp_launcher.py
@@ -3,8 +3,8 @@
The gateway serves the Webapp on its own listener when its home's config sets ``webapp.enabled``
(``gateway.webapp_listener``). This command builds the bundle when it is stale, starts the gateway that
serves the current profile, asks it for a one-use launch link over the control socket (same OS user
-only) and opens that link, so the credential appears in no argv, output or link. On macOS and the BSDs,
-where nothing checks who redeems the link, it prints the one-use link for the user to open instead.
+only) and opens that link, so the credential appears in no argv, output or link. On macOS, the BSDs and
+WSL, where nothing checks who redeems the link, it prints the one-use link for the user to open instead.
``--status`` and ``--sign-out`` talk to a running gateway and never start one.
"""
from __future__ import annotations
@@ -251,7 +251,7 @@ def cmd_webapp(args) -> int:
print(f"Open the Webapp and sign in with your dashboard account: {link['url']}")
return 0
if link.get("private"):
- # macOS, the BSDs: nothing checks who redeems this one-use link, so it is for the user to
+ # macOS, the BSDs, WSL: nothing checks who redeems this one-use link, so it is for the user to
# open, never for a browser launcher whose arguments other processes can read.
print(f"Open this link in your browser within {link['expires_in_seconds']} seconds. It works "
"once and signs the browser in as you, so keep it private:")
diff --git a/tests/hermes_cli/test_webapp_launch.py b/tests/hermes_cli/test_webapp_launch.py
index 5f79ae56a92..7610cc3ab4d 100644
--- a/tests/hermes_cli/test_webapp_launch.py
+++ b/tests/hermes_cli/test_webapp_launch.py
@@ -156,3 +156,71 @@ def test_a_handoff_page_keeps_the_credential_out_of_every_url(monkeypatch, page)
assert "a" * 43 not in served.text
assert served.headers["cache-control"] == "no-store"
assert served.headers["referrer-policy"] == "no-referrer"
+
+
+def _launch_link(tmp_path, monkeypatch):
+ """The loopback Webapp of a gateway serving *tmp_path*, and the ``webapp-launch`` answer its control
+ socket gives ``hermes webapp``."""
+ import json
+ from types import SimpleNamespace
+
+ from fastapi.testclient import TestClient
+
+ from gateway.control_socket import GatewayControlServer
+ from gateway.webapp_listener import register_control_verbs
+ from hermes_cli import web_server as server
+
+ home = tmp_path / ".hermes"
+ home.mkdir()
+ monkeypatch.setattr(server.app.state, "ui_surface", "webapp", raising=False)
+ monkeypatch.setattr(server.app.state, "auth_required", False, raising=False)
+ monkeypatch.setattr(server.app.state, "bound_host", "127.0.0.1", raising=False)
+ monkeypatch.setattr(server.app.state, "webapp_window_tickets", {}, raising=False)
+ monkeypatch.setattr(server, "_SESSION_TOKEN", "a" * 43)
+ runner = SimpleNamespace(session_control_server=GatewayControlServer(home),
+ webapp_status={"state": "serving", "url": "http://127.0.0.1:9120", "reason": None},
+ session_api=SimpleNamespace(app=SimpleNamespace(web=server)))
+ register_control_verbs(runner, home)
+ raw = json.dumps({"protocol": 1, "verb": "webapp-launch", "id": 1, "params": {}}).encode()
+ answer = json.loads(runner.session_control_server.handle_request_line(raw, "same-user"))
+ assert answer["ok"], answer
+ return TestClient(server.app, base_url="http://127.0.0.1"), answer["result"]
+
+
+def _spend(client, link):
+ from urllib.parse import parse_qs, urlsplit
+
+ ticket = parse_qs(urlsplit(link["url"]).fragment)["ticket"][0]
+ return client.post("/webapp/window-session", headers={"Origin": "http://127.0.0.1", "X-Hermes-Window-Ticket": ticket})
+
+
+def test_under_wsl_the_launch_link_is_printed_and_the_windows_browser_spends_it(tmp_path, monkeypatch):
+ """Under WSL2 the Windows browser reaches the gateway's loopback port through WSL's localhost relay,
+ so inside Linux its connection belongs to root (uid 0, measured live), and any Windows account can
+ reach that port. Nothing identifies the user's own browser, so, as on macOS, ``hermes webapp`` gets
+ a link marked private, to print for the user to open by hand, and that link spends without the
+ peer check instead of being refused every time."""
+ from hermes_cli.web_routers import webapp
+
+ monkeypatch.setattr("hermes_constants.is_wsl", lambda: True)
+ monkeypatch.setattr(webapp, "_loopback_peer_uid", lambda client, server: 0) # WSL's localhost relay
+ client, link = _launch_link(tmp_path, monkeypatch)
+ assert link["private"] is True
+ spent = _spend(client, link)
+ assert spent.status_code == 200, f"the WSL user's own Windows browser cannot spend the link: {spent.text}"
+
+
+def test_native_linux_keeps_the_peer_check_on_the_launch_link(tmp_path, monkeypatch):
+ """Outside WSL, Linux names the owner of the browser's loopback connection: ``hermes webapp`` opens
+ the link itself, and a connection another user owns cannot spend it."""
+ import os
+
+ from hermes_cli.web_routers import webapp
+
+ monkeypatch.setattr("hermes_constants.is_wsl", lambda: False)
+ monkeypatch.setattr(webapp, "_loopback_peer_uid", lambda client, server: os.getuid() + 1) # windows-footgun: ok — platforms("linux") test
+ client, link = _launch_link(tmp_path, monkeypatch)
+ assert link["private"] is False
+ assert _spend(client, link).status_code == 403
diff --git a/website/docs/reference/cli-commands.md b/website/docs/reference/cli-commands.md
index 85c88b99b79..4bf80adfc97 100644
--- a/website/docs/reference/cli-commands.md
+++ b/website/docs/reference/cli-commands.md
@@ -1937,8 +1937,8 @@ use. See [Desktop App → Use the Desktop workspace from a browser](../user-guid
The link is valid for 60 seconds and works once; the browser exchanges it for the sign-in it keeps in
that tab. Only your own OS user can get a link: the gateway answers this command over its private
-control socket. On macOS and the BSDs, where a one-use link cannot be checked against your OS user,
-the command prints the link for you to open and never passes it to a browser launcher. Until it is
+control socket. On macOS, the BSDs and WSL, where a one-use link cannot be checked against your OS
+user, the command prints the link for you to open and never passes it to a browser launcher. Until it is
used it signs a browser in as you, so keep it private.
For a named profile served by the default gateway (`hermes -p work webapp`), the Webapp opens on that
diff --git a/website/docs/user-guide/desktop.md b/website/docs/user-guide/desktop.md
index acb4abfc0b1..00936e91ec0 100644
--- a/website/docs/user-guide/desktop.md
+++ b/website/docs/user-guide/desktop.md
@@ -53,9 +53,10 @@ serves the CLI, the native Desktop and messaging, so every surface sees one conv
keeps in that tab, so reloading keeps you signed in, and so does a gateway restart. A fresh tab or a
bare URL shows instructions instead of access: run `hermes webapp` again for a new link. **New
Window**, profile windows and session pop-outs hand the sign-in over with their own one-use link. On
-macOS and the BSDs, where Hermes cannot check which OS user's browser opens the link, the command
-prints the one-use link for you to open instead of opening a browser; until it is used it signs a
-browser in as you, so keep it private. `hermes webapp --sign-out` signs out every
+macOS, the BSDs and WSL (where the Windows browser reaches the gateway through WSL's relay), Hermes
+cannot check which OS user's browser opens the link, so the command prints the one-use link for you
+to open instead of opening a browser; until it is used it signs a browser in as you, so keep it
+private. `hermes webapp --sign-out` signs out every
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