Spaces:
Running
Running
File size: 2,388 Bytes
4997b06 06e1bfa 37f9abc 4997b06 37f9abc e66e95e 37f9abc e66e95e 37f9abc e66e95e 37f9abc e66e95e 37f9abc e66e95e 37f9abc e66e95e 37f9abc c706455 e66e95e 37f9abc e66e95e 37f9abc c706455 37f9abc c706455 37f9abc 06e1bfa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | from pathlib import Path, PurePosixPath
from re import fullmatch
from multi_agent_sdlc.config import SANDBOX_ROOT
def normalise_relative_path(path: str) -> PurePosixPath:
"""Normalise and validate a project-relative path."""
cleaned = path.strip().replace("\\", "/")
if not cleaned:
raise ValueError("Path cannot be empty.")
candidate = PurePosixPath(cleaned)
if candidate.is_absolute():
raise PermissionError(f"Absolute paths are not allowed: {path}")
if ".." in candidate.parts:
raise PermissionError(f"Parent-directory traversal is not allowed: {path}")
return candidate
def reject_repeated_project_prefix(
project_directory: Path,
candidate: PurePosixPath,
) -> None:
"""Reject paths prefixed with the current project directory."""
parts = candidate.parts
project_id = project_directory.name
sandbox_root = project_directory.parent.name
if parts and parts[0] == project_id:
prefix_length = 1
elif len(parts) >= 2 and parts[0] == sandbox_root and parts[1] == project_id:
prefix_length = 2
else:
return
remaining_parts = parts[prefix_length:]
suggested_path = (
PurePosixPath(*remaining_parts).as_posix() if remaining_parts else "."
)
raise PermissionError(
"Do not include the project-directory prefix. "
f"Use {suggested_path} instead of {candidate.as_posix()}."
)
def resolve_project_path(
project_directory: Path,
path: str,
) -> Path:
"""Resolve a project-relative path and ensure it stays inside the project."""
relative_path = normalise_relative_path(path)
reject_repeated_project_prefix(
project_directory,
relative_path,
)
root = project_directory.resolve()
resolved = (root / relative_path).resolve()
if not resolved.is_relative_to(root):
raise PermissionError(f"Path escapes the project directory: {path!r}")
return resolved
def create_project_directory(project_id: str) -> Path:
"Create project folder inside 'sandbox', e.g: terminal-calculator"
if not fullmatch(r"[a-z0-9]+(?:-[a-z0-9]+)*", project_id):
raise ValueError("project_id must use lowercase kebab-case.")
project_directory = Path(SANDBOX_ROOT) / project_id
project_directory.mkdir(parents=True, exist_ok=True)
return project_directory
|