mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-11-09 05:24:49 +09:00
When the panel sits behind a reverse proxy / tunnel, browsers and agents arrive from SERVERKIT_PUBLIC_URL. Without that origin in CORS_ORIGINS the engine.io WS handshake rejects it and every Socket.IO connection 400s with "not an accepted origin". Auto-append SERVERKIT_PUBLIC_URL (deduped, whitespace-stripped) so operators configure the public URL in one place. Also bumps Flask-SocketIO 5.4.1 -> 5.6.1. Recovered from a local stash. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
112 lines
4.5 KiB
Python
112 lines
4.5 KiB
Python
import os
|
|
import sys
|
|
import warnings
|
|
from datetime import timedelta
|
|
|
|
# Default insecure keys that must be changed in production
|
|
INSECURE_SECRET_KEYS = [
|
|
'dev-secret-key-change-in-production',
|
|
'jwt-secret-key-change-in-production',
|
|
'change-this-to-a-secure-random-string',
|
|
'change-this-to-another-secure-random-string',
|
|
]
|
|
|
|
|
|
class Config:
|
|
SECRET_KEY = os.environ.get('SECRET_KEY', 'dev-secret-key-change-in-production')
|
|
|
|
# Database - use instance folder for Flask convention
|
|
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', 'sqlite:////app/instance/serverkit.db')
|
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
|
|
|
# JWT
|
|
JWT_SECRET_KEY = os.environ.get('JWT_SECRET_KEY', 'jwt-secret-key-change-in-production')
|
|
JWT_ACCESS_TOKEN_EXPIRES = timedelta(minutes=15)
|
|
JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=30)
|
|
|
|
# CORS - allow the launcher defaults plus legacy local dev ports.
|
|
DEFAULT_CORS_ORIGINS = ','.join([
|
|
'http://localhost:41921',
|
|
'http://127.0.0.1:41921',
|
|
'http://localhost:47927',
|
|
'http://127.0.0.1:47927',
|
|
'http://localhost:5173',
|
|
'http://127.0.0.1:5173',
|
|
'http://localhost:5000',
|
|
'http://127.0.0.1:5000',
|
|
])
|
|
_cors_raw = os.environ.get('CORS_ORIGINS', DEFAULT_CORS_ORIGINS)
|
|
CORS_ORIGINS = [o.strip() for o in _cors_raw.split(',') if o.strip()]
|
|
# Whenever the panel sits behind a reverse proxy / tunnel, agents and
|
|
# browsers come from SERVERKIT_PUBLIC_URL — auto-allow it so the user
|
|
# only has to configure that public URL in one place. Without this
|
|
# the engine.io WS handshake rejects the public origin and every
|
|
# Socket.IO connection 400s with "not an accepted origin".
|
|
_public_url = os.environ.get('SERVERKIT_PUBLIC_URL', '').strip().rstrip('/')
|
|
if _public_url and _public_url not in CORS_ORIGINS:
|
|
CORS_ORIGINS.append(_public_url)
|
|
|
|
|
|
class DevelopmentConfig(Config):
|
|
DEBUG = True
|
|
|
|
@classmethod
|
|
def init_app(cls, app):
|
|
if app.config.get('SECRET_KEY') == 'dev-secret-key-change-in-production':
|
|
warnings.warn('WARNING: Using default SECRET_KEY. Change before deploying.')
|
|
if app.config.get('JWT_SECRET_KEY') == 'jwt-secret-key-change-in-production':
|
|
warnings.warn('WARNING: Using default JWT_SECRET_KEY. Change before deploying.')
|
|
|
|
|
|
class TestingConfig(Config):
|
|
"""Config for pytest and other automated tests."""
|
|
TESTING = True
|
|
DEBUG = True
|
|
SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL', 'sqlite:///:memory:')
|
|
# Reduce noise during tests
|
|
JWT_ACCESS_TOKEN_EXPIRES = timedelta(minutes=5)
|
|
|
|
|
|
class ProductionConfig(Config):
|
|
DEBUG = False
|
|
|
|
# Secure session cookies in production
|
|
SESSION_COOKIE_SECURE = True
|
|
SESSION_COOKIE_HTTPONLY = True
|
|
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)
|
|
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)
|
|
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:
|
|
raise ValueError('CRITICAL: SECRET_KEY must be changed for production deployment')
|
|
if app.config['JWT_SECRET_KEY'] in insecure_keys:
|
|
raise ValueError('CRITICAL: JWT_SECRET_KEY must be changed for production deployment')
|
|
# Validate CORS origins
|
|
cors_raw = os.environ.get('CORS_ORIGINS', '')
|
|
cors_origins = [o.strip() for o in cors_raw.split(',') if o.strip()]
|
|
if not cors_origins:
|
|
raise ValueError('CORS_ORIGINS must be explicitly set in production')
|
|
app.config['CORS_ORIGINS'] = cors_origins
|
|
|
|
|
|
config = {
|
|
'development': DevelopmentConfig,
|
|
'testing': TestingConfig,
|
|
'production': ProductionConfig,
|
|
'default': DevelopmentConfig,
|
|
}
|