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(config): optional Redis-backed rate-limit storage via RATELIMIT_STORAGE_URI
Juan Denis5aaaab9905f2
diff --git a/.env.example b/.env.exampleindex ed582c3a..fe7deb14 100644--- a/.env.example+++ b/.env.example@@ -91,6 +91,14 @@ TRUSTED_PROXY_HOPS=1# AUTH_IP_WINDOW_MINUTES=15# AUTH_IP_BLOCK_MINUTES=15+# ----------------------------------------+# Rate-limit storage backend+# ----------------------------------------+# Default is in-memory, which matches the deliberate single-worker gunicorn+# design (see docs/ARCHITECTURE.md). Point at Redis to share limit state+# across processes and silence flask-limiter's in-memory production warning.+# RATELIMIT_STORAGE_URI=redis://localhost:6379/0+# ----------------------------------------# Environment# ----------------------------------------diff --git a/backend/app/__init__.py b/backend/app/__init__.pyindex 75ec993a..92896c08 100644--- a/backend/app/__init__.py+++ b/backend/app/__init__.py@@ -110,6 +110,8 @@ def create_app(config_name=None):db.init_app(app)migrate.init_app(app, db)jwt.init_app(app)+ # Storage backend comes from app.config's RATELIMIT_STORAGE_URI when set+ # (see config.py); unset = in-memory, correct for the single-worker design.limiter.init_app(app)# SQLite concurrency tuning: the queue-bus consumers, job system anddiff --git a/backend/config.py b/backend/config.pyindex 5d71ca5c..5bfe0736 100644--- a/backend/config.py+++ b/backend/config.py@@ -72,6 +72,16 @@ class Config:JWT_ACCESS_TOKEN_EXPIRES = timedelta(minutes=15)JWT_REFRESH_TOKEN_EXPIRES = timedelta(days=30)+ # Rate-limit storage backend. flask-limiter reads RATELIMIT_STORAGE_URI+ # from app.config at init_app time. Unset = in-memory storage, which is+ # correct for the deliberate single-worker deployment but per-process and+ # triggers flask-limiter's "not recommended for production" warning. Set+ # e.g. redis://localhost:6379/0 to share limit state across processes.+ # Defined only when the env var is non-empty so a blank value never+ # reaches the storage URI parser.+ if os.environ.get('RATELIMIT_STORAGE_URI'):+ RATELIMIT_STORAGE_URI = os.environ['RATELIMIT_STORAGE_URI']+# CORS - allow the launcher defaults plus legacy local dev ports.DEFAULT_CORS_ORIGINS = ','.join(['http://localhost:41921',diff --git a/backend/tests/test_rate_limit_storage.py b/backend/tests/test_rate_limit_storage.pynew file mode 100644index 00000000..4286c2e4--- /dev/null+++ b/backend/tests/test_rate_limit_storage.py@@ -0,0 +1,84 @@+"""RATELIMIT_STORAGE_URI plumbing (flask-limiter storage backend).++Default stays in-memory — correct for the deliberate single-worker+deployment. Setting the env var surfaces the key in app.config, where+flask-limiter's init_app picks it up, which both shares limit state across+processes and silences the "in-memory storage ... not recommended for+production" warning.+"""+import importlib.util+import os+import warnings++import pytest++CONFIG_PATH = os.path.join(+ os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'config.py')+++def probe_config(monkeypatch):+ """Re-evaluate config.py's class body under a patched environment.++ Private throwaway module, deliberately NOT importlib.reload(config) —+ see test_external_reverse_proxy.py's probe_config for why reload is a+ trap.+ """+ def _load(**env):+ for key, value in env.items():+ if value is None:+ monkeypatch.delenv(key, raising=False)+ else:+ monkeypatch.setenv(key, value)+ spec = importlib.util.spec_from_file_location('_sk_config_probe', CONFIG_PATH)+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module++ return _load+++def test_storage_uri_unset_stays_out_of_config(probe_config):+ """Unset (or empty) env var must not leak a blank URI into app.config —+ flask-limiter would hand it to the storage URI parser."""+ probed = probe_config(RATELIMIT_STORAGE_URI=None)+ assert not hasattr(probed.Config, 'RATELIMIT_STORAGE_URI')+ probed = probe_config(RATELIMIT_STORAGE_URI='')+ assert not hasattr(probed.Config, 'RATELIMIT_STORAGE_URI')+++def test_storage_uri_env_is_picked_up(probe_config):+ probed = probe_config(RATELIMIT_STORAGE_URI='redis://localhost:6379/0')+ assert probed.Config.RATELIMIT_STORAGE_URI == 'redis://localhost:6379/0'+ # Inherits into every environment-specific config.+ assert probed.TestingConfig.RATELIMIT_STORAGE_URI == 'redis://localhost:6379/0'+++def test_config_key_silences_in_memory_warning():+ """flask-limiter's init_app reads RATELIMIT_STORAGE_URI from app.config —+ the mechanism our config key relies on. A bare Flask app with a fresh+ Limiter keeps this clear of the shared module-level one."""+ from flask import Flask+ from flask_limiter import Limiter+ from flask_limiter.util import get_remote_address++ bare = Flask(__name__)+ bare.config['RATELIMIT_STORAGE_URI'] = 'memory://'+ with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter('always')+ Limiter(key_func=get_remote_address).init_app(bare)+ assert not [w for w in caught if 'in-memory storage' in str(w.message)]+++def test_missing_config_key_still_warns():+ """Control for the test above: without the key the warning fires, so its+ absence above is the config doing the work, not a dead assertion."""+ from flask import Flask+ from flask_limiter import Limiter+ from flask_limiter.util import get_remote_address++ bare = Flask(__name__)+ with warnings.catch_warnings(record=True) as caught:+ warnings.simplefilter('always')+ Limiter(key_func=get_remote_address).init_app(bare)+ assert any('in-memory storage' in str(w.message) for w in caught)