Restrict workspace paths to configured roots

This commit is contained in:
Hamza Ayed
2026-10-03 01:02:47 +03:00
parent 5430d4ac4b
commit 1b56f70aa6
4 changed files with 49 additions and 7 deletions
@@ -4,7 +4,9 @@ from __future__ import annotations
import tempfile
import unittest
import os
from pathlib import Path
from unittest.mock import patch
from app import workspace
@@ -86,6 +88,21 @@ class WorkspaceChangeTests(unittest.TestCase):
self.root, relative_path, operation, "content\n"
)
def test_selected_workspace_must_be_under_server_allowlisted_roots(self) -> None:
allowed = self.root / "allowed"
nested = allowed / "project"
nested.mkdir(parents=True)
outside = self.root / "outside"
outside.mkdir()
with patch.dict(
os.environ,
{"SOVEREIGNAI_ALLOWED_WORKSPACES": str(allowed)},
clear=False,
):
with self.assertRaisesRegex(ValueError, "خارج مجلدات المشاريع"):
workspace.selected_root(str(outside))
self.assertEqual(workspace.selected_root(str(nested)), nested.resolve())
def test_rejects_create_overwrite_and_update_of_missing_file(self) -> None:
target = self.root / "src" / "existing.py"
target.write_text("value = 1\n", encoding="utf-8")