Bird
Raised Fist0
Djangoframework~20 mins

SECRET_KEY and security settings in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Django Security Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why is the SECRET_KEY important in Django?
What is the main purpose of the SECRET_KEY in a Django project?
AIt stores the database connection password securely.
BIt is used to encrypt user passwords in the database.
CIt signs cookies and tokens to protect against tampering.
DIt controls the debug mode of the Django application.
Attempts:
2 left
💡 Hint
Think about what needs to be protected from being changed by attackers.
❓ component_behavior
intermediate
1:30remaining
Effect of DEBUG=True on security
What happens if you leave DEBUG = True in your Django settings on a public website?
ADetailed error pages with sensitive info are shown to all users.
BThe site runs faster because debugging is enabled.
CThe SECRET_KEY is automatically hidden from logs.
DThe database connection is encrypted automatically.
Attempts:
2 left
💡 Hint
Consider what information an attacker might see if debugging is on.
📝 Syntax
advanced
2:00remaining
Correct way to set SECRET_KEY in settings.py
Which option correctly sets the SECRET_KEY in Django's settings.py using an environment variable with a fallback?
ASECRET_KEY = os.environ['DJANGO_SECRET_KEY'] or 'defaultsecret'
BSECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'defaultsecret')
CSECRET_KEY = os.get('DJANGO_SECRET_KEY', 'defaultsecret')
DSECRET_KEY = os.environ.get('DJANGO_SECRET_KEY') ?? 'defaultsecret'
Attempts:
2 left
💡 Hint
Check the correct method to get environment variables with a default value in Python.
🔧 Debug
advanced
1:30remaining
Identify the error in this security setting
What error will this Django setting cause?
SECURE_SSL_REDIRECT = 'True'
ATypeError because the value should be a boolean, not a string.
BSyntaxError due to quotes around True.
CRuntimeWarning about insecure connection.
DNo error; the setting works as expected.
Attempts:
2 left
💡 Hint
Check the data type expected for this setting.
❓ state_output
expert
2:00remaining
Result of missing SECRET_KEY in production
What happens when you run a Django project in production mode without setting a SECRET_KEY?
ADjango generates a random SECRET_KEY each time the server starts.
BDjango disables session and cookie security features silently.
CDjango runs normally but logs a warning about missing SECRET_KEY.
DDjango raises an ImproperlyConfigured error and refuses to start.
Attempts:
2 left
💡 Hint
Think about how critical SECRET_KEY is for Django's security.

Practice

(1/5)
1. What is the main purpose of the SECRET_KEY in a Django project?
easy
A. To define the database connection string
B. To secure sessions and cryptographic signing
C. To list allowed hosts for the app
D. To enable debug mode during development

Solution

  1. Step 1: Understand the role of SECRET_KEY

    The SECRET_KEY is used by Django to provide cryptographic signing, which secures sessions and other data.
  2. Step 2: Differentiate from other settings

    Database connection strings, allowed hosts, and debug mode serve different purposes unrelated to cryptographic security.
  3. Final Answer:

    To secure sessions and cryptographic signing -> Option B
  4. Quick Check:

    SECRET_KEY secures sessions = B [OK]
Hint: SECRET_KEY protects data and sessions, not database or hosts [OK]
Common Mistakes:
  • Confusing SECRET_KEY with database settings
  • Thinking SECRET_KEY controls allowed hosts
  • Assuming SECRET_KEY enables debug mode
2. Which of the following is the correct way to set a SECRET_KEY in Django's settings.py?
easy
A. SECRET_KEY = 'a-very-long-random-string'
B. SECRET_KEY = None
C. SECRET_KEY = True
D. SECRET_KEY = 1234567890

Solution

  1. Step 1: Check the type of SECRET_KEY

    The SECRET_KEY must be a long, random string enclosed in quotes (a string).
  2. Step 2: Evaluate each option

    SECRET_KEY = 1234567890 is a number, not a string. SECRET_KEY = True is a boolean. SECRET_KEY = None is None. Only SECRET_KEY = 'a-very-long-random-string' is a proper string.
  3. Final Answer:

    SECRET_KEY = 'a-very-long-random-string' -> Option A
  4. Quick Check:

    SECRET_KEY must be a string = D [OK]
