The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(browser): apply safety checks to browser_exec URLs
Laith Weinberger9e5e1740ed5e
diff --git a/tools/browser_tool.py b/tools/browser_tool.pyindex 05d4059c30a..f2b0ac13144 100644--- a/tools/browser_tool.py+++ b/tools/browser_tool.py@@ -2947,6 +2947,37 @@ def _redact_browser_output(value: Any) -> Any:# Browser Tool Functions# ============================================================================+def evaluate_url_safety(url: str) -> Optional[dict]:+ """Run URL safety checks; None if safe, else an error dict"""+ import urllib.parse+ from agent.redact import _PREFIX_RE++ _secret = {"success": False, "error": "Blocked: URL contains what appears to be an API key or token. Secrets must not be sent in URLs."}+ if _PREFIX_RE.search(url) or _PREFIX_RE.search(urllib.parse.unquote(url)):+ return _secret+ url = _normalize_url_for_request(url)+ if _PREFIX_RE.search(url) or _PREFIX_RE.search(urllib.parse.unquote(url)):+ return _secret++ local = _is_local_backend()+ sensitive_query_key = _sensitive_query_param_name(url)+ if sensitive_query_key and not local:+ return {"success": False, "error": (+ "Blocked: URL contains a credential-like query parameter "+ f"({sensitive_query_key}). Cloud browser backends are third-party "+ "readers; use a local browser/CDP session or remove the sensitive "+ "query parameter before navigating.")}+ if _is_always_blocked_url(url):+ return {"success": False, "error": "Blocked: URL targets a cloud metadata endpoint"}+ if not local and not _allow_private_urls() and not _is_safe_url(url):+ return {"success": False, "error": "Blocked: URL targets a private or internal address"}+ blocked = check_website_access(url)+ if blocked:+ return {"success": False, "error": blocked["message"],+ "blocked_by_policy": {"host": blocked["host"], "rule": blocked["rule"], "source": blocked["source"]}}+ return None++def browser_navigate(url: str, task_id: Optional[str] = None) -> str:"""Navigate to a URL in the browser.diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.pyindex a3a95d57def..ff5479bac8d 100644--- a/tools/browser_use_cli.py+++ b/tools/browser_use_cli.py@@ -34,6 +34,20 @@ _TASK_ID_SAFE_RE = re.compile(r"[^A-Za-z0-9._-]+")# Screenshot paths printed by capture_screenshot() in the exec output_IMAGE_PATH_RE = re.compile(r"(/[^\s\"']+?\.(?:png|jpe?g|webp))", re.IGNORECASE)+# http(s) URL literals in exec code checked against browser_navigate's policy+_URL_RE = re.compile(r"https?://[^\s'\"\\)]+", re.IGNORECASE)+++def _blocked_url_in_code(code: str) -> Optional[str]:+ """Return an error if a URL literal fails the built-in navigation checks."""+ from tools.browser_tool import evaluate_url_safety++ for url in _URL_RE.findall(code or ""):+ err = evaluate_url_safety(url)+ if err:+ return err.get("error", "Blocked: unsafe URL")+ return None+def _base_subprocess_env() -> dict:from tools.browser_tool import _build_browser_env@@ -175,6 +189,10 @@ def browser_exec(if not code or not code.strip():return tool_error("No code provided. Pass Python that uses the pre-imported helpers, e.g. new_tab(\"https://example.com\") then print(page_info()).")+ blocked = _blocked_url_in_code(code)+ if blocked:+ return tool_error(blocked)+cmd = _find_cli()if not cmd:return tool_error(