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.

Protect the idle update slot in the file manager

Juan Denisd60eaf466bbf
diff --git a/backend/app/services/file_service.py b/backend/app/services/file_service.py
index d24bae9a..74be71cd 100644
--- a/backend/app/services/file_service.py
+++ b/backend/app/services/file_service.py
@@ -33,6 +33,23 @@ from app.exceptions import (
from app.utils.formatting import format_bytes
+def _install_roots(install_dir: str) -> List[str]:
+ """*install_dir* plus its blue/green sibling when it is an update slot.
+
+ Updates install into the idle slot (``/opt/serverkit-a`` / ``-b``) and
+ flip the ``/opt/serverkit`` symlink, so the previous version — with its
+ own ``.env`` and JWT key — stays on disk next to the running one.
+ Protecting only the running slot leaves that copy readable.
+ """
+ roots = [install_dir]
+ parent, name = os.path.split(install_dir)
+ if name.endswith(('-a', '-b')):
+ base = name[:-2]
+ roots += [os.path.join(parent, f'{base}-{s}') for s in ('a', 'b')
+ if f'{base}-{s}' != name]
+ return roots
+
+
class FileService:
"""Service for file system operations."""
@@ -69,7 +86,7 @@ class FileService:
os.path.join(os.path.dirname(__file__), '..', '..'))
_INSTALL_DIR = os.path.realpath(os.path.join(_BACKEND_DIR, '..'))
PROTECTED_ROOTS = [
- _INSTALL_DIR,
+ *_install_roots(_INSTALL_DIR),
_BACKEND_DIR, # covers flat layouts (e.g. code at /app, install=/ inert)
os.path.realpath(paths.SERVERKIT_CONFIG_DIR),
]
diff --git a/backend/tests/test_files_rbac.py b/backend/tests/test_files_rbac.py
index fe273913..5d4af1fe 100644
--- a/backend/tests/test_files_rbac.py
+++ b/backend/tests/test_files_rbac.py
@@ -161,6 +161,7 @@ def test_revoked_files_read_blocks_read_endpoints(client, no_read_headers, url):
# --------------------------------------------------------------------------- #
import os as _os
+from pathlib import Path
import app as _app_pkg
from app import paths as _paths
@@ -211,6 +212,31 @@ def test_install_root_blocked_even_when_under_an_allowed_root(monkeypatch):
_os.path.join(install_dir, 'backend', 'run.py'))
[email protected]('running', ['a', 'b'])
+def test_idle_update_slot_is_protected(tmp_path, running):
+ """Running from /opt/serverkit-<x>, the previous version in the other
+ slot keeps its own .env (JWT key). It must be as unreachable as ours."""
+ from app.services.file_service import _install_roots
+ idle = 'b' if running == 'a' else 'a'
+ tmp_path = Path(_os.path.realpath(tmp_path))
+ slot = str(tmp_path / f'serverkit-{running}')
+ roots = _install_roots(slot)
+ assert str(tmp_path / f'serverkit-{idle}') in roots
+
+ class _Slotted(_FileService):
+ ALLOWED_ROOTS = [str(tmp_path)]
+ PROTECTED_ROOTS = roots
+
+ assert not _Slotted.is_path_allowed(str(tmp_path / f'serverkit-{idle}' / '.env'))
+ assert not _Slotted.is_path_allowed(str(tmp_path / f'serverkit-{running}' / '.env'))
+ assert _Slotted.is_path_allowed(str(tmp_path / 'serverkit-apps' / '.env'))
+
+
+def test_non_slot_install_has_no_siblings():
+ from app.services.file_service import _install_roots
+ assert _install_roots('/opt/serverkit') == ['/opt/serverkit']
+
+
@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'),