Skip to content

The agent that grows with you

fix(gateway): replace the browser credential when the Webapp cannot hold its port

emozilla82b7f064485a
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index a09c5cb6c73..753928f7a03 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -496,7 +496,7 @@ async def recover_bot_mailbox(authority):
async def start_gateway_runtime_api(runner):
from gateway import run_api
- from gateway.webapp_listener import dial_origin, listener_plan, register_control_verbs
+ from gateway.webapp_listener import browser_session_token, dial_origin, listener_plan, register_control_verbs
from hermes_constants import get_hermes_home
home = get_hermes_home()
host, port, webapp = listener_plan(home)
@@ -518,6 +518,13 @@ async def start_gateway_runtime_api(runner):
reason = f"could not listen on {host}:{port} ({cause})"
logger.error("Webapp unavailable: %s", reason)
runner.webapp_status = {"state": "unavailable", "url": None, "reason": reason}
+ # Open tabs keep sending their credential to whatever answers on the Webapp's port, and that
+ # is not this gateway now. Replace it, so one captured there never opens this gateway; the
+ # tabs sign in again once the Webapp is back.
+ try:
+ browser_session_token(home, rotate=True)
+ except OSError as write_error:
+ logger.warning("Could not replace the Webapp's browser credential: %s", write_error)
runner.session_api = await run_api.start_gateway_api(runner)
else:
if webapp is not None:
diff --git a/tests/gateway/test_webapp_listener_fallback.py b/tests/gateway/test_webapp_listener_fallback.py
index 6f8e1994557..98b679c66c8 100644
--- a/tests/gateway/test_webapp_listener_fallback.py
+++ b/tests/gateway/test_webapp_listener_fallback.py
@@ -40,6 +40,25 @@ def test_the_private_listener_after_a_failed_webapp_mount_is_mains(lost_listen_r
assert "WEBAPP_BUNDLE" not in report["index"]["body"]
+def test_a_webapp_that_loses_its_port_replaces_the_browser_credential(lost_listen_race, tmp_path):
+ # Open tabs keep sending their credential to whatever answers on the Webapp's port. When that is
+ # not this gateway, the credential may have been captured, so it must not open this gateway later.
+ _, report = lost_listen_race
+ assert report["token_rotated"] is True
+ held = _run(tmp_path, {"webapp": {"enabled": True, "port": _free_port()}, "hold_port": True})
+ assert held["start_error"] is None, held.get("start_traceback")
+ assert held["status"]["state"] == "unavailable"
+ assert held["native_session_list"] is True
+ assert held["token_rotated"] is True
+
+
+def test_a_webapp_that_starts_keeps_the_browser_credential(tmp_path):
+ report = _run(tmp_path, {"webapp": {"enabled": True, "port": _free_port()}})
+ assert report["status"]["state"] == "serving"
+ assert report["state"]["session_token_is_browser_credential"] is True
+ assert report["token_rotated"] is False
+
+
@pytest.mark.skipif(sys.platform != "linux", reason="only Linux lets two SO_REUSEADDR sockets bind one address")
def test_the_webapp_owns_its_port_before_mounting_the_dashboard(tmp_path):
# Two processes may both bind the Webapp's port under SO_REUSEADDR; the first to listen owns it.