Skip to content

"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/

Python

fix(browser): address PR #308 review — keyword-only grep, finally-cwd, newline guard, wire tests

apirenoe43e738dae3b
diff --git a/browser/agent-harness/cli_anything/browser/tests/test_domshell_backend.py b/browser/agent-harness/cli_anything/browser/tests/test_domshell_backend.py
new file mode 100644
index 000000000..076fe7abf
--- /dev/null
+++ b/browser/agent-harness/cli_anything/browser/tests/test_domshell_backend.py
@@ -0,0 +1,167 @@
+"""Wire-format tests for cli_anything.browser.utils.domshell_backend.
+
+These tests patch the async ``_call_execute`` helper and assert the exact
+command string sent to the DOMShell ``domshell_execute`` tool, so wire-format
+regressions (quoting, command names, multi-line layout, restore ordering)
+fail loudly.
+"""
+
+import pytest
+from unittest.mock import AsyncMock, call, patch
+
+from cli_anything.browser.utils import domshell_backend as backend
+
+
+# ── grep: command string and call sequencing ──────────────────────────
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_grep_unrooted_produces_single_grep_call(mock_call):
+ """Unrooted grep dispatches one ``grep <pattern>`` call."""
+ mock_call.return_value = {}
+
+ backend.grep("Login")
+
+ assert mock_call.call_args_list == [call("grep Login", False)]
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_grep_rooted_produces_three_calls_in_order(mock_call):
+ """Rooted grep dispatches cd → grep → cd-back, in that order."""
+ mock_call.return_value = {}
+
+ backend.grep("Login", path="/main", prev="/")
+
+ assert mock_call.call_args_list == [
+ call("cd /main", False),
+ call("grep Login", False),
+ call("cd /", False),
+ ]
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_grep_rooted_restores_cwd_when_grep_raises(mock_call):
+ """If grep errors mid-flight, the trailing cd-back still runs (try/finally)."""
+ mock_call.side_effect = [
+ {}, # cd /main → success
+ RuntimeError("grep blew up"), # grep → raise
+ {}, # cd / (restore) — required
+ ]
+
+ with pytest.raises(RuntimeError, match="grep blew up"):
+ backend.grep("Login", path="/main", prev="/")
+
+ assert mock_call.call_count == 3
+ assert mock_call.call_args_list[-1] == call("cd /", False)
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_grep_rooted_short_circuits_when_cd_errors(mock_call):
+ """If the initial cd reports an error, grep is skipped and no restore is sent."""
+ mock_call.return_value = {"isError": True, "error": "no such path"}
+
+ result = backend.grep("Login", path="/missing", prev="/")
+
+ # Only the initial cd ran; grep + restore were skipped.
+ assert mock_call.call_args_list == [call("cd /missing", False)]
+ assert result == {"isError": True, "error": "no such path"}
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_grep_rooted_quotes_path_with_spaces(mock_call):
+ """Paths with whitespace are shell-quoted before interpolation."""
+ mock_call.return_value = {}
+
+ backend.grep("Login", path="/path with spaces", prev="/")
+
+ cd_cmd = mock_call.call_args_list[0].args[0]
+ # shlex.quote single-quotes anything containing whitespace.
+ assert cd_cmd == "cd '/path with spaces'"
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_grep_pattern_with_shell_metacharacters_quoted(mock_call):
+ """Patterns with shell metacharacters get quoted (no injection via grep)."""
+ mock_call.return_value = {}
+
+ backend.grep("$(rm -rf /)")
+
+ grep_cmd = mock_call.call_args_list[0].args[0]
+ # shlex.quote will single-quote the dangerous payload.
+ assert grep_cmd == "grep '$(rm -rf /)'"
+
+
+def test_grep_rejects_positional_path():
+ """grep(pattern, path) — positional path raises TypeError.
+
+ Pre-migration callers writing ``grep("Login", True)`` to mean
+ ``use_daemon=True`` must not silently get ``path=True``.
+ """
+ with pytest.raises(TypeError):
+ backend.grep("Login", True) # type: ignore[misc]
+
+
+def test_grep_rejects_positional_use_daemon():
+ """Even the third positional slot is blocked."""
+ with pytest.raises(TypeError):
+ backend.grep("Login", "/main", "/", True) # type: ignore[misc]
+
+
+def test_grep_keyword_use_daemon_still_works():
+ """Keyword call against the new signature still type-checks at call time."""
+ with patch.object(backend, "_call_execute", new_callable=AsyncMock) as mock_call:
+ mock_call.return_value = {}
+ backend.grep("Login", use_daemon=True)
+ assert mock_call.call_args_list == [call("grep Login", True)]
+
+
+# ── type_text: focus+type pairing and newline injection guard ─────────
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_type_text_emits_focus_then_type_in_one_call(mock_call):
+ """type_text builds a single multi-line ``focus … \\ntype …`` execute call."""
+ mock_call.return_value = {}
+
+ backend.type_text("search_input", "machine learning")
+
+ assert mock_call.call_args_list == [
+ call("focus search_input\ntype 'machine learning'", False),
+ ]
+
+
+def test_type_text_rejects_newline_in_text():
+ """``\\n`` in text would inject a new DOMShell command — must raise."""
+ with pytest.raises(ValueError, match="newline"):
+ backend.type_text("search_input", "line1\nline2")
+
+
+def test_type_text_rejects_carriage_return_in_text():
+ """``\\r`` is just as dangerous as ``\\n`` for DOMShell's line splitter."""
+ with pytest.raises(ValueError, match="newline"):
+ backend.type_text("search_input", "line1\rline2")
+
+
+def test_type_text_rejects_newline_in_path():
+ """A newline in the path argument also injects — guard both fields."""
+ with pytest.raises(ValueError, match="newline"):
+ backend.type_text("input\nclick /admin", "anything")
+
+
+# ── grep: newline guard on rooted multi-step path ─────────────────────
+
+
+def test_grep_rejects_newline_in_path():
+ """Rooted grep interpolates path into a multi-line cd/grep/cd — reject newlines."""
+ with pytest.raises(ValueError, match="newline"):
+ backend.grep("Login", path="/main\nclick /admin", prev="/")
+
+
+def test_grep_rejects_newline_in_pattern():
+ with pytest.raises(ValueError, match="newline"):
+ backend.grep("Login\nclick /admin", path="/main", prev="/")
+
+
+def test_grep_rejects_newline_in_prev():
+ with pytest.raises(ValueError, match="newline"):
+ backend.grep("Login", path="/main", prev="/\nclick /admin")
diff --git a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
index 6f9fb8799..1f967ab55 100644
--- a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
+++ b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
@@ -121,6 +121,46 @@ def _q(arg: str) -> str:
return shlex.quote(arg)
+def _assert_single_line(field: str, value: str) -> None:
+ """Reject newline characters in a user-supplied string.
+
+ DOMShell's ``domshell_execute`` splits its ``command`` argument on
+ newlines *before* shell-style quote parsing, so a literal ``\\n`` or
+ ``\\r`` inside an otherwise-quoted argument escapes the quoting and
+ starts a fresh DOMShell command. Guard at the wrapper layer for any
+ value that gets interpolated into a multi-line command string.
+ """
+ if "\n" in value or "\r" in value:
+ raise ValueError(
+ f"{field}: newline characters are not allowed (would be interpreted "
+ f"as DOMShell command separators). Got: {value!r}"
+ )
+
+
+def _is_error(result: Any) -> bool:
+ """Best-effort check that a ``domshell_execute`` result represents an error.
+
+ Inspects ``isError`` if the MCP SDK populated it; otherwise scans the
+ concatenated text content for a leading "error". Robust to both the
+ raw ``CallToolResult`` object and the dict shapes used in unit tests.
+ """
+ if hasattr(result, "isError") and result.isError:
+ return True
+ if isinstance(result, dict):
+ if result.get("isError"):
+ return True
+ if "error" in result:
+ return True
+ text = ""
+ content = getattr(result, "content", None)
+ if content:
+ for c in content:
+ piece = getattr(c, "text", None)
+ if piece:
+ text += piece
+ return text.strip().lower().startswith("error")
+
+
async def _call_execute(command: str, use_daemon: bool = False) -> Any:
"""Run a DOMShell command via the single `domshell_execute` MCP tool.
@@ -294,6 +334,7 @@ def cat(path: str, use_daemon: bool = False) -> dict:
def grep(
pattern: str,
+ *,
path: str = "",
prev: str = "/",
use_daemon: bool = False,
@@ -301,9 +342,16 @@ def grep(
"""Search for pattern in the accessibility tree.
When ``path`` is provided and is not ``/``, the search is rooted at that
- path: ``cd`` into it, ``grep``, then ``cd`` back to ``prev`` — sent as one
- multi-line ``domshell_execute`` call so all three steps share shell state
- and complete in a single MCP round-trip.
+ path: ``cd`` into it, ``grep``, then ``cd`` back to ``prev``. The three
+ steps run as separate ``domshell_execute`` calls with the restore in a
+ ``finally`` block, so the cwd is restored even if ``grep`` errors
+ mid-flight. (A single multi-line call would be one round-trip instead of
+ three, but it relies on DOMShell's currently-undocumented continue-past-
+ error behavior — see PR review.)
+
+ ``path``, ``prev``, and ``use_daemon`` are keyword-only to prevent silent
+ breakage of callers written against the pre-migration positional
+ signature ``grep(pattern, use_daemon)``.
Args:
pattern: Text pattern to search for
@@ -323,10 +371,17 @@ def grep(
{"matches": ["/main/button[0]"]}
"""
if path and path != "/":
- command = f"cd {_q(path)}\ngrep {_q(pattern)}\ncd {_q(prev)}"
- else:
- command = f"grep {_q(pattern)}"
- return asyncio.run(_call_execute(command, use_daemon))
+ _assert_single_line("path", path)
+ _assert_single_line("prev", prev)
+ _assert_single_line("pattern", pattern)
+ cd_result = asyncio.run(_call_execute(f"cd {_q(path)}", use_daemon))
+ if _is_error(cd_result):
+ return cd_result
+ try:
+ return asyncio.run(_call_execute(f"grep {_q(pattern)}", use_daemon))
+ finally:
+ asyncio.run(_call_execute(f"cd {_q(prev)}", use_daemon))
+ return asyncio.run(_call_execute(f"grep {_q(pattern)}", use_daemon))
def click(path: str, use_daemon: bool = False) -> dict:
@@ -413,7 +468,15 @@ def type_text(path: str, text: str, use_daemon: bool = False) -> dict:
Returns:
Dict with action result
+
+ Raises:
+ ValueError: If ``path`` or ``text`` contains a newline. DOMShell's
+ ``domshell_execute`` treats newlines as command separators, so
+ an embedded newline would inject additional commands. Split
+ into multiple ``type_text`` calls for multi-line input.
"""
+ _assert_single_line("path", path)
+ _assert_single_line("text", text)
command = f"focus {_q(path)}\ntype {_q(text)}"
return asyncio.run(_call_execute(command, use_daemon))