Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): stop git subcommands from passing the read-only fast path with destructive arguments (#2883)
ydflowab4bfbeafa42
diff --git a/src/agentscope/tool/_builtin/_bash_parser.py b/src/agentscope/tool/_builtin/_bash_parser.pyindex 8f40526ab..30635e939 100644--- a/src/agentscope/tool/_builtin/_bash_parser.py+++ b/src/agentscope/tool/_builtin/_bash_parser.py@@ -68,9 +68,6 @@ GIT_READ_ONLY_COMMANDS = {"git log","git diff","git show",- "git branch",- "git tag",- "git remote","git ls-files","git ls-tree","git cat-file",@@ -80,7 +77,6 @@ GIT_READ_ONLY_COMMANDS = {"git shortlog","git blame","git grep",- "git reflog","git config --get","git config --list",}@@ -214,6 +210,19 @@ class BashCommandParser:if self._is_mutating_find_command(cmd):return False+ # ``git grep -O<cmd>`` runs a command, ``--output=<file>`` writes one+ if cmd.startswith("git "):+ try:+ tokens = shlex.split(cmd)+ except ValueError:+ return False+ if any(+ _.startswith(("--output", "--op"))+ or (_.startswith("-") and not _.startswith("--") and "O" in _)+ for _ in tokens[2:]+ ):+ return False+# Check if it starts with a read-only prefixfor readonly_cmd in READ_ONLY_COMMANDS:if cmd == readonly_cmd or cmd.startswith(readonly_cmd + " "):diff --git a/tests/permission_bash_parser_test.py b/tests/permission_bash_parser_test.pyindex 002b23b82..458102251 100644--- a/tests/permission_bash_parser_test.py+++ b/tests/permission_bash_parser_test.py@@ -223,8 +223,6 @@ class BashParserReadOnlyTest(IsolatedAsyncioTestCase):"git log","git diff","git show",- "git branch",- "git remote -v","git log --oneline",]for cmd in read_only_commands:@@ -298,6 +296,32 @@ class BashParserReadOnlyTest(IsolatedAsyncioTestCase):f"Expected '{cmd}' to be non-read-only",)+ async def test_mutating_git_commands_are_not_read_only(self) -> None:+ """Git subcommands that mutate by argument skip the fast path."""+ expected = {+ "git branch -D main": False,+ "git branch -vD x": False,+ "git tag -d v1": False,+ "git remote add o u": False,+ "git reflog expire --all": False,+ "git grep -Otouch foo": False,+ "git grep -iO touch foo": False,+ "git grep '-Otouch' foo": False,+ "git grep --open-files-in-pager=touch foo": False,+ "git diff --output=/tmp/x": False,+ "git log --output ~/.bashrc": False,+ "git status": True,+ "git log": True,+ "git log --oneline -n 5": True,+ "git grep -n foo": True,+ }+ for cmd, read_only in expected.items():+ with self.subTest(cmd=cmd):+ self.assertEqual(+ self.parser.is_read_only_command(cmd),+ read_only,+ )+async def test_single_read_only_docker_commands(self) -> None:"""Test single read-only docker commands."""read_only_commands = [