-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.py
More file actions
32 lines (26 loc) · 1.02 KB
/
Copy pathsecurity.py
File metadata and controls
32 lines (26 loc) · 1.02 KB
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
import os
from pathlib import Path
from rich.console import Console
from rich.prompt import Confirm
console = Console()
def get_cwd():
return Path.cwd()
def validate_path(file_path: str) -> str:
"""
Sandboxing: Ensures the path is within the current working directory.
Resolves relative paths and checks against CWD.
"""
cwd = get_cwd()
# Resolve the absolute path
target = (cwd / file_path).resolve()
# Check if target starts with CWD (prevents traversal like ../../etc/passwd)
if not str(target).startswith(str(cwd)):
raise PermissionError(f"Access denied: Path '{file_path}' is outside the current working directory.")
return str(target)
def confirm_action(action_type: str, details: str) -> bool:
"""
Gating: Forces user confirmation for sensitive actions.
"""
console.print(f"\n[bold yellow]⚠️ CONFIRMATION REQUIRED[/bold yellow]")
console.print(f"[bold]{action_type}:[/bold] {details}")
return Confirm.ask("Do you want to proceed?", default=False)