fix(gateway): refuse live /browser connect on any owner that can multiplex
A multiplexing owner serving one profile accepted /browser connect and wrote the process-wide BROWSER_CDP_URL. A profile hot-served afterwards read that override ahead of its own browser.cdp_url, so it drove the first profile's browser, and disconnect was then refused because two profiles were served. The gate now asks whether the owner can multiplex (the authority registry's multiplexed flag, or more than one served profile), not how many profiles it serves right now. A standalone owner still runs connect/disconnect. The refusal carries data.reason=browser_profile_shared. Review #141357 (P1, connect-then-hot-serve). Refs #134889 (CLI-9)
This commit is contained in:
@@ -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(h
|
||||
assert 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 os
|
||||
from gateway import run_api
|
||||
server, 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, reply
|
||||
assert os.environ["BROWSER_CDP_URL"] == url and reaps
|
||||
assert _call(server, launch, {"action": "status"})["result"]["url"] == url
|
||||
assert _call(server, launch, {"action": "disconnect"})["result"]["connected"] is False
|
||||
assert "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")
|
||||
|
||||
@@ -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_active
|
||||
from gateway import run_api
|
||||
runner = 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)) > 1
|
||||
|
||||
|
||||
def 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:
|
||||
|
||||
Reference in New Issue
Block a user