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.

harden: broaden insecure-secret detection, bind API to loopback, fix break-glass sub

Juan Denis69123a8b807b
diff --git a/backend/app/services/cli_api_client.py b/backend/app/services/cli_api_client.py
index a58ea658..b9963aa1 100644
--- a/backend/app/services/cli_api_client.py
+++ b/backend/app/services/cli_api_client.py
@@ -80,7 +80,10 @@ def _mint_in_context():
if user is None:
raise CliApiError('No active admin user found — create one with "serverkit create-admin".')
- token = create_access_token(identity=user.id, expires_delta=BREAKGLASS_TTL)
+ # Identity must be a string — flask-jwt-extended rejects a non-string
+ # `sub` at decode time with a 422, which otherwise breaks every CLI call
+ # that hits the panel API with a break-glass token.
+ token = create_access_token(identity=str(user.id), expires_delta=BREAKGLASS_TTL)
try:
from app.services.audit_service import AuditService
diff --git a/backend/config.py b/backend/config.py
index 0551fae1..b882f147 100644
--- a/backend/config.py
+++ b/backend/config.py
@@ -12,6 +12,26 @@ INSECURE_SECRET_KEYS = [
]
+def is_insecure_secret(value):
+ """True when a key is an obvious dev/placeholder value unfit for production.
+
+ Matches the known defaults *plus* any value carrying a dev marker, so a
+ variant the exact list doesn't enumerate (e.g. 'dev-jwt-...') can't slip
+ through. A real random/hex secret has none of these markers.
+ """
+ if not value:
+ return True
+ if value in INSECURE_SECRET_KEYS:
+ return True
+ lowered = value.lower()
+ return (
+ lowered.startswith('dev-')
+ or lowered.startswith('jwt-secret-key')
+ or 'change-in-production' in lowered
+ or 'change-this' in lowered
+ )
+
+
def _resolve_ssl_mode():
"""Whether this deployment terminates real end-to-end HTTPS.
@@ -135,24 +155,23 @@ class ProductionConfig(Config):
SESSION_COOKIE_SAMESITE = 'Lax'
def __init__(self):
- # Validate that secret keys are not default values in production
- if self.SECRET_KEY in INSECURE_SECRET_KEYS:
- print("FATAL: SECRET_KEY is set to a default insecure value in production mode!", file=sys.stderr)
+ # Validate that secret keys are not default/dev values in production
+ if is_insecure_secret(self.SECRET_KEY):
+ print("FATAL: SECRET_KEY is set to an insecure/default value in production mode!", file=sys.stderr)
print("Generate a secure key with: python -c \"import secrets; print(secrets.token_hex(32))\"", file=sys.stderr)
sys.exit(1)
- if self.JWT_SECRET_KEY in INSECURE_SECRET_KEYS:
- print("FATAL: JWT_SECRET_KEY is set to a default insecure value in production mode!", file=sys.stderr)
+ if is_insecure_secret(self.JWT_SECRET_KEY):
+ print("FATAL: JWT_SECRET_KEY is set to an insecure/default value in production mode!", file=sys.stderr)
print("Generate a secure key with: python -c \"import secrets; print(secrets.token_hex(32))\"", file=sys.stderr)
sys.exit(1)
@classmethod
def init_app(cls, app):
"""Validate production configuration."""
- insecure_keys = ['dev-secret-key-change-in-production', 'jwt-secret-key-change-in-production']
- if app.config['SECRET_KEY'] in insecure_keys:
+ if is_insecure_secret(app.config['SECRET_KEY']):
raise ValueError('CRITICAL: SECRET_KEY must be changed for production deployment')
- if app.config['JWT_SECRET_KEY'] in insecure_keys:
+ if is_insecure_secret(app.config['JWT_SECRET_KEY']):
raise ValueError('CRITICAL: JWT_SECRET_KEY must be changed for production deployment')
# Validate CORS origins
cors_raw = os.environ.get('CORS_ORIGINS', '')
diff --git a/install.sh b/install.sh
index c53056f1..6266d622 100644
--- a/install.sh
+++ b/install.sh
@@ -1442,10 +1442,15 @@ EOF
render_service_unit() {
local out="$1"
local template="$INSTALL_DIR/templates/serverkit-backend.service.in"
+ # Bind the API to loopback by default — host nginx fronts it on :80/:443, so
+ # the raw gunicorn port must not be world-reachable. Operators who front it
+ # differently can override with SERVERKIT_BIND_HOST=0.0.0.0.
+ local bind_host="${SERVERKIT_BIND_HOST:-127.0.0.1}"
if [ -f "$template" ]; then
sed -e "s|@SERVERKIT_DIR@|$INSTALL_DIR|g" \
-e "s|@SERVERKIT_VENV_DIR@|$VENV_DIR|g" \
-e "s|@PORT@|5000|g" \
+ -e "s|@BIND_HOST@|$bind_host|g" \
-e "s|@USER@|root|g" \
-e "s|@LOG_DIR@|$LOG_DIR|g" \
"$template" > "$out"
diff --git a/templates/serverkit-backend.service.in b/templates/serverkit-backend.service.in
index 4cd72115..20bf6475 100644
--- a/templates/serverkit-backend.service.in
+++ b/templates/serverkit-backend.service.in
@@ -18,7 +18,7 @@ EnvironmentFile=@SERVERKIT_DIR@/.env
# on the raw socket — a gevent-websocket worker ALSO answers the upgrade,
# producing a double 101 handshake the browser reports as
# "Invalid frame header" (WS then silently degrades to polling).
-ExecStart=@SERVERKIT_VENV_DIR@/bin/gunicorn -w 1 --threads 100 -b 0.0.0.0:@PORT@ --timeout 120 --access-logfile @LOG_DIR@/access.log --error-logfile @LOG_DIR@/error.log run:app
+ExecStart=@SERVERKIT_VENV_DIR@/bin/gunicorn -w 1 --threads 100 -b @BIND_HOST@:@PORT@ --timeout 120 --access-logfile @LOG_DIR@/access.log --error-logfile @LOG_DIR@/error.log run:app
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
RestartSec=5