Skip to content

The agent that grows with you

fix(gateway): publish the numeric loopback a localhost Webapp bound to native clients

Teknium319a5e69965e
diff --git a/gateway/run_api.py b/gateway/run_api.py
index 3d6bb43904a..055eb975824 100644
--- a/gateway/run_api.py
+++ b/gateway/run_api.py
@@ -271,7 +271,9 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0, w
task = asyncio.create_task(serve(), name="gateway-api")
return GatewayAPIHandle(
- api_origin=dial_origin(host, state.bound_port),
+ # The address the socket bound, never a name: native clients accept only a numeric loopback
+ # origin, and ``localhost`` may resolve to either family.
+ api_origin=dial_origin(listener.getsockname()[0], state.bound_port),
server=server, task=task, app=mount, socket=listener,
)
diff --git a/gateway/webapp_listener.py b/gateway/webapp_listener.py
index f9c288c54ea..bb6d12b9014 100644
--- a/gateway/webapp_listener.py
+++ b/gateway/webapp_listener.py
@@ -170,7 +170,9 @@ def _is_wildcard(host: str) -> bool:
def dial_host(bind_host: str) -> str:
- """The loopback address native clients dial for a listener bound to *bind_host*."""
+ """The loopback address native clients dial for a listener bound to the address *bind_host*. A
+ name (``localhost``) comes back as is, which suits a browser only: native clients get the
+ address the socket bound (``gateway.run_api.start_gateway_api``)."""
if _is_loopback(bind_host):
return bind_host
return "::1" if bind_host == "::" else "127.0.0.1"
diff --git a/tests/gateway/test_webapp_listener.py b/tests/gateway/test_webapp_listener.py
index ef719c8154e..c28aad9872c 100644
--- a/tests/gateway/test_webapp_listener.py
+++ b/tests/gateway/test_webapp_listener.py
@@ -53,7 +53,6 @@ def test_a_malformed_webapp_block_keeps_the_listener_private(tmp_path, block):
@pytest.mark.parametrize(("bind", "dial"), [
("127.0.0.1", "127.0.0.1"),
- ("localhost", "localhost"),
("::1", "::1"),
("0.0.0.0", "127.0.0.1"),
("::", "::1"),
diff --git a/tests/gateway/test_webapp_listener_fallback.py b/tests/gateway/test_webapp_listener_fallback.py
index 39a6754a3ce..f3335f4800f 100644
--- a/tests/gateway/test_webapp_listener_fallback.py
+++ b/tests/gateway/test_webapp_listener_fallback.py
@@ -6,7 +6,9 @@ Desktop, CLI and TUI clients still connect, the mount carries none of the Webapp
``hermes webapp --status`` says why the Webapp is not served. Each case runs a real gateway listener
in a child process (``fixtures/webapp_fallback_peer.py``) with its own ``HERMES_HOME``.
"""
+import ipaddress
import sys
+from urllib.parse import urlsplit
import pytest
@@ -123,3 +125,19 @@ def test_a_webapp_whose_browser_credential_cannot_be_kept_leaves_native_clients_
assert report["state"]["surface"] == "<absent>"
assert report["state"]["web_dist_set"] is False
+
+def test_a_webapp_bound_to_localhost_publishes_a_numeric_loopback_origin_native_clients_accept(tmp_path):
+ from hermes_cli.gateway_runtime import _endpoint
+
+ report = _run(tmp_path, {"webapp": {"enabled": True, "host": "localhost", "port": _free_port()}})
+ assert report["start_error"] is None, report.get("start_traceback")
+ assert report["status"]["state"] == "serving"
+ origin = report["api_origin"]
+ assert ipaddress.ip_address(urlsplit(origin).hostname).is_loopback
+ assert report["native_session_list"] is True
+ home = tmp_path / "state"
+ payload = {"runtime_protocol": 1, "served_profiles": [{"home": str(home), "profile_id": "default"}],
+ "state": "ready", "capabilities": ["session-authority-v1"], "authority_epoch": 1,
+ "instance_id": "webapp-fallback", "api_origin": origin, "supervisor": "none"}
+ discovered = _endpoint(payload, home)
+ assert (discovered.state, discovered.reason_code) == ("ready", None)