Skip to content

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.

fix(security): block panel-internal paths from the file manager (GHSA-rm3m-9mvw-68fh)

Juan Denis5bc3f26d7729
diff --git a/backend/app/services/file_service.py b/backend/app/services/file_service.py
index f9e8c05c..34c8195f 100644
--- a/backend/app/services/file_service.py
+++ b/backend/app/services/file_service.py
@@ -33,6 +33,20 @@ class FileService:
# Allowed root directories for browsing (security)
ALLOWED_ROOTS = ['/home', '/var/www', '/opt', '/srv', '/var/log', paths.SERVERKIT_DIR]
+ # Panel-internal directories that must NEVER be reachable through the file
+ # manager, for any role. The backend .env (JWT_SECRET_KEY, encryption key,
+ # DB creds), the SQLite instance dir and the generated deploy config all
+ # live here; on the documented install.sh layout the install dir sits under
+ # 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__), '..'))
+ _INSTALL_DIR = os.path.realpath(os.path.join(_BACKEND_DIR, '..'))
+ PROTECTED_ROOTS = [
+ _INSTALL_DIR,
+ os.path.realpath(paths.SERVERKIT_CONFIG_DIR),
+ ]
+
# File extensions that can be edited in browser
EDITABLE_EXTENSIONS = {
'.txt', '.md', '.json', '.xml', '.yml', '.yaml', '.ini', '.conf', '.cfg',
@@ -49,9 +63,12 @@ class FileService:
@classmethod
def is_path_allowed(cls, path: str) -> bool:
- """Check if path is within allowed directories."""
+ """Check if path is within allowed directories and not panel-internal."""
try:
real_path = os.path.realpath(path)
+ 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)
except (ValueError, OSError):
return False
diff --git a/backend/tests/test_files_rbac.py b/backend/tests/test_files_rbac.py
index 8be34026..12d58ed5 100644
--- a/backend/tests/test_files_rbac.py
+++ b/backend/tests/test_files_rbac.py
@@ -140,6 +140,56 @@ def test_revoked_files_read_blocks_read_endpoints(client, no_read_headers, url):
assert 'files' in resp.get_json()['error']
+# --------------------------------------------------------------------------- #
+# GHSA-rm3m-9mvw-68fh: the panel's own files must never be reachable through
+# the file manager, for ANY role. The default viewer role legitimately has
+# files.read=True, and the documented install layout (/opt/serverkit) sits
+# under the allowed root /opt — so without the PROTECTED_ROOTS exclusion a
+# viewer could read the backend .env, steal JWT_SECRET_KEY, and forge admin
+# sessions.
+# --------------------------------------------------------------------------- #
+
+import os as _os
+
+import app as _app_pkg
+from app import paths as _paths
+from app.services.file_service import FileService as _FileService
+
+_BACKEND_DIR = _os.path.realpath(_os.path.join(_os.path.dirname(_app_pkg.__file__), '..'))
+_INSTALL_DIR = _os.path.realpath(_os.path.join(_BACKEND_DIR, '..'))
+
+PROTECTED_CASES = [
+ _os.path.join(_INSTALL_DIR, '.env'),
+ _os.path.join(_BACKEND_DIR, '.env'),
+ _os.path.join(_BACKEND_DIR, 'run.py'),
+ _os.path.join(_BACKEND_DIR, 'instance', 'serverkit.db'),
+ _os.path.join(_INSTALL_DIR, 'frontend', 'dist', 'index.html'),
+ _os.path.join(_paths.SERVERKIT_CONFIG_DIR, 'deployments.json'),
+]
+
+
[email protected]('path', PROTECTED_CASES)
+def test_panel_files_are_never_allowed(path):
+ assert not _FileService.is_path_allowed(path)
+
+
+ '/api/v1/files/read?path=' + _os.path.join(_INSTALL_DIR, '.env'),
+ '/api/v1/files/download?path=' + _os.path.join(_BACKEND_DIR, '.env'),
+])
+def test_viewer_cannot_read_panel_files(client, role_headers, url):
+ """A viewer (files.read=True) still gets 403 on panel-internal paths."""
+ resp = client.get(url, headers=role_headers[User.ROLE_VIEWER])
+ assert resp.status_code == 403
+
+
+def test_normal_paths_still_allowed(tmp_path, monkeypatch):
+ """Paths under the allowed roots keep working (native path, any OS)."""
+ monkeypatch.setattr(_FileService, 'ALLOWED_ROOTS', [str(tmp_path)])
+ assert _FileService.is_path_allowed(str(tmp_path / 'apps' / 'mysite' / 'index.php'))
+ assert _FileService.is_path_allowed(str(tmp_path / '.env')) # app envs unaffected
+
+
# --------------------------------------------------------------------------- #
# log_service._read_syslog: the service filter must never go through a shell
# (subprocess.list2cmdline is cmd.exe quoting and does not stop $(...)/backtick