fix: isolate registered project folders by tenant
This commit is contained in:
@@ -153,6 +153,49 @@ def validate_workspace_registration(value: str, *, must_exist: bool = True) -> P
|
||||
return root
|
||||
|
||||
|
||||
def validate_workspace_registration_isolation(root: Path, user_id: str) -> None:
|
||||
"""Prevent locally registered roots from overlapping another tenant's admin grant."""
|
||||
from app import auth, database
|
||||
|
||||
raw_mapping = os.getenv("SOVEREIGNAI_USER_WORKSPACES", "")
|
||||
try:
|
||||
mapping = json.loads(raw_mapping) if raw_mapping else {}
|
||||
except json.JSONDecodeError as exc:
|
||||
raise ValueError("إعداد مساحات العمل حسب الحساب غير صالح على الخادم.") from exc
|
||||
if not isinstance(mapping, dict):
|
||||
raise ValueError("إعداد مساحات العمل حسب الحساب يجب أن يكون كائن JSON.")
|
||||
|
||||
current_email = (
|
||||
auth.account_email(user_id)
|
||||
if user_id != database.LOCAL_USER_ID
|
||||
else None
|
||||
)
|
||||
candidate = os.path.normcase(os.path.abspath(str(root)))
|
||||
for assigned_email, assigned_paths in mapping.items():
|
||||
if not isinstance(assigned_email, str) or not isinstance(assigned_paths, list):
|
||||
raise ValueError("كل مستخدم في إعداد مساحات العمل يجب أن يقابله مصفوفة مسارات.")
|
||||
if current_email and assigned_email.strip().casefold() == current_email.casefold():
|
||||
continue
|
||||
for value in assigned_paths:
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
raise ValueError("مسار مساحة العمل المخصصة في الخادم غير صالح.")
|
||||
try:
|
||||
assigned = Path(value).expanduser().resolve(strict=True)
|
||||
except (OSError, RuntimeError) as exc:
|
||||
raise ValueError("أحد مسارات مساحة العمل المخصصة غير موجود أو غير متاح.") from exc
|
||||
if not assigned.is_dir():
|
||||
raise ValueError("جذر مساحة العمل المخصصة ليس مجلدًا صالحًا.")
|
||||
normalized = os.path.normcase(os.path.abspath(str(assigned)))
|
||||
try:
|
||||
common = os.path.commonpath((candidate, normalized))
|
||||
except ValueError:
|
||||
continue
|
||||
if common in (candidate, normalized):
|
||||
raise ValueError(
|
||||
"مجلد المشروع يتداخل مع مساحة عمل مخصصة لحساب آخر؛ اختر مجلدًا منفصلًا."
|
||||
)
|
||||
|
||||
|
||||
def selected_root(value: str | None, *, user_id: str | None = None) -> Path | None:
|
||||
"""Resolve a selected directory only within this user's server-assigned roots."""
|
||||
allowed_roots = _roots_for_user(user_id)
|
||||
|
||||
Reference in New Issue
Block a user