Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tool): escape glob metacharacters in Write/Edit permission suggestions (#3378)
lihongyuan991a60e2d977c3
diff --git a/src/agentscope/tool/_builtin/_edit.py b/src/agentscope/tool/_builtin/_edit.pyindex 9e87720a2..e247c8eab 100644--- a/src/agentscope/tool/_builtin/_edit.py+++ b/src/agentscope/tool/_builtin/_edit.py@@ -2,6 +2,7 @@"""The edit tool in agentscope."""import difflibimport fnmatch+import globfrom typing import Any, Listfrom .._base import ToolBase, ToolMiddlewareBase@@ -240,7 +241,10 @@ Usage:parent = self._backend.dirname(file_path)# Glob patterns are POSIX-style strings (matched by fnmatch),# not real filesystem paths — do NOT use backend.join_path here.- pattern = (parent.rstrip("/\\") + "/**") if parent else "**"+ # ``parent`` is a literal path, so its fnmatch metacharacters must be+ # escaped, otherwise the suggested rule matches sibling directories.+ escaped = glob.escape(parent)+ pattern = (escaped.rstrip("/\\") + "/**") if escaped else "**"return [PermissionRule(diff --git a/src/agentscope/tool/_builtin/_write.py b/src/agentscope/tool/_builtin/_write.pyindex 54f3910af..d0e650565 100644--- a/src/agentscope/tool/_builtin/_write.py+++ b/src/agentscope/tool/_builtin/_write.py@@ -2,6 +2,7 @@"""The write tool in agentscope."""import difflibimport fnmatch+import globfrom typing import Any, Listfrom .._base import ToolBase, ToolMiddlewareBase@@ -217,7 +218,10 @@ Usage:parent = self._backend.dirname(file_path)# Glob patterns are POSIX-style strings (matched by fnmatch),# not real filesystem paths — do NOT use backend.join_path here.- pattern = (parent.rstrip("/\\") + "/**") if parent else "**"+ # ``parent`` is a literal path, so its fnmatch metacharacters must be+ # escaped, otherwise the suggested rule matches sibling directories.+ escaped = glob.escape(parent)+ pattern = (escaped.rstrip("/\\") + "/**") if escaped else "**"return [PermissionRule(diff --git a/tests/builtin_edit_test.py b/tests/builtin_edit_test.pyindex 25c166cfe..3e8f31650 100644--- a/tests/builtin_edit_test.py+++ b/tests/builtin_edit_test.py@@ -8,6 +8,7 @@ from agentscope.tool import Editfrom agentscope.permission import (PermissionContext,PermissionBehavior,+ PermissionEngine,PermissionRule,)@@ -238,3 +239,59 @@ class EditToolTest(IsolatedAsyncioTestCase):{"file_path": "/test.py"},)self.assertGreater(len(suggestions), 0)++ async def test_permission_suggestions_escape_parent(self) -> None:+ """Test approved suggestions cover only the literal parent."""+ for parent, escaped, sibling in (+ ("reports", "reports", "reports-other"),+ ("reports [2024]", "reports [[]2024]", "reports 2"),+ ("reports ?", "reports [?]", "reports x"),+ ("reports *", "reports [*]", "reports other"),+ ):+ with self.subTest(parent=parent):+ tool_input = {+ "file_path": f"/tmp/probe/{parent}/out.csv",+ "old_string": "a",+ "new_string": "b",+ }+ engine = PermissionEngine(PermissionContext())+ first = await engine.check_permission(+ self.edit_tool,+ tool_input,+ )+ self.assertEqual(+ first.suggested_rules,+ [+ PermissionRule(+ tool_name="Edit",+ rule_content=f"/tmp/probe/{escaped}/**",+ behavior=PermissionBehavior.ALLOW,+ source="suggested",+ ),+ ],+ )++ engine.add_rule(first.suggested_rules[0])+ approved = await engine.check_permission(+ self.edit_tool,+ tool_input,+ )+ unrelated = await engine.check_permission(+ self.edit_tool,+ {+ **tool_input,+ "file_path": f"/tmp/probe/{sibling}/out.csv",+ },+ )+ self.assertEqual(+ [+ first.behavior,+ approved.behavior,+ unrelated.behavior,+ ],+ [+ PermissionBehavior.ASK,+ PermissionBehavior.ALLOW,+ PermissionBehavior.ASK,+ ],+ )diff --git a/tests/builtin_write_test.py b/tests/builtin_write_test.pyindex 294ae490f..e815aab19 100644--- a/tests/builtin_write_test.py+++ b/tests/builtin_write_test.py@@ -10,6 +10,7 @@ from agentscope.tool import Writefrom agentscope.permission import (PermissionContext,PermissionBehavior,+ PermissionEngine,PermissionRule,)from agentscope.state import AgentState@@ -245,3 +246,58 @@ class WriteToolTest(IsolatedAsyncioTestCase):{"file_path": "/test.py"},)self.assertGreater(len(suggestions), 0)++ async def test_permission_suggestions_escape_parent(self) -> None:+ """Test approved suggestions cover only the literal parent."""+ for parent, escaped, sibling in (+ ("reports", "reports", "reports-other"),+ ("reports [2024]", "reports [[]2024]", "reports 2"),+ ("reports ?", "reports [?]", "reports x"),+ ("reports *", "reports [*]", "reports other"),+ ):+ with self.subTest(parent=parent):+ tool_input = {+ "file_path": f"/tmp/probe/{parent}/out.csv",+ "content": "x",+ }+ engine = PermissionEngine(PermissionContext())+ first = await engine.check_permission(+ self.write_tool,+ tool_input,+ )+ self.assertEqual(+ first.suggested_rules,+ [+ PermissionRule(+ tool_name="Write",+ rule_content=f"/tmp/probe/{escaped}/**",+ behavior=PermissionBehavior.ALLOW,+ source="suggested",+ ),+ ],+ )++ engine.add_rule(first.suggested_rules[0])+ approved = await engine.check_permission(+ self.write_tool,+ tool_input,+ )+ unrelated = await engine.check_permission(+ self.write_tool,+ {+ **tool_input,+ "file_path": f"/tmp/probe/{sibling}/out.csv",+ },+ )+ self.assertEqual(+ [+ first.behavior,+ approved.behavior,+ unrelated.behavior,+ ],+ [+ PermissionBehavior.ASK,+ PermissionBehavior.ALLOW,+ PermissionBehavior.ASK,+ ],+ )