Details
Suggested Fix
Fail-closed at import time when the secret is the default, irrespective of PLATFORM_ENV. Permit explicit dev-mode opt-in with a separate variable that is NEVER the default.
--- a/src/praisonai-platform/praisonai_platform/services/auth_service.py
+++ b/src/praisonai-platform/praisonai_platform/services/auth_service.py
@@ -23,12 +23,16 @@
_pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
-_DEFAULT_SECRET = "dev-secret-change-me"
-JWT_SECRET = os.environ.get("PLATFORM_JWT_SECRET", _DEFAULT_SECRET)
+JWT_SECRET = os.environ.get("PLATFORM_JWT_SECRET")
JWT_ALGORITHM = "HS256"
JWT_TTL_SECONDS = int(os.environ.get("PLATFORM_JWT_TTL", str(30 * 24 * 3600)))
-if JWT_SECRET == _DEFAULT_SECRET and os.environ.get("PLATFORM_ENV", "dev") != "dev":
- raise RuntimeError(
- "PLATFORM_JWT_SECRET must be set to a strong random value in production. "
- "Set PLATFORM_ENV=dev to suppress this check during development."
- )
+if not JWT_SECRET:
+ if os.environ.get("PRAISONAI_PLATFORM_ALLOW_INSECURE_JWT") != "1":
+ raise RuntimeError(
+ "PLATFORM_JWT_SECRET must be set to a strong random value (min 32 bytes). "
+ "For local development, set PRAISONAI_PLATFORM_ALLOW_INSECURE_JWT=1 to "
+ "auto-generate an ephemeral random secret per process."
+ )
+ import secrets
+ JWT_SECRET = secrets.token_urlsafe(32)
+ # ephemeral; tokens issued before restart will not validate after restart
+ import warnings
+ warnings.warn("Using ephemeral JWT secret; set PLATFORM_JWT_SECRET in production")
The guard now fails-closed: an unset PLATFORM_JWT_SECRET raises at import unless the operator explicitly opts into dev mode with a separate variable. The dev-mode path generates a per-process random secret instead of using a hardcoded one, so even leaked dev-mode tokens cannot be used against another deployment. Add a startup banner that prints the JWT secret's hash prefix (not the secret itself) so operators can confirm at runtime which key is in use.