Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Secret Key Compromised

I am wondering what are the steps one would need to take should the production secret key become compromised. Luckily, that is not the case, but it would be good to know, nonetheless.

In particular, what happens if one simply swaps the old key to a newly generated one? Since it is used in making hashes, does it break the entire system or make it unstable?

In the case of a compromise, would the response be as simple as generating a new key and inserting it into the production settings?

like image 318
MadPhysicist Avatar asked Oct 27 '19 03:10

MadPhysicist


People also ask

What can someone do with Django secret key?

Summary: The Django secret key is used to provide cryptographic signing. This key is mostly used to sign session cookies. If one were to have this key, they would be able to modify the cookies sent by the application.

What happens if you change Django secret key?

Once you change the SECRET_KEY on production, all the old sessions and cookies are invalidated, users are logged out and data in sessions are lost. This is good if your SECRET_KEY is compromised!

How long is Django secret key?

Length, forbidden prefix, and unique characters To put that into plain English, your SECRET_KEY must: Be a minimum of 50 characters in length.


1 Answers

The SECRET_KEY is used for the following:

  • 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.

"If you rotate your secret key, all of the above will be invalidated. Secret keys are not used for passwords of users and key rotation will not affect them."

You can use the following function to generate a new key:

from django.core.management.utils import get_random_secret_key

print(get_random_secret_key())

Simply copy/paste the printed results into your settings.py.

like image 104
Lord Elrond Avatar answered Oct 19 '22 19:10

Lord Elrond