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(databases): scope the extension listing to apps the caller can see

Juan Denis6edeba2ff5ed
diff --git a/backend/app/api/database_engines.py b/backend/app/api/database_engines.py
index 4b62e219..473888e5 100644
--- a/backend/app/api/database_engines.py
+++ b/backend/app/api/database_engines.py
@@ -1,3 +1,7 @@
+# Bucket: PER-APP (plan 29 #9). The per-app extension routes
+# (/<app_id>/extensions) address one installed engine, so they gate on
+# can_access_app; the catalog reads above them are system-level and carry no
+# app linkage, and every mutation is admin-only.
"""Database engine catalog + install, prefix ``/api/v1/databases/engines``.
Deliberately thin. There is no engine model and no engine install mechanism
@@ -20,6 +24,8 @@ from flask import Blueprint, jsonify, request
from flask_jwt_extended import get_jwt_identity, jwt_required
from app.middleware.rbac import admin_required
+from app.models import Application, User
+from app.services.resource_grant_service import ResourceGrantService
from app.services import database_engine_extension_service as extensions
from app.services import database_engine_service as engines
from app.services.deployment_job_service import DeploymentJobService
@@ -121,6 +127,25 @@ def list_engine_extensions():
'family': TemplateService.EXTENSION_FAMILY}), 200
+def _require_app_access(app_id):
+ """Ensure the caller may see this app, mirroring the per-app docker DB read.
+
+ Returns an (error_body, status) tuple to return, or None when allowed. The
+ extension listing names an instance's image, its databases and what it can
+ load — all facts about someone's application, so a bare `@jwt_required()`
+ would have let any signed-in user enumerate them across the whole panel.
+ """
+ user = User.query.get(get_jwt_identity())
+ if not user:
+ return {'error': 'Unauthorized'}, 401
+ app = Application.query.get(app_id)
+ if not app:
+ return {'error': 'Application not found'}, 404
+ if not ResourceGrantService.can_access_app(user, app):
+ return {'error': 'Access denied'}, 403
+ return None
+
+
@database_engines_bp.route('/<int:app_id>/extensions', methods=['GET'])
@jwt_required()
def list_instance_extensions(app_id):
@@ -130,6 +155,10 @@ def list_instance_extensions(app_id):
inferring it from the image name -- slower (a docker exec per extension),
authoritative, and the only way to see what is already installed.
"""
+ denied = _require_app_access(app_id)
+ if denied:
+ return jsonify(denied[0]), denied[1]
+
probe = request.args.get('probe', 'false').lower() == 'true'
result = extensions.instance_extensions(app_id, probe=probe)
if 'error' in result:
diff --git a/backend/tests/test_database_engine_extensions.py b/backend/tests/test_database_engine_extensions.py
index 6499b861..bf96cf8f 100644
--- a/backend/tests/test_database_engine_extensions.py
+++ b/backend/tests/test_database_engine_extensions.py
@@ -761,3 +761,52 @@ class TestProbing:
client.get(f'/api/v1/databases/engines/{application.id}/extensions',
headers=auth_headers)
assert not fake.statements
+
+
+# ── per-app authorization on the read (plan 29 #9) ──────────────────────────
+class TestInstanceExtensionsAreScopedToTheApp:
+ """`GET /<app_id>/extensions` describes someone's application: the image it
+ runs, the databases on it, what it can load. A bare `@jwt_required()` let
+ any signed-in user enumerate that for every instance on the panel — caught
+ by the structural authz sweep, not by a feature test, which is exactly what
+ that sweep is for."""
+
+ def test_a_user_without_access_is_denied(self, client, viewer_headers,
+ tmp_path, monkeypatch):
+ application = _install_engine(tmp_path, monkeypatch)
+ # `_install_engine` hardcodes user_id=1, and with no admin created first
+ # the viewer IS user 1 — so reassign the owner explicitly rather than
+ # letting the assertion depend on fixture ordering.
+ owner = User(email='[email protected]', username='extowner',
+ password_hash='x', role=User.ROLE_ADMIN, is_active=True)
+ db.session.add(owner)
+ db.session.commit()
+ application.user_id = owner.id
+ db.session.commit()
+
+ response = client.get(
+ f'/api/v1/databases/engines/{application.id}/extensions',
+ headers=viewer_headers)
+ assert response.status_code == 403, response.get_json()
+ assert 'error' in response.get_json()
+
+ def test_the_owner_still_sees_it(self, client, auth_headers, tmp_path, monkeypatch):
+ application = _install_engine(tmp_path, monkeypatch)
+ response = client.get(
+ f'/api/v1/databases/engines/{application.id}/extensions',
+ headers=auth_headers)
+ assert response.status_code == 200
+ assert 'extensions' in response.get_json()
+
+ def test_unknown_app_is_404_not_403(self, client, auth_headers):
+ """A missing app and a forbidden one are different answers; conflating
+ them would make the endpoint useless for its own caller."""
+ response = client.get('/api/v1/databases/engines/999999/extensions',
+ headers=auth_headers)
+ assert response.status_code == 404
+
+ def test_anonymous_is_rejected(self, client, tmp_path, monkeypatch):
+ application = _install_engine(tmp_path, monkeypatch)
+ response = client.get(
+ f'/api/v1/databases/engines/{application.id}/extensions')
+ assert response.status_code == 401