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.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
feat(security): gate panel HSTS on installer SSL mode (keep HTTPS optional)
Juan Denisa09bf0fbbe27
diff --git a/backend/app/middleware/security.py b/backend/app/middleware/security.pyindex 454db759..e78fd842 100644--- a/backend/app/middleware/security.py+++ b/backend/app/middleware/security.py@@ -53,9 +53,13 @@ def register_security_headers(app: Flask):# Permissions Policy (formerly Feature-Policy)response.headers['Permissions-Policy'] = 'geolocation=(), microphone=(), camera=()'- # HSTS - only in production and on HTTPS- # 63072000 seconds = 2 years, matching the nginx edge config and preload requirements- if not app.debug:+ # HSTS - only when the deployment actually terminates HTTPS. The installer+ # records that choice (SSL_MODE / HSTS_ENABLED in config); we never+ # advertise HSTS or `preload` on a plain-HTTP or Cloudflare-Flexible+ # install, since that's a hard-to-reverse commitment and HTTPS is+ # intentionally optional. 63072000s = 2 years, matching the nginx edge+ # config and the preload-list requirements.+ if app.config.get('HSTS_ENABLED') and not app.debug:response.headers['Strict-Transport-Security'] = 'max-age=63072000; includeSubDomains; preload'return responsediff --git a/backend/config.py b/backend/config.pyindex deb464ce..270e31b2 100644--- a/backend/config.py+++ b/backend/config.py@@ -12,6 +12,29 @@ INSECURE_SECRET_KEYS = []+def _resolve_ssl_mode():+ """Whether this deployment terminates real end-to-end HTTPS.++ Set by the installer via the ``SERVERKIT_SSL_MODE`` env var or the+ ``/etc/serverkit/ssl-mode`` file. Defaults to ``'insecure'`` so we never+ advertise HSTS/preload on a plain-HTTP or Cloudflare-Flexible deployment —+ that's a hard-to-reverse browser commitment, and HTTPS is intentionally+ optional in ServerKit. Behind a proxy Flask can't tell real TLS from a+ Flexible edge via X-Forwarded-Proto, so we trust the operator's choice.+ """+ mode = os.environ.get('SERVERKIT_SSL_MODE', '').strip().lower()+ if mode in ('secure', 'insecure'):+ return mode+ try:+ with open('/etc/serverkit/ssl-mode', 'r') as fh:+ mode = fh.read().strip().lower()+ if mode in ('secure', 'insecure'):+ return mode+ except OSError:+ pass+ return 'insecure'++class Config:SECRET_KEY = os.environ.get('SECRET_KEY', 'dev-secret-key-change-in-production')@@ -58,6 +81,13 @@ class Config:# DNS records via a connected provider.SERVER_PUBLIC_IP = os.environ.get('SERVER_PUBLIC_IP', '')+ # Whether the deployment serves real end-to-end HTTPS. Gates the HSTS+ # response header so the panel never forces/preloads SSL on an HTTP-only or+ # Cloudflare-Flexible install (HTTPS is optional). The nginx edge config+ # still emits HSTS in its own secure server block independently of this.+ SSL_MODE = _resolve_ssl_mode()+ HSTS_ENABLED = SSL_MODE == 'secure'+class DevelopmentConfig(Config):DEBUG = Truediff --git a/backend/tests/test_security_headers.py b/backend/tests/test_security_headers.pynew file mode 100644index 00000000..bf8659ec--- /dev/null+++ b/backend/tests/test_security_headers.py@@ -0,0 +1,55 @@+"""Proving tests — HSTS is gated on the operator's SSL choice.++HTTPS is optional in ServerKit, so the panel must NOT advertise HSTS/preload+(a hard-to-reverse browser commitment) unless the deployment actually+terminates HTTPS. The installer records that via SSL_MODE/HSTS_ENABLED; behind+a proxy Flask can't tell real TLS from a Cloudflare-Flexible edge, so we trust+the recorded choice rather than the request scheme.+"""++import config as cfg+++def _make_app(hsts_enabled, debug):+ from flask import Flask+ from app.middleware.security import register_security_headers+ app = Flask(__name__)+ app.debug = debug+ app.config['HSTS_ENABLED'] = hsts_enabled+ register_security_headers(app)++ @app.route('/ping')+ def ping():+ return 'ok'++ return app+++def test_hsts_sent_in_secure_production():+ r = _make_app(hsts_enabled=True, debug=False).test_client().get('/ping')+ hsts = r.headers.get('Strict-Transport-Security')+ assert hsts and 'preload' in hsts and 'includeSubDomains' in hsts+++def test_hsts_absent_in_insecure_mode():+ r = _make_app(hsts_enabled=False, debug=False).test_client().get('/ping')+ assert 'Strict-Transport-Security' not in r.headers+ # The non-committal hardening headers are still applied.+ assert r.headers.get('X-Content-Type-Options') == 'nosniff'+ assert 'Content-Security-Policy' in r.headers+++def test_hsts_absent_in_debug_even_if_secure():+ r = _make_app(hsts_enabled=True, debug=True).test_client().get('/ping')+ assert 'Strict-Transport-Security' not in r.headers+++def test_resolve_ssl_mode(monkeypatch):+ monkeypatch.setenv('SERVERKIT_SSL_MODE', 'secure')+ assert cfg._resolve_ssl_mode() == 'secure'+ # Unrecognised value falls through; no /etc/serverkit/ssl-mode on the test+ # box, so it lands on the safe default.+ monkeypatch.setenv('SERVERKIT_SSL_MODE', 'bogus')+ assert cfg._resolve_ssl_mode() == 'insecure'+ monkeypatch.delenv('SERVERKIT_SSL_MODE', raising=False)+ assert cfg._resolve_ssl_mode() == 'insecure'diff --git a/install.sh b/install.shindex 7848718a..2fb4fffe 100644--- a/install.sh+++ b/install.sh@@ -543,6 +543,11 @@ CORS_ORIGINS=$cors_origins# Public URL for agents and install commands (optional)${public_url:+# SERVERKIT_PUBLIC_URL=$public_url}+# SSL mode (secure|insecure) — gates the panel's HSTS header so HTTPS stays+# optional. Mirrors /etc/serverkit/ssl-mode; set to 'secure' only when this+# server terminates real end-to-end HTTPS.+SERVERKIT_SSL_MODE=$SSL_MODE+# PortsPORT=80SSL_PORT=443