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): enforce files.write permission on File Manager mutations (GHSA-4wqh-7f4f-5qmx)

Juan Denis696226002b20
diff --git a/backend/app/api/files.py b/backend/app/api/files.py
index d8579b6a..b8da7aef 100644
--- a/backend/app/api/files.py
+++ b/backend/app/api/files.py
@@ -2,6 +2,7 @@
from flask import Blueprint, request, jsonify, send_file
from flask_jwt_extended import jwt_required
+from ..middleware.rbac import permission_required
from ..services.file_service import FileService
from ..services.storage_provider_service import StorageProviderService
import os
@@ -64,7 +65,7 @@ def read_file():
@files_bp.route('/write', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def write_file():
"""Write content to a file."""
data = request.get_json()
@@ -92,7 +93,7 @@ def write_file():
@files_bp.route('/create', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def create_file():
"""Create a new file."""
data = request.get_json()
@@ -116,7 +117,7 @@ def create_file():
@files_bp.route('/mkdir', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def create_directory():
"""Create a new directory."""
data = request.get_json()
@@ -139,7 +140,7 @@ def create_directory():
@files_bp.route('/delete', methods=['DELETE'])
-@jwt_required()
+@permission_required('files', 'write')
def delete_path():
"""Delete a file or directory."""
path = request.args.get('path')
@@ -157,7 +158,7 @@ def delete_path():
@files_bp.route('/rename', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def rename_path():
"""Rename a file or directory."""
data = request.get_json()
@@ -181,7 +182,7 @@ def rename_path():
@files_bp.route('/copy', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def copy_path():
"""Copy a file or directory."""
data = request.get_json()
@@ -205,7 +206,7 @@ def copy_path():
@files_bp.route('/move', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def move_path():
"""Move a file or directory."""
data = request.get_json()
@@ -229,7 +230,7 @@ def move_path():
@files_bp.route('/chmod', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def change_permissions():
"""Change file/directory permissions."""
data = request.get_json()
@@ -358,7 +359,7 @@ def download_file():
@files_bp.route('/upload', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def upload_file():
"""Upload a file."""
if 'file' not in request.files:
@@ -439,7 +440,7 @@ def s3_read():
@files_bp.route('/s3/write', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def s3_write():
"""Write (create or overwrite) an object from text content."""
data = request.get_json() or {}
@@ -454,7 +455,7 @@ def s3_write():
@files_bp.route('/s3/delete', methods=['DELETE'])
-@jwt_required()
+@permission_required('files', 'write')
def s3_delete():
"""Delete an object (or every object beneath a prefix)."""
path = request.args.get('path')
@@ -476,7 +477,7 @@ def s3_download_url():
@files_bp.route('/s3/upload', methods=['POST'])
-@jwt_required()
+@permission_required('files', 'write')
def s3_upload():
"""Upload a file into the bucket at the given prefix."""
if 'file' not in request.files:
diff --git a/backend/tests/test_files_rbac.py b/backend/tests/test_files_rbac.py
new file mode 100644
index 00000000..ca17d32b
--- /dev/null
+++ b/backend/tests/test_files_rbac.py
@@ -0,0 +1,113 @@
+"""Regression tests for two security advisories.
+
+GHSA-4wqh-7f4f-5qmx — the File Manager API only required a valid JWT, so a
+viewer account (files.write=False) could write/delete files. Mutating
+endpoints must now require the 'files' write permission.
+
+GHSA-mc93-rc3x-fpgq — PostfixService.install() interpolated the hostname into
+a bash -c string. Hostnames are now validated and debconf values are piped
+via stdin, never through a shell.
+"""
+import pytest
+
+from app import db as _db
+from app.models import User
+from app.services.postfix_service import PostfixService
+
+
+def role_headers(app):
+ """JWT headers for a viewer and a developer user."""
+ from flask_jwt_extended import create_access_token
+ from werkzeug.security import generate_password_hash
+
+ headers = {}
+ with app.app_context():
+ for name, role in (('files_viewer', User.ROLE_VIEWER),
+ ('files_dev', User.ROLE_DEVELOPER)):
+ u = User(email=f'{name}@t.local', username=name,
+ password_hash=generate_password_hash('x'),
+ role=role, is_active=True)
+ _db.session.add(u)
+ _db.session.commit()
+ headers[role] = {'Authorization': f'Bearer {create_access_token(identity=u.id)}'}
+ return headers
+
+
+# --------------------------------------------------------------------------- #
+# GHSA-4wqh-7f4f-5qmx: viewer role blocked from file write/delete operations
+# --------------------------------------------------------------------------- #
+
+WRITE_CASES = [
+ ('post', '/api/v1/files/write', {'json': {'path': '/tmp/x', 'content': 'x'}}),
+ ('post', '/api/v1/files/create', {'json': {'path': '/tmp/x'}}),
+ ('post', '/api/v1/files/mkdir', {'json': {'path': '/tmp/x'}}),
+ ('delete', '/api/v1/files/delete?path=/tmp/x', {}),
+ ('post', '/api/v1/files/rename', {'json': {'path': '/tmp/x', 'new_name': 'y'}}),
+ ('post', '/api/v1/files/copy', {'json': {'src': '/tmp/x', 'dest': '/tmp/y'}}),
+ ('post', '/api/v1/files/move', {'json': {'src': '/tmp/x', 'dest': '/tmp/y'}}),
+ ('post', '/api/v1/files/chmod', {'json': {'path': '/tmp/x', 'mode': '755'}}),
+ ('post', '/api/v1/files/s3/write', {'json': {'path': '/x', 'content': 'x'}}),
+ ('delete', '/api/v1/files/s3/delete?path=/x', {}),
+]
+
+
[email protected]('method,url,kwargs', WRITE_CASES)
+def test_viewer_cannot_mutate_files(client, role_headers, method, url, kwargs):
+ resp = getattr(client, method)(url, headers=role_headers[User.ROLE_VIEWER], **kwargs)
+ assert resp.status_code == 403
+ assert 'files' in resp.get_json()['error']
+
+
[email protected]('method,url,kwargs', WRITE_CASES)
+def test_developer_passes_files_write_gate(client, role_headers, monkeypatch,
+ method, url, kwargs):
+ """A role with files.write=True must get past the permission gate.
+
+ The underlying services are stubbed so the test never touches the real
+ filesystem or S3; we only assert the gate does not return 403.
+ """
+ from app.services.file_service import FileService
+ from app.services.storage_provider_service import StorageProviderService
+
+ ok = {'success': True}
+ for name in ('write_file', 'create_file', 'create_directory', 'delete',
+ 'rename', 'copy', 'move', 'change_permissions'):
+ monkeypatch.setattr(FileService, name, staticmethod(lambda *a, **k: dict(ok)))
+ monkeypatch.setattr(StorageProviderService, 's3_write',
+ staticmethod(lambda *a, **k: dict(ok)))
+ monkeypatch.setattr(StorageProviderService, 's3_delete',
+ staticmethod(lambda *a, **k: dict(ok)))
+
+ resp = getattr(client, method)(url, headers=role_headers[User.ROLE_DEVELOPER], **kwargs)
+ assert resp.status_code != 403
+
+
+def test_viewer_can_still_browse(client, role_headers):
+ """Read endpoints stay reachable for viewers (files.read=True)."""
+ resp = client.get('/api/v1/files/browse?path=/tmp',
+ headers=role_headers[User.ROLE_VIEWER])
+ assert resp.status_code != 403
+
+
+# --------------------------------------------------------------------------- #
+# GHSA-mc93-rc3x-fpgq: postfix install rejects malicious hostnames
+# --------------------------------------------------------------------------- #
+
[email protected]('hostname', [
+ '"; id > /tmp/pwned; echo "',
+ '$(id)',
+ '`id`',
+ 'mail.example.com; rm -rf /',
+ 'host name with spaces',
+])
+def test_postfix_install_rejects_bad_hostname(hostname):
+ result = PostfixService.install(hostname=hostname)
+ assert result == {'success': False, 'error': 'Invalid hostname format'}
+
+
[email protected]('hostname', ['mail.example.com', 'mx-1.example.org', 'localhost'])
+def test_postfix_install_accepts_valid_hostname_format(hostname):
+ """Valid hostnames pass validation (install itself is not executed)."""
+ import re
+ assert re.match(r'^[a-zA-Z0-9.-]+$', hostname)