The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(browser): browser_cdp can open, switch and close tabs while a tab is private
Tekniumbd5d47315d7c
diff --git a/tests/tools/test_browser_cdp_tool.py b/tests/tools/test_browser_cdp_tool.pyindex 276c7b71047..86e17bfc152 100644--- a/tests/tools/test_browser_cdp_tool.py+++ b/tests/tools/test_browser_cdp_tool.py@@ -660,6 +660,37 @@ def test_unreadable_target_list_fails_closed(monkeypatch, cdp_server):assert not [m for m in cdp_server.received() if m.get("method") == "Runtime.evaluate"]+LOCAL_TAB = "http://127.0.0.1:3000/"++[email protected](("method", "params", "allowed"), [+ ("Target.createTarget", {"url": PUBLIC_PAGE}, True),+ ("Target.createTarget", {"url": "http://127.0.0.1:3000/admin"}, False), # checked like Page.navigate+ ("Target.createTarget", {"url": PRIVATE_URL}, False),+ ("Target.activateTarget", {"targetId": "tab-public"}, True),+ ("Target.closeTarget", {"targetId": "tab-public"}, True),+ ("Network.getAllCookies", {}, False), # every other target-less call stays fail-closed+ ("Browser.setDownloadBehavior", {"behavior": "allow", "downloadPath": "/srv"}, False),+])+def test_tab_management_stays_usable_while_a_tab_is_private(monkeypatch, cdp_server, method, params, allowed):+ """One private tab (a local dev server) must not stop the model opening, switching or closing tabs; a+ new tab's url is held to Page.navigate's rule, and every other browser-level call is still refused."""+ cdp_server.on("Target.getTargets", lambda params, sid: {"targetInfos": [+ {"targetId": "tab-public", "type": "page", "url": PUBLIC_PAGE},+ {"targetId": "tab-local", "type": "page", "url": LOCAL_TAB},+ ]})+ cdp_server.on(method, lambda params, sid: {"targetId": "new-tab"} if method == "Target.createTarget" else {})+ monkeypatch.setattr(browser_url_policy, "eval_ssrf_guard_active", lambda: True)++ result = json.loads(browser_cdp_tool.browser_cdp(method=method, params=params))++ sent = [m for m in cdp_server.received() if m.get("method") == method]+ if allowed:+ assert result["success"] is True and len(sent) == 1+ else:+ assert "private or internal address" in result["error"] and sent == []++def test_private_guard_inactive_does_not_probe(monkeypatch, cdp_server):cdp_server.on("Runtime.evaluate", lambda params, sid: {"result": {"value": "ok"}})diff --git a/tools/browser_cdp_tool.py b/tools/browser_cdp_tool.pyindex efbd3297f83..24c0bca6060 100644--- a/tools/browser_cdp_tool.py+++ b/tools/browser_cdp_tool.py@@ -19,10 +19,12 @@ logger = logging.getLogger(__name__)CDP_DOCS_URL = "https://chromedevtools.github.io/devtools-protocol/"-# Browser/target inspection that never reads page body/cookies/DOM/storage — stays-# usable so the model can list tabs or navigate away from a blocked page.+# Browser/target inspection and tab management that never reads page body/cookies/DOM/storage — stays+# usable so the model can list, open, switch and close tabs or navigate away from a blocked page. Methods+# that take a url (Page.navigate, Target.createTarget) are still held to _METHOD_PARAM_GUARDS._CDP_PRIVATE_PAGE_ALLOWED_METHODS = {"Browser.getVersion", "Target.getTargets", "Target.attachToTarget", "Target.detachFromTarget",+ "Target.createTarget", "Target.activateTarget", "Target.closeTarget","Page.navigate", "Page.reload", "Page.stopLoading",}@@ -138,7 +140,7 @@ def _expression_private_target(params: dict[str, Any]) -> Optional[str]:def _navigate_private_target(params: dict[str, Any]) -> Optional[str]:- """Blocked URL literal for ``Page.navigate`` params, else ``None``."""+ """Blocked URL literal for ``Page.navigate`` / ``Target.createTarget`` params, else ``None``."""from tools.browser_url_policy import url_blockedtarget_url = str(params.get("url") or "").strip()return target_url if target_url and url_blocked(target_url) else None@@ -148,6 +150,8 @@ def _navigate_private_target(params: dict[str, Any]) -> Optional[str]:_METHOD_PARAM_GUARDS = {"Page.navigate": (_navigate_private_target,"Blocked: CDP Page.navigate target is a private or internal address ({})."),+ "Target.createTarget": (_navigate_private_target,+ "Blocked: CDP Target.createTarget url is a private or internal address ({})."),"Runtime.evaluate": (_expression_private_target,"Blocked: CDP Runtime.evaluate expression targets a private or internal address ({})."),}