Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
test(tool): add regression coverage for bash read-only permissions (#3474)
ChengFacc400f9dc6f0
diff --git a/src/agentscope/tool/_builtin/_bash_parser.py b/src/agentscope/tool/_builtin/_bash_parser.pyindex ea27e536b..9d1320fea 100644--- a/src/agentscope/tool/_builtin/_bash_parser.py+++ b/src/agentscope/tool/_builtin/_bash_parser.py@@ -151,8 +151,8 @@ class BashCommandParser:def is_read_only_command(self, command: str) -> bool:"""Check if a command is read-only (safe to auto-allow).- For compound commands (&&, ||, ;, |), ALL subcommands must be- read-only for the entire command to be considered read-only.+ For compound commands (&&, ||, ;, |, newline, &), ALL subcommands+ must be read-only for the entire command to be considered read-only.Commands with output redirections (>, >>) are NOT considered read-only.diff --git a/tests/builtin_bash_test.py b/tests/builtin_bash_test.pyindex f77918cdf..101f07d1e 100644--- a/tests/builtin_bash_test.py+++ b/tests/builtin_bash_test.py@@ -291,6 +291,26 @@ class BashToolInjectionCheckTest(IsolatedAsyncioTestCase):self.assertEqual(decision.behavior, PermissionBehavior.ASK)self.assertIn("command_substitution", decision.message)+ async def test_separator_hidden_commands_not_auto_allowed(self) -> None:+ """Test newline and ``&`` separators cannot bypass permissions."""+ test_cases = [+ "cat file.txt\nrm -rf /tmp/victim",+ "cat file.txt & rm -rf /tmp/victim",+ "ls\nshutdown now",+ "cat \u20ac\u20ac\u20ac\nrm -f ls",+ "cat \u20ac\u20ac\u20ac & rm -f ls",+ ]+ for cmd in test_cases:+ with self.subTest(cmd=repr(cmd)):+ decision = await self.bash_tool.check_permissions(+ {"command": cmd},+ self.context,+ )+ self.assertNotEqual(+ decision.behavior,+ PermissionBehavior.ALLOW,+ )+async def test_mutating_find_commands_not_auto_allowed(self) -> None:"""Test mutating find commands are not auto-allowed as read-only."""test_cases = [diff --git a/tests/permission_bash_parser_test.py b/tests/permission_bash_parser_test.pyindex 720ee15dc..6ef051a11 100644--- a/tests/permission_bash_parser_test.py+++ b/tests/permission_bash_parser_test.py@@ -467,6 +467,74 @@ class BashParserReadOnlyTest(IsolatedAsyncioTestCase):f"(contains non-read-only subcommand)",)+ async def test_separator_hidden_commands_not_read_only(self) -> None:+ """Test newline and ``&`` separators cannot hide mutations."""+ hidden_commands = [+ "cat file.txt\nrm -rf /tmp/victim",+ "cat file.txt\nmkdir new_dir",+ "cat file.txt & rm -rf /tmp/victim",+ "cat file.txt & touch file.txt",+ "ls\nshutdown now",+ "ls\r\nshutdown now",+ "cat \u20ac\u20ac\u20ac\nrm -f ls",+ "cat \u20ac\u20ac\u20ac & rm -f ls",+ ]+ for cmd in hidden_commands:+ with self.subTest(cmd=repr(cmd)):+ self.assertFalse(+ self.parser.is_read_only_command(cmd),+ f"Expected '{cmd!r}' to be non-read-only "+ f"(contains non-read-only subcommand)",+ )++ async def test_separator_compound_all_read_only(self) -> None:+ """Test newline- and ``&``-separated all-read-only compounds."""+ read_only_commands = [+ "git status\ngit diff",+ "cat file1.txt\ncat file2.txt",+ "git status & git log",+ "cat file.txt &",+ "cat caf\u00e9.txt\npwd",+ "cat caf\u00e9.txt & pwd",+ ]+ for cmd in read_only_commands:+ with self.subTest(cmd=repr(cmd)):+ self.assertTrue(+ self.parser.is_read_only_command(cmd),+ f"Expected '{cmd!r}' to be read-only "+ f"(all subcommands are read-only)",+ )++ async def test_split_compound_command_unicode(self) -> None:+ """Test compound splitting preserves Unicode command text."""+ for separator in ["\n", " & "]:+ for commands in [+ ["cat \u20ac\u20ac\u20ac", "rm -f ls"],+ ["cat caf\u00e9.txt", "pwd"],+ ]:+ command = separator.join(commands)+ with self.subTest(command=command):+ root = self.parser.parser.parse(+ command.encode("utf8"),+ ).root_node+ self.assertEqual(+ self.parser.split_compound_command(root, command),+ commands,+ )++ async def test_separator_lookalikes_stay_read_only(self) -> None:+ """Test quoted ``&`` and heredoc newlines are not separators."""+ read_only_commands = [+ 'grep "a&b" file.txt',+ "cat <<EOF\nhello world\nEOF",+ ]+ for cmd in read_only_commands:+ with self.subTest(cmd=repr(cmd)):+ self.assertTrue(+ self.parser.is_read_only_command(cmd),+ f"Expected '{cmd!r}' to be read-only",+ )+async def test_commands_with_output_redirection(self) -> None:"""Test commands with output redirections are not read-only."""redirect_commands = [