feat(security): gate panel HSTS on installer SSL mode (keep HTTPS optional)

The Flask middleware emitted HSTS with includeSubDomains+preload whenever not in
debug mode -- even on HTTP-only or Cloudflare-Flexible installs, where preload is
a hard-to-reverse commitment. HTTPS is intentionally optional, so HSTS is now
gated on the operator's recorded SSL choice.

- config._resolve_ssl_mode(): SERVERKIT_SSL_MODE env -> /etc/serverkit/ssl-mode
  file -> safe 'insecure' default. Exposed as SSL_MODE / HSTS_ENABLED.
- security middleware sends HSTS only when HSTS_ENABLED and not debug. Behind a
  proxy Flask can't distinguish real TLS from a Flexible edge via
  X-Forwarded-Proto, so we trust the installer's choice, not the request scheme.
- install.sh records SERVERKIT_SSL_MODE in .env; existing installs are covered by
  the /etc/serverkit/ssl-mode file. The nginx edge still emits HSTS in its own
  secure server block, so secure installs are unaffected.

Proving tests in tests/test_security_headers.py. Full backend suite: 342 passed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Juan DenisandClaude Opus 4.8 committed 2026-06-19 16:52:40 -04:00
1 parent 49611a38f3
commit a09bf0fbbe
4 files changed
+97 -3

No files matched your search

+7 -3
View File
@@ -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 response
+30
View File
@@ -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 = True
+55
View File
@@ -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'
+5
View File
@@ -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
# Ports
PORT=80
SSL_PORT=443