The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(browser): raw CDP's metadata floor covers Runtime.evaluate and url params when the guard is off
Teknium1fdfdecbcf9d
diff --git a/tests/tools/test_browser_eval_ssrf.py b/tests/tools/test_browser_eval_ssrf.pyindex 04f2922f296..cb68f3475cc 100644--- a/tests/tools/test_browser_eval_ssrf.py+++ b/tests/tools/test_browser_eval_ssrf.py@@ -93,6 +93,11 @@ def test_raw_cdp_keeps_the_metadata_floor_when_the_private_guard_is_off(monkeypamonkeypatch.setattr(browser_session_cloud, "is_local_backend", lambda: local_backend)monkeypatch.setattr(browser_session_cloud, "allow_private_urls", lambda: allow_private)guard = browser_cdp_tool._browser_cdp_private_guard- for method in ("Page.navigate", "Target.createTarget"):+ for method in ("Page.navigate", "Target.createTarget", "Network.loadNetworkResource", "Fetch.continueRequest"):assert "cloud metadata" in guard(method=method, params={"url": "http://169.254.169.254/latest/meta-data/"})assert guard(method=method, params={"url": "http://192.168.1.5/"}) is None+ for template in ("location.href='{}'", "fetch('{}').then(r => r.text())"):+ metadata = {"expression": template.format("http://169.254.169.254/latest/meta-data/")}+ assert "cloud metadata" in guard(method="Runtime.evaluate", params=metadata)+ lan = {"expression": template.format("http://192.168.1.5/")}+ assert guard(method="Runtime.evaluate", params=lan) is Nonediff --git a/tools/browser_cdp_tool.py b/tools/browser_cdp_tool.pyindex 7e41336f859..bdd8d5f7df8 100644--- a/tools/browser_cdp_tool.py+++ b/tools/browser_cdp_tool.py@@ -11,7 +11,7 @@ from __future__ import annotationsimport asyncioimport jsonimport logging-from typing import Any, Dict, Optional+from typing import Any, Callable, Dict, Optionalfrom tools.registry import registry, tool_error@@ -134,28 +134,27 @@ def _blocked(message: str, method: str) -> str:return tool_error(message, method=method, cdp_docs=CDP_DOCS_URL)-def _expression_private_target(params: dict[str, Any]) -> Optional[str]:+def _expression_private_target(params: dict[str, Any], blocked: Callable[[str], bool]) -> Optional[str]:from tools.browser_url_policy import expression_targets_private_url- return expression_targets_private_url(str(params.get("expression") or ""))+ return expression_targets_private_url(str(params.get("expression") or ""), blocked=blocked)-def _navigate_private_target(params: dict[str, Any]) -> Optional[str]:+def _navigate_private_target(params: dict[str, Any], blocked: Callable[[str], bool]) -> Optional[str]:"""Blocked URL literal for ``Page.navigate`` 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+ return target_url if target_url and blocked(target_url) else None-def _new_tab_private_target(params: dict[str, Any]) -> Optional[str]:+def _new_tab_private_target(params: dict[str, Any], blocked: Callable[[str], bool]) -> Optional[str]:"""``Target.createTarget``: a blank tab (``about:blank``, Chrome's default for no url) reaches nothing, soit opens before scripts are installed or a page is chosen; any other url is held to ``Page.navigate``'srule (``url_blocked`` refuses every other non-http scheme)."""if str(params.get("url") or "").strip().lower() == "about:blank":return None- return _navigate_private_target(params)+ return _navigate_private_target(params, blocked)-# method → (probe(params) -> blocked literal | None, error template)+# method → (probe(params, blocked) -> blocked literal | None, error template)_METHOD_PARAM_GUARDS = {"Page.navigate": (_navigate_private_target,"Blocked: CDP Page.navigate target is a private or internal address ({})."),@@ -205,20 +204,20 @@ def _browser_cdp_private_guard(*, method: str, params: dict[str, Any], endpoint:Raw CDP shares the cloud/private-network boundary of ``browser_exec``'s URL policy and must not becomeits bypass, so a page whose URL cannot be verified as public refuses page-reading methods."""- from tools.browser_url_policy import eval_ssrf_guard_active- if not eval_ssrf_guard_active():- # The cloud-metadata floor stays unconditional, as in browser_exec's _url_policy_error (#16234).- target_url = str((params or {}).get("url") or "").strip()- if method in ("Page.navigate", "Target.createTarget") and target_url:- from tools.url_safety import is_always_blocked_url- if is_always_blocked_url(target_url):- return _blocked(f"Blocked: CDP {method} url targets a cloud metadata endpoint ({target_url}).",- method)- return None+ from tools.browser_url_policy import eval_ssrf_guard_active, url_blockedguard = _METHOD_PARAM_GUARDS.get(method)+ if not eval_ssrf_guard_active():+ # The cloud-metadata floor stays unconditional, as in browser_exec's _url_policy_error (#16234):+ # every guarded method's probe, plus the ``url`` param any other method takes+ # (Network.loadNetworkResource, Fetch.continueRequest).+ from tools.url_safety import is_always_blocked_url+ probe = guard[0] if guard is not None else _navigate_private_target+ literal = probe(params or {}, is_always_blocked_url)+ return _blocked(f"Blocked: CDP {method} targets a cloud metadata endpoint ({literal}).",+ method) if literal else Noneif guard is not None:probe, template = guard- literal = probe(params or {})+ literal = probe(params or {}, url_blocked)if literal:return _blocked(template.format(literal), method)if method not in _CDP_PRIVATE_PAGE_ALLOWED_METHODS:diff --git a/tools/browser_url_policy.py b/tools/browser_url_policy.pyindex 5a1df5f1abd..13c67eec2a8 100644--- a/tools/browser_url_policy.py+++ b/tools/browser_url_policy.py@@ -13,7 +13,7 @@ import loggingimport reimport socketimport urllib.parse-from typing import Optional+from typing import Callable, Optionalfrom tools.url_safety import (_is_declared_fake_ip,@@ -136,7 +136,7 @@ def url_blocked(url: str) -> bool:return is_always_blocked_url(url) or not is_safe_url(url)-def expression_targets_private_url(expression: str) -> Optional[str]:- """The first private/always-blocked ``http(s)://`` literal in a JS expression (best-effort), else None."""+def expression_targets_private_url(expression: str, *, blocked: Callable[[str], bool] = url_blocked) -> Optional[str]:+ """The first ``http(s)://`` literal in a JS expression that ``blocked`` refuses (best-effort), else None."""literals = _JS_URL_LITERAL_RE.findall(expression) if isinstance(expression, str) else []- return next((c for c in (m.rstrip(".,;") for m in literals) if url_blocked(c)), None)+ return next((c for c in (m.rstrip(".,;") for m in literals) if blocked(c)), None)