On this page
Shared worktree path-traversal guard for wesktop's filesystem and git MCP tools: resolves a relative path and rejects anything escaping the worktree.
#src.wesktop.mcp_tools._paths
#src.wesktop.mcp_tools._paths
Shared worktree path-traversal guard for the MCP tool modules.
Kept in its own module so both filesystem.py and git.py can reuse the same logic without one tool module importing another. Pure stdlib, zero internal dependencies -- consistent with the rest of mcp_tools.
#guard_path
def guard_path(worktree: Path, relative: str) -> PathResolve relative against worktree and ensure it stays inside.
Uses Path.is_relative_to on the fully-resolved paths, which is correct on every platform (it compares path components, not string prefixes, so it is immune to both the '/a/repo2' sibling-prefix escape and to os.sep differences on Windows).
Raises ValueError on any path that escapes the worktree.