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(connect): use the policy door, the privilege wrapper, and safe reads

Juan Denis1f92ae1c91e1
diff --git a/backend/app/api/connect.py b/backend/app/api/connect.py
index 95de0602..6d7e9406 100644
--- a/backend/app/api/connect.py
+++ b/backend/app/api/connect.py
@@ -5,15 +5,15 @@ app/services/connect_client.py); this read-only endpoint lets the Settings
UI render the connection state.
"""
from flask import Blueprint, jsonify
-from flask_jwt_extended import jwt_required
+from app.middleware.rbac import admin_required
from app.services import connect_client
connect_bp = Blueprint('connect', __name__)
@connect_bp.route('/status', methods=['GET'])
-@jwt_required()
+@admin_required
def get_status():
"""Current ServerKit Cloud connection state (unpaired/paired_offline/...)."""
return jsonify(connect_client.status())
diff --git a/backend/app/jobs/thread_ownership.py b/backend/app/jobs/thread_ownership.py
index 8a6abfa8..ab46f697 100644
--- a/backend/app/jobs/thread_ownership.py
+++ b/backend/app/jobs/thread_ownership.py
@@ -159,6 +159,25 @@ THREAD_OWNERSHIP = {
'lifecycle': LIFECYCLE_REQUEST_STREAM,
'rationale': 'Emitter lifetime is coupled to one socket subscription.',
},
+ 'app/services/connect_client.py:start:self._run': {
+ 'owner': 'connect relay client',
+ 'lifecycle': LIFECYCLE_PROCESS_LOOP,
+ 'rationale': (
+ 'One outbound socket per process, started at boot only when the '
+ 'panel is paired and stopped with it; there is nothing to resume '
+ 'because reconnection is the loop itself.'
+ ),
+ },
+ 'app/services/connect_client.py:_run_command:self._command_worker': {
+ 'owner': 'connect signed command',
+ 'lifecycle': LIFECYCLE_BOUNDED_DELIVERY,
+ 'rationale': (
+ 'One signed command runs off the socket read loop so a slow '
+ 'handler cannot stall the heartbeat. The command row lives in the '
+ 'control plane, which times it out and is told the outcome, so a '
+ 'restart loses an attempt rather than the record of it.'
+ ),
+ },
}
diff --git a/backend/app/services/connect_client.py b/backend/app/services/connect_client.py
index 233bc494..1a507be4 100644
--- a/backend/app/services/connect_client.py
+++ b/backend/app/services/connect_client.py
@@ -336,6 +336,19 @@ def _past(iso_ts: str) -> bool:
# ==================== Status / disconnect ====================
+def _configured_cloud_url() -> str:
+ """The configured control-plane URL, or the default if it is malformed.
+
+ status() is a read: a typo in SERVERKIT_CLOUD_URL should show up when the
+ operator tries to pair, not turn `serverkit connect status` and the
+ Settings panel into an error.
+ """
+ try:
+ return resolve_cloud_url()
+ except ConnectError:
+ return DEFAULT_CLOUD_URL
+
+
def status() -> dict:
"""Connection state as the UI/CLI should show it.
@@ -344,6 +357,7 @@ def status() -> dict:
needs to be running in this process.
"""
data = _read_connect_file()
+ cloud_url = _configured_cloud_url()
key_path = data.get(KEY_PATH_FIELD) or connect_keys.default_key_path()
key_present = os.path.exists(key_path)
if not data.get('device_id'):
@@ -351,7 +365,7 @@ def status() -> dict:
'state': 'unpaired',
'state_reason': None,
'paired': False,
- 'cloud_url': resolve_cloud_url(),
+ 'cloud_url': cloud_url,
'key_present': key_present,
'key_path': key_path,
}
@@ -366,7 +380,7 @@ def status() -> dict:
'state': state,
'state_reason': reason,
'paired': True,
- 'cloud_url': data.get('cloud_url') or resolve_cloud_url(),
+ 'cloud_url': data.get('cloud_url') or cloud_url,
'device_id': data.get('device_id'),
'org_slug': data.get('org_slug'),
'name': data.get('name'),
@@ -509,8 +523,13 @@ HELLO_HEADERS = {
def hello_headers(hello: dict) -> dict:
- """The same hello carried in X-ServerKit-* headers for the poll fallback."""
- return {header: str(hello[field]) for field, header in HELLO_HEADERS.items()}
+ """The same hello carried in X-ServerKit-* headers for the poll fallback.
+
+ build_hello() drops the end-to-end key rather than fail the connection,
+ so a field it did not publish is simply a header we do not send.
+ """
+ return {header: str(hello[field]) for field, header in HELLO_HEADERS.items()
+ if field in hello}
# ---------- runtime state file ----------
diff --git a/backend/app/services/connect_policy.py b/backend/app/services/connect_policy.py
index b3f16898..a20690c5 100644
--- a/backend/app/services/connect_policy.py
+++ b/backend/app/services/connect_policy.py
@@ -173,7 +173,7 @@ def facts_frame(stream_id: str, facts: dict) -> dict:
@handler('policy.report')
def report(args: dict, app=None) -> dict:
- """"Check now". Build a document and push it up the policy stream."""
+ """"Check now": build a document and push it up the policy stream."""
facts = build_facts(app)
client = _relay_client()
if client is None or not client.publish_policy(facts):
@@ -270,14 +270,17 @@ def _in_app(app, fn):
def _systemctl(action: str, unit: str):
- """True, or the reason it did not work. Never raises."""
- import shutil
- import subprocess
- if shutil.which('systemctl') is None:
+ """True, or the reason it did not work. Never raises.
+
+ run_privileged() adds sudo when the panel is not root, which is the
+ difference between a repair that works off a packaged install and one
+ that reports a permission error the operator cannot act on.
+ """
+ from app.utils.system import is_command_available, run_privileged
+ if not is_command_available('systemctl'):
return 'this host does not use systemd'
try:
- out = subprocess.run(['systemctl', action, unit], capture_output=True,
- text=True, timeout=60)
+ out = run_privileged(['systemctl', action, unit], timeout=60)
except Exception as exc:
return str(exc)
if out.returncode == 0: