ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix(security): correct PROTECTED_ROOTS path math; tighten root matching
Juan Denis048a560f8cb2
diff --git a/backend/app/services/file_service.py b/backend/app/services/file_service.pyindex 34c8195f..7a3977d2 100644--- a/backend/app/services/file_service.py+++ b/backend/app/services/file_service.py@@ -40,10 +40,15 @@ class FileService:# the allowed root /opt, so without this exclusion any user with files.read# (including the default viewer role) could read the panel's own secrets# and forge admin sessions (GHSA-rm3m-9mvw-68fh).- _BACKEND_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))+ # __file__ = <install>/backend/app/services/file_service.py — two levels up+ # is the backend dir, three is the install root. Getting this wrong by one+ # level silently un-protects <install>/.env and frontend/dist.+ _BACKEND_DIR = os.path.realpath(+ os.path.join(os.path.dirname(__file__), '..', '..'))_INSTALL_DIR = os.path.realpath(os.path.join(_BACKEND_DIR, '..'))PROTECTED_ROOTS = [_INSTALL_DIR,+ _BACKEND_DIR, # covers flat layouts (e.g. code at /app, install=/ inert)os.path.realpath(paths.SERVERKIT_CONFIG_DIR),]@@ -69,7 +74,8 @@ class FileService:if any(real_path == root or real_path.startswith(root + os.sep)for root in cls.PROTECTED_ROOTS):return False- return any(real_path.startswith(root) for root in cls.ALLOWED_ROOTS)+ return any(real_path == root or real_path.startswith(root + os.sep)+ for root in cls.ALLOWED_ROOTS)except (ValueError, OSError):return Falsediff --git a/backend/tests/test_files_rbac.py b/backend/tests/test_files_rbac.pyindex 12d58ed5..e5c7eb93 100644--- a/backend/tests/test_files_rbac.py+++ b/backend/tests/test_files_rbac.py@@ -173,6 +173,33 @@ def test_panel_files_are_never_allowed(path):assert not _FileService.is_path_allowed(path)+def test_protected_roots_resolve_to_the_real_install_layout():+ """The service's own path math must land on <install>/backend and+ <install> — one level off silently un-protects <install>/.env and+ frontend/dist (caught in review; the other tests compute the expected+ layout independently of the service, so they can't see the service+ getting it wrong)."""+ services_dir = _os.path.dirname(_os.path.realpath(+ _os.path.join(_os.path.dirname(_app_pkg.__file__), 'services', 'file_service.py')))+ backend_dir = _os.path.realpath(_os.path.join(services_dir, '..', '..'))+ install_dir = _os.path.realpath(_os.path.join(backend_dir, '..'))+ assert _FileService._BACKEND_DIR == backend_dir+ assert _FileService._INSTALL_DIR == install_dir+++def test_install_root_blocked_even_when_under_an_allowed_root(monkeypatch):+ """Deployed layout simulation: the install dir sits INSIDE an allowed+ root (/opt/serverkit under /opt). Protected must win over allowed —+ this is the exact /read .env path from the advisory."""+ install_dir = _FileService._INSTALL_DIR+ monkeypatch.setattr(_FileService, 'ALLOWED_ROOTS', [install_dir])+ assert not _FileService.is_path_allowed(_os.path.join(install_dir, '.env'))+ assert not _FileService.is_path_allowed(+ _os.path.join(install_dir, 'frontend', 'dist', 'index.html'))+ assert not _FileService.is_path_allowed(+ _os.path.join(install_dir, 'backend', 'run.py'))++@pytest.mark.parametrize('url', ['/api/v1/files/read?path=' + _os.path.join(_INSTALL_DIR, '.env'),'/api/v1/files/download?path=' + _os.path.join(_BACKEND_DIR, '.env'),