#3032·agentscope

[Bug]: a symlink alias bypasses the dangerous-path safety check, so writes to ~/.ssh/authorized_keys, .netrc and .env skip the bypass_immune ASK

Author: sunrong1Created Sep 30, 2026Updated Sep 30, 2026
Labelstriage/confirmed

Prerequisites

  • I have searched the existing issues and discussions, and this is not a duplicate.
  • This is a bug, not a usage question.

Background / Description

ToolBase._is_dangerous_path normalizes the candidate path with os.path.abspath (_base.py:471):

python
# Normalize path
abs_path = os.path.abspath(os.path.expanduser(file_path))

# Split path into segments
path_parts = Path(abs_path).parts

os.path.abspath is a pure string operation: it prepends the current working directory and collapses . / .., but it never touches the filesystem and therefore never resolves a symbolic link. A symlink named after something harmless is judged by its name, not by its target.

Write and Edit call this helper before anything else and raise a bypass_immune ASK when it returns True (_edit.py:154-162, _write.py:133-140):

python
if self._is_dangerous_path(file_path):
    return PermissionDecision(
        behavior=PermissionBehavior.ASK,
        decision_reason="Safety check: dangerous file or directory",
        bypass_immune=True,
    )

bypass_immune=True is documented as a guardrail that must hold "regardless of allow rules" — ACCEPT_EDITS honors it over its working-directory auto-allow, and DONT_ASK turns it into a DENY (_engine.py:638-644). A symlink skips that guardrail entirely, so the operation falls through to PASSTHROUGH and then to the ordinary allow rules.

The same module already gets this right elsewhere. _path_in_allowed_working_path compares both sides with os.path.realpath precisely so that aliases compare equal (_base.py:422-427), and its docstring says so explicitly:

"Paths are compared via :func:os.path.realpath so that aliases like macOS's /tmp → /private/tmp and symlinked working directories compare equal on both sides."

That was introduced by #1767 as the fix for #1649. #1649 was the opposite failure — abspath there caused a false positive (a legitimate in-directory file was asked about). This report is the false negative: abspath here lets a real sensitive target through. The two are not the same defect; #1649's fix left this call site untouched.

Error Messages

No exception is raised. The permission decision itself is the evidence:

$ python repro_symlink_bypass.py
target      : /tmp/tmpXXXX/.bashrc
alias       : /tmp/tmpXXXX/innocent.txt  -> /tmp/tmpXXXX/.bashrc

_is_dangerous_path(target) = True
_is_dangerous_path(alias)  = False

os.path.abspath(alias)  = /tmp/tmpXXXX/innocent.txt
os.path.realpath(alias) = /tmp/tmpXXXX/.bashrc

check_permissions(Write, alias,  DONT_ASK).behavior = PASSTHROUGH
check_permissions(Write, target, DONT_ASK).behavior = ASK
   -> "Permission required: Write operation on sensitive file ..."

PASSTHROUGH for the alias and ASK for the file it points at is the bypass: the engine never sees a safety concern, so no allow rule has to be bypassed — there is simply nothing to ask about.

Steps to Reproduce

Runs in a few seconds, no API key, no network, no MCP.

python
import asyncio
import os
import tempfile

from agentscope.permission import PermissionContext, PermissionMode
from agentscope.tool import Write


async def main() -> None:
    with tempfile.TemporaryDirectory() as tmp:
        target = os.path.join(tmp, ".bashrc")
        with open(target, "w", encoding="utf-8") as f:
            f.write("export PATH=/tmp
")
        alias = os.path.join(tmp, "innocent.txt")
        os.symlink(target, alias)

        write = Write()
        context = PermissionContext(mode=PermissionMode.DONT_ASK)

        print("target     :", target)
        print("alias      :", alias, "->", os.readlink(alias))
        print()
        print("_is_dangerous_path(target) =",
              write._is_dangerous_path(target))
        print("_is_dangerous_path(alias)  =",
              write._is_dangerous_path(alias))
        print()
        print("abspath(alias)  =", os.path.abspath(alias))
        print("realpath(alias) =", os.path.realpath(alias))
        print()

        for label, path in (("alias", alias), ("target", target)):
            decision = await write.check_permissions(
                tool_input={"file_path": path, "content": "pwned"},
                context=context,
            )
            print(f"check_permissions({label}) = {decision.behavior}")


asyncio.run(main())

On main at 78c4ec47 this prints PASSTHROUGH for the alias and ASK for the target. The same shape works for a symlinked directory — innocent -> ~/.ssh, then innocent/authorized_keys — because the segment scan also runs on the unresolved name.

Environment

  • AgentScope Version: 2.0.10dev (main at 78c4ec47)
  • Python Version: 3.11
  • OS: Linux (x86_64)
  • Verified on: main, unmodified; the three regression tests added with the fix fail on this baseline

Suggested Fix

Normalize with the same function the rest of the module uses:

diff
-        # Normalize path
-        abs_path = os.path.abspath(os.path.expanduser(file_path))
+        # Normalize path, resolving symlinks so an alias cannot smuggle a
+        # dangerous target past the check below.
+        abs_path = os.path.realpath(os.path.expanduser(file_path))

A residual TOCTOU window remains — the link can still be swapped between the check and the open — so this is necessary, not sufficient. What it removes is the case where the path is already a symlink at check time, which is the shape this bypass takes. The accompanying tests cover the dangerous-file alias, a symlinked .ssh directory, and a symlink to an ordinary file so the change does not start over-blocking.

PR: coming.

Source: agentscope-ai/agentscope