Skip to content

Your lightweight open-source personal agent.

test(exec): verify windows drive-root workspace guard

Xubin Ren485c75e06580
diff --git a/tests/tools/test_tool_validation.py b/tests/tools/test_tool_validation.py
index af4675310..98a3dc903 100644
--- a/tests/tools/test_tool_validation.py
+++ b/tests/tools/test_tool_validation.py
@@ -142,6 +142,45 @@ def test_exec_guard_blocks_quoted_home_path_outside_workspace(tmp_path) -> None:
assert error == "Error: Command blocked by safety guard (path outside working dir)"
+def test_exec_guard_blocks_windows_drive_root_outside_workspace(monkeypatch) -> None:
+ import nanobot.agent.tools.shell as shell_mod
+
+ class FakeWindowsPath:
+ def __init__(self, raw: str) -> None:
+ self.raw = raw.rstrip("\\") + ("\\" if raw.endswith("\\") else "")
+
+ def resolve(self) -> "FakeWindowsPath":
+ return self
+
+ def expanduser(self) -> "FakeWindowsPath":
+ return self
+
+ def is_absolute(self) -> bool:
+ return len(self.raw) >= 3 and self.raw[1:3] == ":\\"
+
+ @property
+ def parents(self) -> list["FakeWindowsPath"]:
+ if not self.is_absolute():
+ return []
+ trimmed = self.raw.rstrip("\\")
+ if len(trimmed) <= 2:
+ return []
+ idx = trimmed.rfind("\\")
+ if idx <= 2:
+ return [FakeWindowsPath(trimmed[:2] + "\\")]
+ parent = FakeWindowsPath(trimmed[:idx])
+ return [parent, *parent.parents]
+
+ def __eq__(self, other: object) -> bool:
+ return isinstance(other, FakeWindowsPath) and self.raw.lower() == other.raw.lower()
+
+ monkeypatch.setattr(shell_mod, "Path", FakeWindowsPath)
+
+ tool = ExecTool(restrict_to_workspace=True)
+ error = tool._guard_command("dir E:\\", "E:\\workspace")
+ assert error == "Error: Command blocked by safety guard (path outside working dir)"
+
+
# --- cast_params tests ---