ADAPT-Chase's picture
Add files using upload-large-folder tool
fbf3c28 verified
from pathlib import Path
ALLOWED_ROOTS = [
"/data/adaptai",
]
PROTECTED_PATHS = [
"/",
"/bin",
"/sbin",
"/usr",
"/etc",
"/lib",
"/lib64",
"/boot",
"/sys",
"/proc",
"/dev",
"/root",
]
# Normalize and dedupe
PROTECTED_PATHS = sorted(set([str(Path(p).resolve()) for p in PROTECTED_PATHS]))
ALLOWED_ROOTS = sorted(set([str(Path(p).resolve()) for p in ALLOWED_ROOTS]))
def _real(p: str) -> str:
try:
return str(Path(p).resolve())
except Exception:
return p
def is_allowed_path(path: str) -> bool:
rp = _real(path)
# Allowed if under any allowlisted root
for root in ALLOWED_ROOTS:
if rp == root or rp.startswith(root + "/"):
return True
return False
def is_protected_path(path: str) -> bool:
rp = _real(path)
for prot in PROTECTED_PATHS:
if rp == prot or rp.startswith(prot + "/"):
return True
return False
def guard_write_path(path: str) -> tuple[bool, str]:
"""
Returns (ok, msg). ok=False when path is protected or not under allowed roots.
"""
rp = _real(path)
if is_protected_path(rp) and not is_allowed_path(rp):
return False, f"write blocked: protected path {rp}"
if not is_allowed_path(rp):
return False, f"write blocked: outside allowed roots {rp}"
return True, rp