Django Secret Key

ID

django_secret_key

Severity

high

Vendor

Django

Family

Generic secret

Description

A secret key for a particular Django installation. This is used to provide cryptographic signing, and should be set to a unique, unpredictable value.

Security

Keep this value secret. Running Django with a known SECRET_KEY defeats many of Django’s security protections, and can lead to privilege escalation and remote code execution vulnerabilities.

The secret key is used for:

  • All sessions if you are using any other session backend than django.contrib.sessions.backends.cache, or are using the default get_session_auth_hash().

  • All messages if you are using CookieStorage or FallbackStorage.

  • All PasswordResetView tokens.

  • Any usage of cryptographic signing, unless a different key is provided.

Accidentally checking-in the key to source control repositories could compromise your Django server, allowing an attacker to be able to modify the cookies sent by the application.

Examples

SECRET_KEY = 'qolwvjicds5p53gvod1pyrz*%2uykjw&a^&c4moab!w=&16ou7'

Mitigation / Fix

  1. Remove the SECRET_KEY from the source code or committed configuration file.

  2. Follow your policy for handling leaked secrets, which typically require revoking the secret in the target system(s). To revoke the key, a new secret needs to be generated. All sessions or cookies signed with the key will be invalided.

When rotating your secret key, you should move the old key to SECRET_KEY_FALLBACKS temporarily. Secret keys are not used for passwords of users and key rotation will not affect them. See Settings SECRET KEY for more information.
  1. If under a git repository, you may remove unwanted files from the repository history using tools like git filter-repo or BFG Repo-Cleaner. You may follow the procedure listed here for GitHub.

You should consider any sensitive data in commits with secrets as compromised.

Remember that secrets may be removed from history in your projects, but not in other users' cloned or forked repositories.