Skip to content

The agent that grows with you

fix(webapp): read webapp.host the way the sign-in gate does

emozillacd58d3dd36e2
diff --git a/gateway/webapp_listener.py b/gateway/webapp_listener.py
index 25317c65702..e837123a8f4 100644
--- a/gateway/webapp_listener.py
+++ b/gateway/webapp_listener.py
@@ -2,8 +2,9 @@
``webapp.enabled`` in the gateway home's ``config.yaml`` turns it on. The gateway then binds its one
listener to ``webapp.host``/``webapp.port`` (a stable port, so the browser's URL survives restarts) and
-its dashboard mount serves the Webapp bundle. ``webapp.host`` is a loopback address or a wildcard
-(``0.0.0.0``/``::``), and a wildcard bind is gated by dashboard sign-in like any non-loopback listener.
+its dashboard mount serves the Webapp bundle. ``webapp.host`` is ``127.0.0.1``, ``::1`` or ``localhost``
+(the binds the sign-in gate leaves local-only) or a wildcard (``0.0.0.0``/``::``), and a wildcard bind
+is gated by dashboard sign-in like any non-loopback listener.
``webapp.public_url`` is the address a reverse proxy or tunnel serves the Webapp at: it engages the same
sign-in on a loopback bind, and ``dashboard.public_url`` (``hermes dashboard``'s) never applies here.
Native clients (Desktop, the CLI, Ink) dial the loopback address this module derives from the bind.
@@ -98,9 +99,9 @@ def webapp_settings(home) -> WebappSettings:
return off(f"webapp.enabled must be true or false in {path}")
host = block.get("host", DEFAULT_HOST)
port = block.get("port", DEFAULT_PORT)
- host = _bare_host(host) if isinstance(host, str) else None
- if host is None or not (_is_loopback(host) or _is_wildcard(host)):
- return off(f"webapp.host must be a loopback address, 0.0.0.0 or :: in {path}")
+ host = _canonical_host(_bare_host(host)) if isinstance(host, str) else None
+ if host is None or not (_local_only(host) or _is_wildcard(host)):
+ return off(f"webapp.host must be 127.0.0.1, ::1, localhost, 0.0.0.0 or :: in {path}")
if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535:
return off(f"webapp.port must be a port number (1-65535) in {path}")
public_url = _public_url(block.get("public_url"))
@@ -119,6 +120,24 @@ def _bare_host(host: str) -> str:
return host
+def _canonical_host(host: str) -> str:
+ """*host* spelled as the listener's sign-in gate compares it: an IP address in its short form
+ (``0:0:0:0:0:0:0:1`` is ``::1``), a name in lower case."""
+ try:
+ return ipaddress.ip_address(host).compressed
+ except ValueError:
+ return host.lower()
+
+
+def _local_only(host: str) -> bool:
+ """Whether the listener's sign-in gate leaves a bind to *host* open as local-only. It knows
+ ``127.0.0.1``, ``::1`` and ``localhost``; any other address, another loopback one included,
+ engages sign-in like a public bind."""
+ from hermes_cli.web_server_boundary import should_require_auth
+
+ return not should_require_auth(host)
+
+
def _public_url(value) -> str | None:
"""``webapp.public_url`` without a trailing slash, "" when unset, None when malformed (not an
http(s) address, or one carrying credentials, a query or a fragment)."""
@@ -136,6 +155,8 @@ def _public_url(value) -> str | None:
def _is_loopback(host: str) -> bool:
+ """Whether native clients can dial *host* itself (any loopback address); which binds the
+ sign-in gate leaves open is :func:`_local_only`."""
if host == "localhost":
return True
try:
diff --git a/tests/gateway/test_webapp_settings.py b/tests/gateway/test_webapp_settings.py
index b5ccdd84eb5..f17d1c831de 100644
--- a/tests/gateway/test_webapp_settings.py
+++ b/tests/gateway/test_webapp_settings.py
@@ -57,6 +57,35 @@ def test_brackets_around_anything_but_an_ipv6_address_keep_the_webapp_off(tmp_pa
assert settings.enabled is False and "webapp.host" in settings.reason
[email protected](("host", "bind_host"), [
+ ("127.0.0.1", "127.0.0.1"),
+ ("'::1'", "::1"),
+ ("localhost", "localhost"),
+ ("LocalHost", "localhost"),
+ ("'0:0:0:0:0:0:0:1'", "::1"),
+ ("'0000::0001'", "::1"),
+ ("0.0.0.0", "0.0.0.0"),
+ ("'0:0:0:0:0:0:0:0'", "::"),
+ ("127.0.0.2", None),
+ ("127.255.255.254", None),
+ ("'::ffff:127.0.0.1'", None),
+])
+def test_the_webapp_and_its_sign_in_gate_agree_on_which_hosts_stay_on_this_machine(tmp_path, host, bind_host):
+ """A webapp.host the settings accept as local-only is one the listener's sign-in gate leaves
+ open, and only a wildcard bind needs sign-in. Another spelling of 127.0.0.1, ::1 or localhost
+ binds that address; any other loopback address, which the gate would treat as public, keeps the
+ Webapp off and says which hosts work."""
+ from hermes_cli.web_server_boundary import should_require_auth
+
+ settings = webapp.webapp_settings(_home(tmp_path, f"webapp:\n enabled: true\n host: {host}\n"))
+ if bind_host is None:
+ assert settings.bind == ("127.0.0.1", 0)
+ assert "webapp.host must be 127.0.0.1, ::1, localhost, 0.0.0.0 or ::" in settings.reason
+ return
+ assert settings.bind == (bind_host, webapp.DEFAULT_PORT)
+ assert should_require_auth(settings.host) is webapp._is_wildcard(settings.host)
+
+
def _gateway(monkeypatch, tmp_path, config):
"""The runtime's listener start for *config*, with the listener itself replaced by a stand-in."""
from gateway import run_api, run_runtime
diff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.md
index c828472e2a3..5c7e70e1af4 100644
--- a/website/docs/user-guide/configuration.md
+++ b/website/docs/user-guide/configuration.md
@@ -3170,13 +3170,13 @@ The gateway can serve the [Desktop workspace in a browser](./desktop.md#use-the-
```yaml
webapp:
enabled: false # true: the gateway serves the Webapp
- host: 127.0.0.1 # loopback address, or 0.0.0.0 / :: for other machines (dashboard sign-in required)
+ host: 127.0.0.1 # 127.0.0.1, ::1 or localhost; or 0.0.0.0 / :: for other machines (dashboard sign-in required)
port: 9120 # fixed, so the browser's address stays the same across gateway restarts
public_url: "" # the https:// address a reverse proxy or tunnel serves the Webapp at (sign-in required)
```
- `enabled` — the gateway reads it at start; restart the gateway after changing it (`hermes gateway restart`).
-- `host` — a loopback address keeps the Webapp on this machine. `0.0.0.0` or `::` makes it reachable from other machines and requires a [dashboard sign-in provider](./features/web-dashboard.md#authentication-gated-mode); without one the gateway serves native clients only and `hermes webapp --status` says why. Restart the gateway after adding the provider. Any other value keeps the Webapp off.
+- `host` — `127.0.0.1`, `::1` or `localhost` keeps the Webapp on this machine. `0.0.0.0` or `::` makes it reachable from other machines and requires a [dashboard sign-in provider](./features/web-dashboard.md#authentication-gated-mode); without one the gateway serves native clients only and `hermes webapp --status` says why. Restart the gateway after adding the provider. Any other value, another loopback address such as `127.0.0.2` included, keeps the Webapp off.
- `port` — when the port is taken, the gateway likewise serves native clients only and `hermes webapp --status` reports why. It also replaces the Webapp's browser sign-in, since another program may have received it, so open tabs need `hermes webapp` again once the port is free.
- `public_url` — set it when a reverse proxy or tunnel you run serves the Webapp at its own address (forwarding to `host:port`). It turns on dashboard sign-in even on a loopback `host`, it is the one outside hostname the Webapp accepts requests for, and sign-in callbacks are built on it. Without a sign-in provider the gateway serves native clients only, as for `0.0.0.0`. It must be an `http://` or `https://` address with no credentials, query or fragment; anything else keeps the Webapp off. `dashboard.public_url` is for `hermes dashboard` and never applies to the Webapp.