Skip to content

Commit ed1f1b8

Browse files
authored
Use a randomised Django secret key if one isn't set explicitly (#201)
The Django secret key is no longer hardcoded to an example value, instead using a randomised value if the recommend env var is not set. The previous implementation could be insecure if users didn't set an explicit value, and also encouraged secrets being committed to source rather than the use of env vars. The name of the env var has also been renamed from `SECRET_KEY` to `DJANGO_SECRET_KEY`.
1 parent 20b3df7 commit ed1f1b8

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"keywords": ["python", "django"],
77
"addons": ["heroku-postgresql"],
88
"env": {
9-
"SECRET_KEY": {
9+
"DJANGO_SECRET_KEY": {
1010
"description": "The secret key for the Django application.",
1111
"generator": "secret"
1212
}

gettingstarted/settings.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,35 @@
1010
https://docs.djangoproject.com/en/4.2/ref/settings/
1111
"""
1212

13-
import dj_database_url
1413
import os
15-
from django.test.runner import DiscoverRunner
14+
import secrets
1615
from pathlib import Path
1716

17+
import dj_database_url
18+
from django.test.runner import DiscoverRunner
19+
1820
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1921
BASE_DIR = Path(__file__).resolve().parent.parent
2022

2123

2224
IS_HEROKU = "DYNO" in os.environ
2325

24-
# Quick-start development settings - unsuitable for production
26+
# Before using your Heroku app in production, make sure to review Django's deployment checklist:
2527
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
2628

29+
# Django requires a unique secret key for each Django app, that is used by several of its
30+
# security features. To simplify initial setup (without hardcoding the secret in the source
31+
# code) we set this to a random value every time the app starts. However, this will mean many
32+
# Django features break whenever an app restarts (for example, sessions will be logged out).
33+
# In your production Heroku apps you should set the `DJANGO_SECRET_KEY` config var explicitly.
34+
# Make sure to use a long unique value, like you would for a password. See:
35+
# https://docs.djangoproject.com/en/4.2/ref/settings/#std-setting-SECRET_KEY
36+
# https://devcenter.heroku.com/articles/config-vars
2737
# SECURITY WARNING: keep the secret key used in production secret!
28-
SECRET_KEY = "CHANGE_ME!!!! (P.S. the SECRET_KEY environment variable will be used, if set, instead)."
29-
30-
if "SECRET_KEY" in os.environ:
31-
SECRET_KEY = os.environ["SECRET_KEY"]
38+
SECRET_KEY = os.environ.get(
39+
"DJANGO_SECRET_KEY",
40+
default=secrets.token_urlsafe(nbytes=64),
41+
)
3242

3343
# SECURITY WARNING: don't run with debug turned on in production!
3444
if not IS_HEROKU:

0 commit comments

Comments
 (0)