fix(security): prevent path traversal bypass via startswith check
`startswith` string comparison allows bypassing directory restrictions. For example, `/home/user/workspace_evil` passes the check against `/home/user/workspace` because the string starts with the allowed path. Replace with `Path.relative_to()` which correctly validates that the resolved path is actually inside the allowed directory tree. Fixes #888
This commit is contained in:
@@ -13,7 +13,10 @@ def _resolve_path(path: str, workspace: Path | None = None, allowed_dir: Path |
|
|||||||
if not p.is_absolute() and workspace:
|
if not p.is_absolute() and workspace:
|
||||||
p = workspace / p
|
p = workspace / p
|
||||||
resolved = p.resolve()
|
resolved = p.resolve()
|
||||||
if allowed_dir and not str(resolved).startswith(str(allowed_dir.resolve())):
|
if allowed_dir:
|
||||||
|
try:
|
||||||
|
resolved.relative_to(allowed_dir.resolve())
|
||||||
|
except ValueError:
|
||||||
raise PermissionError(f"Path {path} is outside allowed directory {allowed_dir}")
|
raise PermissionError(f"Path {path} is outside allowed directory {allowed_dir}")
|
||||||
return resolved
|
return resolved
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user