Fail closed in production if SECRET_KEY is missing or the placeholder default (S2)

base.py's env.str() fallback is a publicly known string in this repo's
history. Running prod settings with it (an unset or forgotten .env
value) would sign sessions/CSRF tokens with a key anyone can read.
install.sh already generates a real random key for fresh installs, so
this only blocks the broken case.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-31 12:02:58 -06:00
1 parent 9d91da6fc6
commit 56eb2bc498
1 file changed
+15 -1
+15 -1
View File
@@ -1,8 +1,22 @@
from django.core.exceptions import ImproperlyConfigured
from .base import * # noqa: F403 from .base import * # noqa: F403
from .base import env from .base import SECRET_KEY, env
DEBUG = False DEBUG = False
# base.py falls back to a publicly known placeholder if SECRET_KEY isn't
# set — fine for a throwaway dev DB, but production must never sign
# sessions/CSRF tokens with a key anyone can read in this repo's history.
# Fail startup rather than quietly running with a known key.
if not SECRET_KEY or SECRET_KEY == "django-insecure-change-me-in-production":
raise ImproperlyConfigured(
"SECRET_KEY is missing or still the placeholder default. Set a unique, "
"random SECRET_KEY in .env before running with prod settings — generate "
'one with: python -c "from django.core.management.utils import '
'get_random_secret_key; print(get_random_secret_key())"'
)
# Health checks (scripts/upgrade.sh, an external uptime monitor, ...) hit # Health checks (scripts/upgrade.sh, an external uptime monitor, ...) hit
# gunicorn directly over loopback, bypassing the reverse proxy entirely — # gunicorn directly over loopback, bypassing the reverse proxy entirely —
# that request's Host header is "127.0.0.1:<port>", not the public domain # that request's Host header is "127.0.0.1:<port>", not the public domain