Agent Zero AI framework
Reject browser evaluate calls without a script
Alessandro46ab911a3ec5
diff --git a/plugins/_browser/AGENTS.md b/plugins/_browser/AGENTS.mdindex e84beda58..b8a773b43 100644--- a/plugins/_browser/AGENTS.md+++ b/plugins/_browser/AGENTS.md@@ -49,6 +49,7 @@- Browser model-preset selection resolves omitted preset fields from `_model_config`'s global `Default` preset, not from an unrelated currently scoped model selection. After the first Browser tool call, use the selected preset for subsequent model turns in that monologue and clear it at monologue end.- Open-browser runtime state (tab listing, last interacted browser, page content) is per-loop dynamic context: it must flow through the `message_loop_prompts_after` extension into `loop_data.extras_temporary` (rendered as message extras), never into the system prompt, so the system prompt stays cache-stable across turns. The extension must check the profile Browser tool policy first and inject nothing when the tool is blocked.- Browser tool results serialize minified JSON for agent history; any JSON beautification for display stays plugin-local in the WebUI tool handler (`browser-tool-handler.js`), never in shared `helpers/tool.py` where it would apply to every tool.+- `evaluate` requires a non-empty `script` string in single and multi calls. Reject missing input instead of evaluating `undefined`; preserve legitimate JavaScript null/undefined results.- Clipboard `clipboard_action` reads legacy aliases `operation` and `event_type`; `a0_path` and `context_id` are screenshot result fields, not tool args. Document only canonical args in model-facing prompts.- Browser action values accept legacy spellings `setactive`/`activate`/`focus` for `set_active`, `typesubmit` for `type_submit`, and `keychord` for `key_chord`; prompts document only canonical action values.- Annotation mode highlights the DOM element under the pointer, keeps saved overlays page-local, and may batch annotated pages only within the active chat context.diff --git a/plugins/_browser/helpers/runtime.py b/plugins/_browser/helpers/runtime.pyindex a00f1622d..e6d586978 100644--- a/plugins/_browser/helpers/runtime.py+++ b/plugins/_browser/helpers/runtime.py@@ -1696,10 +1696,12 @@ class _BrowserRuntimeCore:return result or {}async def evaluate(self, browser_id: int | str | None, script: str) -> dict[str, Any]:+ if not isinstance(script, str) or not script.strip():+ raise ValueError("evaluate requires a non-empty 'script' string")await self.ensure_started()resolved_id = self._resolve_browser_id(browser_id)page = self._page(resolved_id)- result = await page.evaluate(str(script or "undefined"), isolated_context=False)+ result = await page.evaluate(script, isolated_context=False)self._maybe_promote(resolved_id)return {"result": result, "state": await self._state(resolved_id)}diff --git a/plugins/_browser/prompts/agent.system.tool.browser.md b/plugins/_browser/prompts/agent.system.tool.browser.mdindex 980d18580..a8b5c67a3 100644--- a/plugins/_browser/prompts/agent.system.tool.browser.md+++ b/plugins/_browser/prompts/agent.system.tool.browser.md@@ -13,3 +13,4 @@ Rules:- Screenshots are explicit only; the browser does not automatically load screenshots. Call `vision_load` with the returned `vision_load.tool_args.paths` value before reasoning visually.- Keep the tab set small; close pages after extracting what you need.- `multi` is only a browser action: use `tool_name: "browser"` with `tool_args.action: "multi"`. Never use `tool_name: "multi"`.+- `evaluate` requires `script`: a JavaScript expression or function whose result is returned.diff --git a/tests/test_browser_agent_regressions.py b/tests/test_browser_agent_regressions.pyindex bbab5942c..a8b0130a5 100644--- a/tests/test_browser_agent_regressions.py+++ b/tests/test_browser_agent_regressions.py@@ -111,7 +111,11 @@ sys.modules.setdefault()sys.modules.setdefault("helpers.tool", SimpleNamespace(Response=_TestResponse, Tool=_TestTool))sys.modules.setdefault("helpers.ws", SimpleNamespace(WsHandler=_TestWsHandler))-sys.modules.setdefault("helpers.ws_manager", SimpleNamespace(WsResult=_TestWsResult))+sys.modules.setdefault("helpers.ws_manager", SimpleNamespace(+ WsResult=_TestWsResult,+ WsPayloadTooLargeError=ValueError,+ get_shared_ws_manager=lambda: _TestWsManager(),+))_model_config_stub = ModuleType("plugins._model_config.helpers.model_config")_model_config_stub.get_presets = lambda: []_model_config_stub.get_preset_by_name = lambda name: Nonediff --git a/tests/test_browser_tool_output.py b/tests/test_browser_tool_output.pyindex 63fddd8db..dab0390e0 100644--- a/tests/test_browser_tool_output.py+++ b/tests/test_browser_tool_output.py@@ -1,6 +1,8 @@import jsonimport subprocessfrom pathlib import Path+from types import SimpleNamespace+from unittest.mock import AsyncMockimport pytest@@ -43,6 +45,34 @@ async def test_evaluate_uses_script_arg(monkeypatch) -> None:assert seen == ["1+1"][email protected]("args", [{"expression": "document.title"}, {}, {"script": " "}, {"script": 42}])+async def test_evaluate_rejects_missing_or_invalid_script(monkeypatch, args) -> None:+ core = _BrowserRuntimeCore("ctx")+ started = AsyncMock()+ monkeypatch.setattr(core, "ensure_started", started)+ with pytest.raises(ValueError, match="non-empty 'script'"):+ await core._dispatch_call({"action": "evaluate", **args})+ result = await core.multi([{"action": "evaluate", **args}])+ assert result == [{"ok": False, "error": "evaluate requires a non-empty 'script' string"}]+ started.assert_not_awaited()++[email protected]("value", [None, False, 0, {"answer": 42}])+async def test_evaluate_preserves_javascript_results(monkeypatch, value) -> None:+ core = _BrowserRuntimeCore("ctx")+ page = SimpleNamespace(evaluate=AsyncMock(return_value=value))+ monkeypatch.setattr(core, "ensure_started", AsyncMock())+ monkeypatch.setattr(core, "_resolve_browser_id", lambda _: 3)+ monkeypatch.setattr(core, "_page", lambda _: page)+ monkeypatch.setattr(core, "_state", AsyncMock(return_value={"id": 3}))+ script = "() => " + json.dumps(value)+ result = await core.evaluate(3, script)+ assert result == {"result": value, "state": {"id": 3}}+ page.evaluate.assert_awaited_once_with(script, isolated_context=False)++def test_webui_beautifier_formats_objects_and_arrays() -> None:source = (PROJECT_ROOT