Hint: SECRET_KEY must be a string, not number or boolean [OK]
Common Mistakes:
  • Setting SECRET_KEY as a number without quotes
  • Using boolean or null instead of a string
  • Using a short or guessable string
3. Given this settings.py snippet:
DEBUG = False
ALLOWED_HOSTS = ['example.com']
SECRET_KEY = 'mysecretkey'

What happens if a request comes from malicious.com?
medium
A. Django will crash with a server error
B. Django will accept the request and serve the page
C. Django will reject the request with a 400 Bad Request error
D. Django will redirect to example.com automatically

Solution

  1. Step 1: Understand ALLOWED_HOSTS role

    When DEBUG is false, Django only accepts requests from hosts listed in ALLOWED_HOSTS.
  2. Step 2: Check the request host

    The request is from malicious.com, which is not in ALLOWED_HOSTS, so Django rejects it with a 400 error.
  3. Final Answer:

    Django will reject the request with a 400 Bad Request error -> Option C
  4. Quick Check:

    Request host not allowed = 400 error = C [OK]
Hint: Requests from hosts not in ALLOWED_HOSTS get 400 error [OK]
Common Mistakes:
  • Thinking Django accepts all hosts when DEBUG is false
  • Assuming Django crashes on disallowed hosts
  • Believing Django redirects disallowed hosts
4. You set DEBUG = false but forgot to add your domain to ALLOWED_HOSTS. What error will you most likely see when accessing your site?
medium
A. No error, site loads normally
B. TemplateDoesNotExist error
C. Database connection error
D. DisallowedHost error

Solution

  1. Step 1: Understand ALLOWED_HOSTS enforcement

    When DEBUG is false, Django checks if the request host is in ALLOWED_HOSTS. If not, it raises a DisallowedHost error.
  2. Step 2: Identify the error from missing domain

    Since the domain is missing from ALLOWED_HOSTS, Django raises DisallowedHost.
  3. Final Answer:

    DisallowedHost error -> Option D
  4. Quick Check:

    Missing domain in ALLOWED_HOSTS = DisallowedHost error = A [OK]
Hint: Missing domain in ALLOWED_HOSTS causes DisallowedHost error [OK]
Common Mistakes:
  • Confusing DisallowedHost with template or database errors
  • Assuming site loads without ALLOWED_HOSTS set
  • Thinking DEBUG controls DisallowedHost error
5. You want to improve your Django app security. Which combination of settings is best practice for production?
hard
A. DEBUG = False, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = ['yourdomain.com']
B. DEBUG = True, SECRET_KEY = 'shortkey', ALLOWED_HOSTS = ['*']
C. DEBUG = False, SECRET_KEY = '', ALLOWED_HOSTS = []
D. DEBUG = True, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = []

Solution

  1. Step 1: Understand production security needs

    In production, DEBUG must be False to avoid exposing sensitive info. SECRET_KEY must be long and random. ALLOWED_HOSTS should list your domain to restrict access.
  2. Step 2: Evaluate each option

    DEBUG = True, SECRET_KEY = 'shortkey', ALLOWED_HOSTS = ['*'] has DEBUG = True and weak SECRET_KEY, unsafe. DEBUG = False, SECRET_KEY = '', ALLOWED_HOSTS = [] has empty SECRET_KEY and ALLOWED_HOSTS, insecure. DEBUG = True, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = [] has DEBUG = True and empty ALLOWED_HOSTS, unsafe. Only DEBUG = False, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = ['yourdomain.com'] meets all best practices.
  3. Final Answer:

    DEBUG = False, SECRET_KEY = 'a-long-random-string', ALLOWED_HOSTS = ['yourdomain.com'] -> Option A
  4. Quick Check:

    Production needs DEBUG false, strong SECRET_KEY, correct ALLOWED_HOSTS = A [OK]
Hint: Production: DEBUG false, strong SECRET_KEY, restrict ALLOWED_HOSTS [OK]
Common Mistakes:
  • Leaving DEBUG true in production
  • Using short or empty SECRET_KEY
  • Allowing all hosts with ALLOWED_HOSTS = ['*']
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard