Skip to content

Build and run agents you can see, understand and trust.

fix(tool): reject mutating find commands as read-only (#2004)

VectorPeak7295c985e20c
diff --git a/src/agentscope/tool/_builtin/_bash_parser.py b/src/agentscope/tool/_builtin/_bash_parser.py
index 62d6f1a81..17cdbc0a8 100644
--- a/src/agentscope/tool/_builtin/_bash_parser.py
+++ b/src/agentscope/tool/_builtin/_bash_parser.py
@@ -132,6 +132,18 @@ READ_ONLY_COMMANDS = {
"pip show",
}
+FIND_MUTATING_PREDICATES = {
+ "-delete",
+ "-exec",
+ "-execdir",
+ "-fls",
+ "-fprint",
+ "-fprint0",
+ "-fprintf",
+ "-ok",
+ "-okdir",
+}
+
class BashCommandParser:
"""Parse Bash commands using tree-sitter for accurate syntax analysis."""
@@ -199,6 +211,9 @@ class BashCommandParser:
if cmd in READ_ONLY_COMMANDS:
return True
+ if self._is_mutating_find_command(cmd):
+ return False
+
# Check if it starts with a read-only prefix
for readonly_cmd in READ_ONLY_COMMANDS:
if cmd == readonly_cmd or cmd.startswith(readonly_cmd + " "):
@@ -221,6 +236,30 @@ class BashCommandParser:
return False
+ def _is_mutating_find_command(self, cmd: str) -> bool:
+ """Check if a find command contains mutating predicates via AST."""
+ try:
+ tree = self.parser.parse(bytes(cmd, "utf8"))
+ except Exception:
+ return False
+
+ root = tree.root_node
+ cmd_node = self._find_first_simple_command(root)
+ if cmd_node is None:
+ return False
+
+ name_node = cmd_node.child_by_field_name("name")
+ if name_node is None or name_node.text.decode("utf8") != "find":
+ return False
+
+ for child in cmd_node.children:
+ if child.type == "word":
+ text = child.text.decode("utf8")
+ if text in FIND_MUTATING_PREDICATES:
+ return True
+
+ return False
+
def extract_file_paths(
self,
command: str,
diff --git a/tests/builtin_bash_test.py b/tests/builtin_bash_test.py
index d78bc0956..01a8584ba 100644
--- a/tests/builtin_bash_test.py
+++ b/tests/builtin_bash_test.py
@@ -217,12 +217,43 @@ class BashToolInjectionCheckTest(IsolatedAsyncioTestCase):
self.assertEqual(decision.behavior, PermissionBehavior.ASK)
self.assertIn("command_substitution", decision.message)
+ async def test_mutating_find_commands_not_auto_allowed(self) -> None:
+ """Test mutating find commands are not auto-allowed as read-only."""
+ test_cases = [
+ "find . -delete",
+ "find . -name '*.tmp' -delete",
+ "find . -daystart -delete",
+ "find . -nogroup -delete",
+ "find . -nouser -delete",
+ "find . -exec rm {} \\;",
+ "find . -execdir rm {} \\;",
+ "find . -fls results.txt",
+ "find . -fprint results.txt",
+ "find . -fprint0 results.txt",
+ "find . -fprintf results.txt '%p\\n'",
+ "find . -ok rm {} \\;",
+ "find . -okdir rm {} \\;",
+ ]
+ for cmd in test_cases:
+ with self.subTest(cmd=cmd):
+ decision = await self.bash_tool.check_permissions(
+ {"command": cmd},
+ self.context,
+ )
+ self.assertNotEqual(
+ decision.behavior,
+ PermissionBehavior.ALLOW,
+ )
+
async def test_safe_commands_pass(self) -> None:
"""Test that safe commands pass injection check."""
safe_commands = [
"ls -la",
"cat file.txt",
+ "find . -name '-delete'",
+ "find . -path './-fprint'",
+ "find . -regex '.*-exec.*'",
"git status",
"echo 'hello world'",
]
diff --git a/tests/permission_bash_parser_test.py b/tests/permission_bash_parser_test.py
index 4c20dc287..975bb5c9b 100644
--- a/tests/permission_bash_parser_test.py
+++ b/tests/permission_bash_parser_test.py
@@ -244,6 +244,9 @@ class BashParserReadOnlyTest(IsolatedAsyncioTestCase):
"tail -f log.txt",
"grep pattern file.txt",
"find . -name '*.py'",
+ "find . -name '-delete'",
+ "find . -path './-fprint'",
+ "find . -regex '.*-exec.*'",
"tree",
"pwd",
"which python",
@@ -255,6 +258,30 @@ class BashParserReadOnlyTest(IsolatedAsyncioTestCase):
f"Expected '{cmd}' to be read-only",
)
+ async def test_mutating_find_commands_are_not_read_only(self) -> None:
+ """Test find predicates that can modify files are not read-only."""
+ mutating_find_commands = [
+ "find . -delete",
+ "find . -name '*.tmp' -delete",
+ "find . -daystart -delete",
+ "find . -nogroup -delete",
+ "find . -nouser -delete",
+ "find . -exec rm {} \\;",
+ "find . -execdir rm {} \\;",
+ "find . -fls results.txt",
+ "find . -fprint results.txt",
+ "find . -fprint0 results.txt",
+ "find . -fprintf results.txt '%p\\n'",
+ "find . -ok rm {} \\;",
+ "find . -okdir rm {} \\;",
+ ]
+ for cmd in mutating_find_commands:
+ with self.subTest(cmd=cmd):
+ self.assertFalse(
+ self.parser.is_read_only_command(cmd),
+ f"Expected '{cmd}' to be non-read-only",
+ )
+
async def test_single_read_only_docker_commands(self) -> None:
"""Test single read-only docker commands."""
read_only_commands = [