Skip to content

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

Python

fix(browser): centralize newline guard inside _q (Codex review)

apirenoe1190252adae
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
index 076fe7abf..275269013 100644
--- a/browser/agent-harness/cli_anything/browser/tests/test_domshell_backend.py
+++ b/browser/agent-harness/cli_anything/browser/tests/test_domshell_backend.py
@@ -165,3 +165,60 @@ def test_grep_rejects_newline_in_pattern():
def test_grep_rejects_newline_in_prev():
with pytest.raises(ValueError, match="newline"):
backend.grep("Login", path="/main", prev="/\nclick /admin")
+
+
+# ── Centralized newline guard in _q ──────────────────────────────────
+#
+# The per-wrapper _assert_single_line calls above cover type_text and
+# rooted grep with field-named error messages. The newline check inside
+# _q itself catches the same class of injection for every OTHER wrapper
+# that flows user input through the quoting layer (open_url, click, cd,
+# cat, unrooted grep, etc.) — without needing a per-call guard at each
+# site.
+
+
+def test_q_rejects_line_feeds():
+ with pytest.raises(ValueError, match="[Nn]ewline"):
+ backend._q("foo\nbar")
+
+
+def test_q_rejects_carriage_returns():
+ with pytest.raises(ValueError, match="[Nn]ewline"):
+ backend._q("foo\rbar")
+
+
+def test_q_accepts_normal_strings():
+ """Plain strings pass through to shlex.quote unchanged."""
+ assert backend._q("simple") == "simple"
+ assert backend._q("hello world") == "'hello world'"
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_unrooted_grep_pattern_rejects_newlines(mock_call):
+ """The unrooted grep path was not field-guarded — _q catches it."""
+ mock_call.return_value = {}
+ with pytest.raises(ValueError, match="[Nn]ewline"):
+ backend.grep("evil\nclick /admin")
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_open_url_rejects_newlines(mock_call):
+ """open_url has no per-call _assert_single_line — _q must catch it."""
+ mock_call.return_value = {}
+ with pytest.raises(ValueError, match="[Nn]ewline"):
+ backend.open_url("https://example.com\nclick /admin")
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_click_rejects_newlines(mock_call):
+ """click is covered structurally by _q without a per-call guard."""
+ mock_call.return_value = {}
+ with pytest.raises(ValueError, match="[Nn]ewline"):
+ backend.click("/main/button[0]\nclick /admin")
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_cd_rejects_newlines(mock_call):
+ mock_call.return_value = {}
+ with pytest.raises(ValueError, match="[Nn]ewline"):
+ backend.cd("/main\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 1f967ab55..3946c116d 100644
--- a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
+++ b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
@@ -117,7 +117,26 @@ def is_available() -> tuple[bool, str]:
def _q(arg: str) -> str:
- """Quote an argument for the DOMShell command parser (shell-style)."""
+ """Quote an argument for the DOMShell command parser (shell-style).
+
+ Rejects newlines: DOMShell's ``domshell_execute`` splits multi-line
+ input *before* shell-style quote parsing, so a ``\\n`` or ``\\r``
+ inside an otherwise-quoted argument still becomes a command
+ separator. Enforcing the check here means every wrapper that flows
+ user input through ``_q`` is protected by default, instead of
+ relying on per-call ``_assert_single_line`` at each call site.
+
+ Wrappers may still call ``_assert_single_line`` ahead of ``_q`` when a
+ field-named error message (e.g. ``"text: ..."``) is more useful than
+ the generic one raised here.
+ """
+ if "\n" in arg or "\r" in arg:
+ raise ValueError(
+ "Newline characters are not allowed in command arguments — "
+ "DOMShell's domshell_execute treats them as command separators, "
+ "so a newline inside any wrapper input would inject additional "
+ f"commands. Got: {arg!r}"
+ )
return shlex.quote(arg)