Skip to content

Your lightweight open-source personal agent.

fix(security): prevent path traversal bypass via startswith check

andienguyen-ecoligo5c9cb3a208a0
diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py
index 9c169e4c4..b87da116d 100644
--- a/nanobot/agent/tools/filesystem.py
+++ b/nanobot/agent/tools/filesystem.py
@@ -13,8 +13,11 @@ def _resolve_path(path: str, workspace: Path | None = None, allowed_dir: Path |
if not p.is_absolute() and workspace:
p = workspace / p
resolved = p.resolve()
- if allowed_dir and not str(resolved).startswith(str(allowed_dir.resolve())):
- raise PermissionError(f"Path {path} is outside allowed directory {allowed_dir}")
+ if allowed_dir:
+ try:
+ resolved.relative_to(allowed_dir.resolve())
+ except ValueError:
+ raise PermissionError(f"Path {path} is outside allowed directory {allowed_dir}")
return resolved