Skip to content

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

fix(tool): resolve symlinks before the dangerous-path check (#3033)

Daveeadb16f3d97b
diff --git a/src/agentscope/tool/_base.py b/src/agentscope/tool/_base.py
index b0a61517c..3ea982e45 100644
--- a/src/agentscope/tool/_base.py
+++ b/src/agentscope/tool/_base.py
@@ -467,8 +467,8 @@ class ToolBase(ABC):
False
"""
- # Normalize path
- abs_path = os.path.abspath(os.path.expanduser(file_path))
+ # Resolve symlinks so an alias is judged by its target
+ abs_path = os.path.realpath(os.path.expanduser(file_path))
# Split path into segments
path_parts = Path(abs_path).parts
diff --git a/tests/permission_engine_test.py b/tests/permission_engine_test.py
index ee71c1f96..14294fa2c 100644
--- a/tests/permission_engine_test.py
+++ b/tests/permission_engine_test.py
@@ -6,7 +6,9 @@ covers rule priority, rule pattern matching (Bash / file glob), dangerous
path detection, suggestion generation, Bash read-only command analysis,
and bypass-immune safety checks.
"""
+import os
import sys
+import tempfile
import unittest
from unittest.async_case import IsolatedAsyncioTestCase
@@ -439,6 +441,39 @@ class PermissionEngineDangerousPathTest(IsolatedAsyncioTestCase):
# Should NOT be a safety check
self.assertNotIn("safety", decision.decision_reason.lower())
+ async def test_symlink_to_dangerous_file_still_asks(self) -> None:
+ """A symlink aliasing a sensitive file is judged by its target."""
+ with tempfile.TemporaryDirectory() as tmp:
+ real = os.path.join(tmp, ".bashrc")
+ with open(real, "w", encoding="utf-8") as f:
+ f.write("export PATH=/tmp\n")
+ link = os.path.join(tmp, "innocent.txt")
+ os.symlink(real, link)
+
+ decision = await self.engine.check_permission(
+ Write(),
+ {"file_path": link},
+ )
+
+ self.assertEqual(decision.behavior, PermissionBehavior.ASK)
+ self.assertIn("safety", decision.decision_reason.lower())
+
+ async def test_symlink_into_dangerous_directory_asks(self) -> None:
+ """A symlinked path into ``.ssh`` is judged as dangerous too."""
+ with tempfile.TemporaryDirectory() as tmp:
+ real_dir = os.path.join(tmp, ".ssh")
+ os.mkdir(real_dir)
+ link = os.path.join(tmp, "innocent")
+ os.symlink(real_dir, link)
+
+ decision = await self.engine.check_permission(
+ Write(),
+ {"file_path": os.path.join(link, "authorized_keys")},
+ )
+
+ self.assertEqual(decision.behavior, PermissionBehavior.ASK)
+ self.assertIn("safety", decision.decision_reason.lower())
+
async def asyncTearDown(self) -> None:
"""Clean up test fixtures."""
self.engine = None