The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): refuse live /browser connect on any owner that can multiplex
Teknium637e3c682eea
diff --git a/tests/hermes_cli/test_classic_browser_manage.py b/tests/hermes_cli/test_classic_browser_manage.pyindex 272afc496a7..4c97b5bb1cb 100644--- a/tests/hermes_cli/test_classic_browser_manage.py+++ b/tests/hermes_cli/test_classic_browser_manage.py@@ -113,9 +113,10 @@ def homes(tmp_path, monkeypatch):cdp.shutdown()-def _runner_serving(*names):+def _runner_serving(*names, multiplexed=True):authorities = [types.SimpleNamespace(profile_id=name, retiring=False) for name in names]- return types.SimpleNamespace(session_authorities=types.SimpleNamespace(serving=lambda: authorities))+ return types.SimpleNamespace(session_authorities=types.SimpleNamespace(+ serving=lambda: authorities, multiplexed=multiplexed))def _call(server, profile_id, params):@@ -147,19 +148,39 @@ def test_a_browser_one_profile_connects_never_reaches_another_profiles_session(hassert status["result"]["connected"] is False, status-def test_single_profile_local_owner_connects_the_gateways_browser(homes, monkeypatch):- """One profile: the gateway's browser tools are that profile's alone, so connect/disconnect run- (what Ink, Desktop and now the classic CLI send)."""+def test_single_profile_standalone_owner_connects_the_gateways_browser(homes, monkeypatch):+ """A standalone owner (never multiplexes, so it can never hot-serve another profile): the+ gateway's browser tools are that profile's alone, so connect/disconnect run (what Ink, Desktop+ and now the classic CLI send)."""import osfrom gateway import run_apiserver, launch, _bee, url, reaps = homes- # Multiplex mode is the default even with one profile: what counts is how many are SERVED.- from agent import secret_scope- monkeypatch.setattr(secret_scope, "is_multiplex_active", lambda: True)- monkeypatch.setattr(run_api, "_ACTIVE_RUNNER", _runner_serving("default"))+ monkeypatch.setattr(run_api, "_ACTIVE_RUNNER", _runner_serving("default", multiplexed=False))reply = _call(server, launch, {"action": "connect", "url": url})assert reply["result"]["connected"] is True, replyassert os.environ["BROWSER_CDP_URL"] == url and reapsassert _call(server, launch, {"action": "status"})["result"]["url"] == urlassert _call(server, launch, {"action": "disconnect"})["result"]["connected"] is Falseassert "BROWSER_CDP_URL" not in os.environ+++def test_connect_then_hot_serve_never_hands_one_profiles_browser_to_the_next(homes, monkeypatch):+ """Review #141357 P1: a multiplexing owner serving ONE profile must still refuse connect, or a+ profile hot-served afterwards inherits the process-wide override over its own browser.cdp_url+ (and disconnect is then refused, so it can never be withdrawn)."""+ import os+ from gateway import run_api+ from gateway.session_authorities import SessionAuthorities+ from tools.browser_tool_cdp import _get_cdp_override_raw+ from gateway.run import _profile_runtime_scope+ server, launch, bee, url, reaps = homes+ (bee / "config.yaml").write_text("browser:\n cdp_url: ws://bee.invalid/devtools/browser/PROFILE-B\n")+ registry = SessionAuthorities(launch, multiplexed=True)+ registry.add(launch, types.SimpleNamespace(profile_id=str(launch), retiring=False), name="default")+ monkeypatch.setattr(run_api, "_ACTIVE_RUNNER", types.SimpleNamespace(session_authorities=registry))+ reply = _call(server, launch, {"action": "connect", "url": url})+ assert reply["error"]["code"] == 4030 and reply["error"]["data"]["reason"] == "browser_profile_shared", reply+ registry.add(bee, types.SimpleNamespace(profile_id=str(bee), retiring=False), name="bee") # hot-serve+ assert "BROWSER_CDP_URL" not in os.environ and reaps == []+ with _profile_runtime_scope(bee):+ assert _get_cdp_override_raw().endswith("/PROFILE-B")diff --git a/tui_gateway/ws_legacy_fallback.py b/tui_gateway/ws_legacy_fallback.pyindex 0cf6b14106f..d2510a497c7 100644--- a/tui_gateway/ws_legacy_fallback.py+++ b/tui_gateway/ws_legacy_fallback.py@@ -50,26 +50,30 @@ _PROCESS_GLOBAL = frozenset({"process.stop", "delegation.pause", "reload.mcp", "# ``BROWSER_CDP_URL`` that every chat's browser tools read first (dokterdok N25); ``status`` and ``use``# (the profile's ``browser.backend``) stay. A missing action means ``status``.#-# CLI-9 (#134889): on an owner serving ONE profile those chats are all that profile's, so the live-# connect is the user's own choice and runs (Ink, Desktop and the classic CLI all send it here). On an-# owner serving several profiles it would reach another profile's sessions, and the browser state has-# no per-profile scope yet (``cleanup_all_browsers`` reaps every task's browser): refused by name.-_PROCESS_GLOBAL_ACTIONS = {"browser.manage": ({"connect", "disconnect"}, (- "/browser connect and /browser disconnect are unavailable on a gateway serving several profiles: "+# CLI-9 (#134889): on an owner that serves ONE profile and never multiplexes (a standalone gateway)+# those chats are all that profile's, so the live connect is the user's own choice and runs (Ink,+# Desktop and the classic CLI all send it here). An owner that CAN multiplex refuses by name even+# while it serves one profile: a later hot-served profile would inherit the process-wide override+# (which wins over its own ``browser.cdp_url``), and the browser state has no per-profile scope yet+# (``cleanup_all_browsers`` reaps every task's browser). ``data.reason`` is the client's bounded code.+_PROCESS_GLOBAL_ACTIONS = {"browser.manage": ({"connect", "disconnect"}, "browser_profile_shared", (+ "/browser connect and /browser disconnect are unavailable on a gateway that serves several profiles: ""the live browser connection is shared by every profile's chats. Set browser.cdp_url in this ""profile's config.yaml instead."))}-def _serves_several_profiles() -> bool:- """Whether this owner serves more than one profile (the running gateway's authority registry);- without a registered runner, whether the process multiplexes at all (fail closed)."""+def _owner_may_multiplex() -> bool:+ """Whether this owner serves, or can hot-serve, more than one profile (the running gateway's+ authority registry: multiplex mode, or a second profile ever served); without a registered+ runner, whether the process multiplexes at all. Unknown means yes (fail closed)."""+ from agent.secret_scope import is_multiplex_activefrom gateway import run_apirunner = getattr(run_api, "_ACTIVE_RUNNER", None)- if runner is not None and getattr(runner, "session_authorities", None) is not None:- from gateway.session_authorities import serving_authorities- return len(serving_authorities(runner)) > 1- from agent.secret_scope import is_multiplex_active- return is_multiplex_active()+ registry = getattr(runner, "session_authorities", None)+ if registry is None:+ return is_multiplex_active()+ from gateway.session_authorities import serving_authorities+ return bool(getattr(registry, "multiplexed", True)) or len(serving_authorities(runner)) > 1def legacy_fallback_allowed(actor: Any, method: str) -> bool:@@ -159,9 +163,10 @@ def _own_profile_read(server: Any, req: dict, home: str) -> dict:def dispatch_legacy(server: Any, req: dict, transport: Any, actor: Any) -> dict | None:"""Run ``server.dispatch`` with the ticket's profile home bound (call from a worker thread;the pool path copies this context, so long handlers see it too)."""- actions, refusal = _PROCESS_GLOBAL_ACTIONS.get(req.get("method"), ((), ""))- if (req.get("params") or {}).get("action", "status") in actions and _serves_several_profiles():- return {"jsonrpc": "2.0", "id": req.get("id"), "error": {"code": 4030, "message": refusal}}+ actions, reason, refusal = _PROCESS_GLOBAL_ACTIONS.get(req.get("method"), ((), "", ""))+ if (req.get("params") or {}).get("action", "status") in actions and _owner_may_multiplex():+ return {"jsonrpc": "2.0", "id": req.get("id"),+ "error": {"code": 4030, "message": refusal, "data": {"reason": reason}}}try:home = _foreign_profile_home(server, getattr(actor, "profile_id", None))except OSError: