diff --git a/src/cleveragents/tool/path_mapper.py b/src/cleveragents/tool/path_mapper.py index e1895ac58..30a31f238 100644 --- a/src/cleveragents/tool/path_mapper.py +++ b/src/cleveragents/tool/path_mapper.py @@ -182,8 +182,14 @@ def _is_under(path: str, root: str) -> bool: def _relative_to(path: str, root: str) -> str: """Return the part of *path* relative to *root*. + Uses :func:`posixpath.relpath` for canonical relative-path extraction, + consistent with :func:`_is_under`. This avoids string-slicing prefix-collision + attacks where an attacker filename is a literal prefix of the sandbox root + (see issue #7478). + Assumes :func:`_is_under` has already been checked. """ if path == root: return "" - return path[len(root) + 1 :] + rel = posixpath.relpath(path, root) + return rel.replace("\\", "/")