Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix(tools): refuse evaluate() on restricted browser profiles

Saurav Pandaa7ce6809489d
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index cf999b849..197f90d2e 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -1775,8 +1775,22 @@ You will be given a query and the markdown of a webpage that has been filtered t
terminates_sequence=True,
)
async def evaluate(code: str, browser_session: BrowserSession):
- # Execute JavaScript with proper error handling and promise support
+ # Refuse on restricted profiles. SecurityWatchdog only hooks navigation,
+ # so evaluate() would otherwise let the agent fetch() internal URLs or
+ # read cookies/localStorage from any allowed origin's context, nullifying
+ # allowed_domains / block_ip_addresses entirely.
+ profile = browser_session.browser_profile
+ if profile.allowed_domains or profile.block_ip_addresses:
+ return ActionResult(
+ error=(
+ 'evaluate() is disabled when allowed_domains or block_ip_addresses '
+ 'is set — this profile is configured as restricted, and evaluate() '
+ 'would let an agent bypass those restrictions via fetch() and direct '
+ 'origin access. Run on an unrestricted profile if you need JS evaluation.'
+ )
+ )
+ # Execute JavaScript with proper error handling and promise support
cdp_session = await browser_session.get_or_create_cdp_session()
try:
diff --git a/tests/ci/security/test_evaluate_restricted.py b/tests/ci/security/test_evaluate_restricted.py
new file mode 100644
index 000000000..f50b87a4e
--- /dev/null
+++ b/tests/ci/security/test_evaluate_restricted.py
@@ -0,0 +1,101 @@
+"""Tests for the evaluate() guard on restricted browser profiles.
+
+The agent's `evaluate()` action calls `Runtime.evaluate` directly through CDP.
+SecurityWatchdog only subscribes to navigation events, so JS running inside an
+already-allowed page can `fetch()` arbitrary internal URLs, read cookies and
+localStorage from any allowed origin's context, and otherwise act as if the
+restrictions weren't there.
+
+On profiles configured with `allowed_domains` or `block_ip_addresses`, the
+operator has signalled "this agent is constrained" — exposing an unmediated
+JS evaluation primitive contradicts that signal. The guard refuses
+`evaluate()` outright on such profiles; agents that need JS can be run on an
+unrestricted profile or the gate can be lifted explicitly later.
+"""
+
+from __future__ import annotations
+
+from typing import Any
+
+import pytest
+
+from browser_use.agent.views import ActionResult
+from browser_use.tools.service import Tools
+
+
+class _StubProfile:
+ def __init__(self, allowed_domains: Any = None, block_ip_addresses: bool = False) -> None:
+ self.allowed_domains = allowed_domains
+ self.block_ip_addresses = block_ip_addresses
+
+
+class _StubBrowserSession:
+ is_local = True
+ agent_focus_target_id: str | None = None
+ session_manager: Any = None
+ cdp_client: Any = None
+
+ def __init__(self, profile: _StubProfile) -> None:
+ self.browser_profile = profile
+
+ async def get_current_page_url(self) -> str:
+ return 'about:blank'
+
+ async def get_or_create_cdp_session(self) -> Any:
+ raise AssertionError('evaluate() must refuse before reaching CDP when restrictions are configured')
+
+
+async def _run_evaluate(profile: _StubProfile, code: str = '1 + 1') -> ActionResult:
+ tools = Tools()
+ result = await tools.registry.execute_action(
+ 'evaluate',
+ {'code': code},
+ browser_session=_StubBrowserSession(profile), # type: ignore[arg-type]
+ )
+ assert isinstance(result, ActionResult)
+ return result
+
+
+async def test_evaluate_refused_when_allowed_domains_set() -> None:
+ """A non-empty `allowed_domains` list signals a restricted profile — refuse."""
+ result = await _run_evaluate(_StubProfile(allowed_domains=['example.com']))
+ assert result.error is not None
+ assert 'allowed_domains' in result.error.lower() or 'restricted' in result.error.lower()
+
+
+async def test_evaluate_refused_when_block_ip_addresses_set() -> None:
+ """`block_ip_addresses=True` signals a restricted profile — refuse."""
+ result = await _run_evaluate(_StubProfile(block_ip_addresses=True))
+ assert result.error is not None
+ assert 'block_ip_addresses' in result.error.lower() or 'restricted' in result.error.lower()
+
+
+async def test_evaluate_refused_when_both_restrictions_set() -> None:
+ """Belt and suspenders configuration — refuse."""
+ result = await _run_evaluate(_StubProfile(allowed_domains=['x.test'], block_ip_addresses=True))
+ assert result.error is not None
+
+
+async def test_evaluate_reaches_cdp_when_unrestricted() -> None:
+ """No restrictions → evaluate() should proceed to CDP. Our stub raises
+ when `get_or_create_cdp_session` is called, so we observe via the wrapped
+ error that the guard did not short-circuit."""
+ tools = Tools()
+ with pytest.raises(RuntimeError, match='must refuse before reaching CDP'):
+ await tools.registry.execute_action(
+ 'evaluate',
+ {'code': '1 + 1'},
+ browser_session=_StubBrowserSession(_StubProfile()), # type: ignore[arg-type]
+ )
+
+
+async def test_evaluate_proceeds_when_allowed_domains_is_empty_list() -> None:
+ """Empty allowed_domains is treated as 'no restriction' elsewhere in the
+ codebase (e.g. SecurityWatchdog). Be consistent: don't refuse evaluate()."""
+ tools = Tools()
+ with pytest.raises(RuntimeError, match='must refuse before reaching CDP'):
+ await tools.registry.execute_action(
+ 'evaluate',
+ {'code': '1 + 1'},
+ browser_session=_StubBrowserSession(_StubProfile(allowed_domains=[])), # type: ignore[arg-type]
+ )