Skip to content

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

fix(tool): recognise paths below a configured filesystem root (#3719)

Kiko0301ffdca1f71e72
diff --git a/src/agentscope/tool/_base.py b/src/agentscope/tool/_base.py
index e8e78f756..3467a1461 100644
--- a/src/agentscope/tool/_base.py
+++ b/src/agentscope/tool/_base.py
@@ -439,16 +439,21 @@ class ToolBase(ABC):
os.path.expanduser(working_dir),
)
try:
- os.path.relpath(abs_file_path, abs_working_dir)
- if (
- abs_file_path.startswith(abs_working_dir + os.sep)
- or abs_file_path == abs_working_dir
- ):
- return True
+ relative = os.path.relpath(abs_file_path, abs_working_dir)
except ValueError:
# On Windows, relpath raises ValueError if paths are on
# different drives.
continue
+ # A plain prefix test misses every filesystem root: for
+ # ``/`` it builds ``"//"`` and for ``D:\`` it builds
+ # ``"D:\\"``, neither of which a real path starts with. The
+ # relative path is ``"."`` for the directory itself and
+ # otherwise only escapes upwards when it begins with
+ # ``".."``.
+ if relative != os.pardir and not relative.startswith(
+ os.pardir + os.sep,
+ ):
+ return True
return False
diff --git a/tests/permission_mode_test.py b/tests/permission_mode_test.py
index 6a619fb96..1dcc8e8e3 100644
--- a/tests/permission_mode_test.py
+++ b/tests/permission_mode_test.py
@@ -458,6 +458,36 @@ class PermissionEngineAcceptEditsModeTest(IsolatedAsyncioTestCase):
shutil.rmtree(parent, ignore_errors=True)
+ async def test_accept_edits_within_filesystem_root(self) -> None:
+ """A configured working directory that is a filesystem root
+ (``/`` or ``D:\\``) must cover the paths below it.
+
+ The containment test used to build ``root + os.sep`` — ``"//"``
+ on POSIX and ``"D:\\\\"`` on Windows — so no path under a root
+ ever matched and every edit there asked for permission.
+ """
+ root = os.path.abspath(os.sep)
+ context = PermissionContext(
+ mode=PermissionMode.ACCEPT_EDITS,
+ working_directories={
+ root: AdditionalWorkingDirectory(path=root, source="test"),
+ },
+ )
+ engine = PermissionEngine(context)
+
+ decision = await engine.check_permission(
+ Write(),
+ {
+ "file_path": os.path.join(
+ root,
+ "tmp",
+ "agentscope-root-test.txt",
+ ),
+ },
+ )
+
+ self.assertEqual(decision.behavior, PermissionBehavior.ALLOW)
+
async def test_accept_edits_outside_working_directory(self) -> None:
"""Edit outside a working directory falls to the default ASK."""
decision = await self.engine.check_permission(