Skip to content

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

address codex review: validate tools.act(action_timeout=...) override

Saurav Panda32416bb48cf4
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index 74e89902e..ea0302fb3 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -121,6 +121,27 @@ def _parse_env_action_timeout(raw: str | None) -> float:
_DEFAULT_ACTION_TIMEOUT_S = _parse_env_action_timeout(os.getenv('BROWSER_USE_ACTION_TIMEOUT_S'))
+def _coerce_valid_action_timeout(value: float | None) -> float:
+ """Normalize a caller-supplied action_timeout to a finite positive value.
+
+ Mirrors the env-var guard so the public `tools.act(action_timeout=...)`
+ override path has the same defenses: nan / inf / <=0 make actions either
+ time out immediately or never, which would silently defeat the hang
+ guard this module exists to provide. Fall back to the env-derived
+ default with a warning instead.
+ """
+ if value is None:
+ return _DEFAULT_ACTION_TIMEOUT_S
+ if not math.isfinite(value) or value <= 0:
+ logging.getLogger(__name__).warning(
+ 'action_timeout=%r is not a finite positive number; falling back to %.0fs',
+ value,
+ _DEFAULT_ACTION_TIMEOUT_S,
+ )
+ return _DEFAULT_ACTION_TIMEOUT_S
+ return float(value)
+
+
def _detect_sensitive_key_name(text: str, sensitive_data: dict[str, str | dict[str, str]] | None) -> str | None:
"""Detect which sensitive key name corresponds to the given text value."""
if not sensitive_data or not text:
@@ -2099,7 +2120,7 @@ Validated Code (after quote fixing):
page_extraction_llm cap used by the `extract` action).
"""
- timeout_s = action_timeout if action_timeout is not None else _DEFAULT_ACTION_TIMEOUT_S
+ timeout_s = _coerce_valid_action_timeout(action_timeout)
for action_name, params in action.model_dump(exclude_unset=True).items():
if params is not None:
diff --git a/tests/ci/test_action_timeout.py b/tests/ci/test_action_timeout.py
index 29f90f34e..1341b454a 100644
--- a/tests/ci/test_action_timeout.py
+++ b/tests/ci/test_action_timeout.py
@@ -90,6 +90,36 @@ async def test_act_passes_through_fast_handler():
assert result.extracted_content == 'done'
+async def test_act_rejects_invalid_action_timeout_override():
+ """An invalid action_timeout override (nan / inf / <=0) must fall back to
+ the default, not silently defeat the timeout (nan → immediate timeout,
+ inf → no timeout at all)."""
+ tools = Tools()
+
+ calls = {'n': 0}
+
+ async def _fast_execute_action(**_kwargs):
+ calls['n'] += 1
+ return ActionResult(extracted_content='done')
+
+ tools.registry.execute_action = _fast_execute_action # type: ignore[assignment]
+
+ # nan would otherwise produce an immediate TimeoutError; we expect the
+ # coercion to fall back to the default, so the fast handler runs to
+ # completion and returns the success result.
+ action = _StubActionModel(fast_action={'x': 1})
+ result = await tools.act(action=action, browser_session=None, action_timeout=float('nan')) # type: ignore[arg-type]
+ assert calls['n'] == 1
+ assert result.error is None
+ assert result.extracted_content == 'done'
+
+ # inf / non-positive values also fall back cleanly.
+ for bad in (float('inf'), 0.0, -5.0):
+ result = await tools.act(action=action, browser_session=None, action_timeout=bad) # type: ignore[arg-type]
+ assert result.error is None, f'override {bad!r} should have fallen back'
+
+
def test_default_action_timeout_accommodates_extract_action():
"""The module-level default must sit above extract's 120s LLM inner cap."""
from browser_use.tools.service import _DEFAULT_ACTION_TIMEOUT_S