Skip to content

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

Bridge BrowserProfile domain constraints

Magnus Müllerf5d3bba4ff6c
diff --git a/RUST_AGENT_INTEGRATION_FEATURES.md b/RUST_AGENT_INTEGRATION_FEATURES.md
index 4a2b03994..adf6682aa 100644
--- a/RUST_AGENT_INTEGRATION_FEATURES.md
+++ b/RUST_AGENT_INTEGRATION_FEATURES.md
@@ -95,10 +95,15 @@ Terminal core branch: `magnus/browser-use-rust-integration` at latest pulled mai
- CDP URLs still select `remote-cdp`, and explicit browser mode environment variables still override profile preferences.
- Proof: `test_rust_agent_translates_browser_profile_cloud`.
+18. BrowserProfile domain constraints bridge
+ - `BrowserProfile.allowed_domains` and `BrowserProfile.prohibited_domains` are now preserved on the Rust-backed agent and passed into the Rust terminal task as explicit navigation constraints.
+ - Domain lists preserve caller order, set values are made deterministic, and duplicate profile entries are ignored.
+ - Proof: `test_rust_agent_adds_browser_profile_domain_constraints`.
+
## Current Verification
- `python3 -m py_compile browser_use/rust/service.py browser_use/rust/__init__.py browser_use/__init__.py tests/ci/test_rust_agent.py examples/rust_agent/basic.py examples/rust_agent/real_v8_smoke.py`
-- `uv run pytest -q tests/ci/test_rust_agent.py` (26 tests)
+- `uv run pytest -q tests/ci/test_rust_agent.py` (27 tests)
- `cargo build -q -p browser-use-cli`
- `CARGO_INCREMENTAL=0 cargo test -q -p browser-use-agent selected_remote_cdp_mode_allows_remote_cdp_connect -- --nocapture`
- Managed-headless end-to-end:
diff --git a/browser_use/rust/service.py b/browser_use/rust/service.py
index e55be1cbd..eddfd120b 100644
--- a/browser_use/rust/service.py
+++ b/browser_use/rust/service.py
@@ -105,6 +105,35 @@ def _extract_cloud_preference(browser_session: BrowserSession | None, browser_pr
return False
+def _domain_list(value: Any) -> list[str]:
+ if value is None or isinstance(value, str):
+ return []
+ if isinstance(value, set):
+ items = sorted(value)
+ elif isinstance(value, list):
+ items = value
+ else:
+ return []
+ return [item for item in items if isinstance(item, str) and item]
+
+
+def _extract_profile_domains(
+ browser_session: BrowserSession | None,
+ browser_profile: BrowserProfile | None,
+ attr: str,
+) -> list[str]:
+ values: list[str] = []
+ seen: set[str] = set()
+ session_profile = getattr(browser_session, 'browser_profile', None)
+ for profile in (session_profile, browser_profile):
+ for domain in _domain_list(getattr(profile, attr, None)):
+ if domain in seen:
+ continue
+ values.append(domain)
+ seen.add(domain)
+ return values
+
+
def _navigation_url_from_action(action: Any) -> str | None:
if not isinstance(action, dict):
return None
@@ -151,6 +180,20 @@ def _task_with_available_files(task: str, available_file_paths: list[str] | None
return f'{task}\n\nAvailable local files:\n{files}'
+def _task_with_domain_constraints(task: str, allowed_domains: list[str], prohibited_domains: list[str]) -> str:
+ if not allowed_domains and not prohibited_domains:
+ return task
+ sections = ['Browser profile navigation constraints:']
+ if allowed_domains:
+ sections.append('Allowed domains:')
+ sections.extend(f'- {domain}' for domain in allowed_domains)
+ if prohibited_domains:
+ sections.append('Prohibited domains:')
+ sections.extend(f'- {domain}' for domain in prohibited_domains)
+ sections.append('Respect these BrowserProfile domain constraints when navigating.')
+ return f'{task}\n\n' + '\n'.join(sections)
+
+
def _extract_start_url(task: str) -> str | None:
task_without_emails = re.sub(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', '', task)
patterns = [
@@ -467,6 +510,8 @@ class Agent(Generic[AgentStructuredOutput]):
final_response_after_failure=final_response_after_failure,
)
self.available_file_paths = available_file_paths or []
+ self.allowed_domains = _extract_profile_domains(self.browser_session, self.browser_profile, 'allowed_domains')
+ self.prohibited_domains = _extract_profile_domains(self.browser_session, self.browser_profile, 'prohibited_domains')
self.display_files_in_done_text = display_files_in_done_text
self.file_system_path = file_system_path
self.directly_open_url = directly_open_url
@@ -480,7 +525,14 @@ class Agent(Generic[AgentStructuredOutput]):
initial_actions = [{'navigate': {'url': self.initial_url, 'new_tab': False}}]
self.initial_actions = initial_actions
self.task = _task_with_schema(
- _task_with_available_files(_task_with_initial_actions(task, initial_actions), self.available_file_paths),
+ _task_with_available_files(
+ _task_with_domain_constraints(
+ _task_with_initial_actions(task, initial_actions),
+ self.allowed_domains,
+ self.prohibited_domains,
+ ),
+ self.available_file_paths,
+ ),
output_model_schema,
)
self.session_id: str | None = None
diff --git a/tests/ci/test_rust_agent.py b/tests/ci/test_rust_agent.py
index 61353d17a..c2f12994c 100644
--- a/tests/ci/test_rust_agent.py
+++ b/tests/ci/test_rust_agent.py
@@ -188,6 +188,25 @@ def test_rust_agent_translates_browser_profile_cloud(monkeypatch):
assert agent._run_env()['LLM_BROWSER_BROWSER_MODE'] == 'cloud'
+def test_rust_agent_adds_browser_profile_domain_constraints():
+ from browser_use.rust import Agent
+
+ class BrowserProfile:
+ allowed_domains = ['example.com', '*.browser-use.com']
+ prohibited_domains = {'ads.example.com'}
+
+ agent = Agent(task='Open the allowed site.', browser_profile=BrowserProfile())
+
+ assert agent.allowed_domains == ['example.com', '*.browser-use.com']
+ assert agent.prohibited_domains == ['ads.example.com']
+ assert 'Browser profile navigation constraints:' in agent.task
+ assert 'Allowed domains:' in agent.task
+ assert '- example.com' in agent.task
+ assert '- *.browser-use.com' in agent.task
+ assert 'Prohibited domains:' in agent.task
+ assert '- ads.example.com' in agent.task
+
+
def test_rust_agent_mirrors_direct_url_startup():
from browser_use.rust import Agent