Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
HTTPS and Secure Cookies in Django
📖 Scenario: You are building a Django web application that needs to be secure. You want to make sure your site uses HTTPS and that cookies are only sent over secure connections to protect user data.
🎯 Goal: Set up HTTPS enforcement and configure Django to use secure cookies for session and CSRF protection.
📋 What You'll Learn
Create a Django settings variable to enable HTTPS redirect
Add a variable to set cookies as secure
Configure session and CSRF cookies to be secure
Complete the settings to enforce HTTPS and secure cookies
💡 Why This Matters
🌍 Real World
Websites that handle user logins, personal data, or payments must use HTTPS and secure cookies to protect users from data theft and attacks.
💼 Career
Understanding how to configure HTTPS and secure cookies is essential for web developers and security engineers to build safe web applications.
Progress0 / 4 steps
1
Enable HTTPS redirect
In your Django settings.py file, create a variable called SECURE_SSL_REDIRECT and set it to True to force all HTTP requests to redirect to HTTPS.
Django
Hint
This setting tells Django to redirect all HTTP requests to HTTPS automatically.
2
Set cookies to be secure
Add a variable called SESSION_COOKIE_SECURE and set it to True in settings.py to ensure session cookies are only sent over HTTPS.
Django
Hint
This makes sure the session cookie is only sent on secure HTTPS connections.
3
Secure CSRF cookie
Add a variable called CSRF_COOKIE_SECURE and set it to True in settings.py to make the CSRF cookie secure.
Django
Hint
This setting ensures the CSRF cookie is only sent over HTTPS.
4
Complete HTTPS and secure cookies setup
Add a variable called SECURE_HSTS_SECONDS and set it to 3600 in settings.py to enable HTTP Strict Transport Security (HSTS) for one hour.
Django
Hint
This setting tells browsers to only use HTTPS for your site for the next hour.
Practice
(1/5)
1. What is the main purpose of setting SESSION_COOKIE_SECURE = True in Django settings?
easy
A. To allow cookies on both HTTP and HTTPS
B. To make cookies accessible to JavaScript
C. To disable cookies entirely
D. To ensure cookies are only sent over HTTPS connections
Solution
Step 1: Understand what SESSION_COOKIE_SECURE does
This setting tells Django to only send session cookies over HTTPS connections, preventing them from being sent over insecure HTTP.
Step 2: Analyze the options
To ensure cookies are only sent over HTTPS connections correctly describes this behavior. The other options do not match the purpose of this setting.
Final Answer:
To ensure cookies are only sent over HTTPS connections -> Option D
Quick Check:
SESSION_COOKIE_SECURE = True means HTTPS only [OK]
Hint: Secure cookies only send on HTTPS connections [OK]
Common Mistakes:
Thinking it makes cookies accessible to JavaScript
Believing it disables cookies
Assuming it allows cookies on HTTP
2. Which of the following is the correct way to enable HTTPS redirection in Django settings?
easy
A. SECURE_SSL_REDIRECT = False
B. SECURE_SSL_REDIRECT = True
C. SESSION_COOKIE_SECURE = False
D. CSRF_COOKIE_SECURE = False
Solution
Step 1: Identify the setting for HTTPS redirection
The setting SECURE_SSL_REDIRECT controls whether Django redirects HTTP requests to HTTPS.
Step 2: Choose the correct value to enable redirection
Setting SECURE_SSL_REDIRECT = True enables automatic redirection to HTTPS. The other options either disable security or relate to cookies.
Final Answer:
SECURE_SSL_REDIRECT = True -> Option B
Quick Check:
Enable HTTPS redirect with SECURE_SSL_REDIRECT = True [OK]
Hint: Set SECURE_SSL_REDIRECT to True to force HTTPS [OK]
Common Mistakes:
Setting SECURE_SSL_REDIRECT to False disables HTTPS redirect
Confusing cookie settings with HTTPS redirect
Not enabling HTTPS redirect at all
3. Given the following Django settings snippet, what will happen when a user accesses the site over HTTP?
Hint: HTTPS redirect happens before cookies are sent [OK]
Common Mistakes:
Thinking cookies are sent over HTTP despite redirect
Assuming CSRF protection is disabled
Believing HTTP access is allowed without redirect
4. You set SESSION_COOKIE_SECURE = True but notice session cookies are still sent over HTTP. What is the most likely cause?
medium
A. The site is not using HTTPS, so cookies are sent anyway
B. The browser does not support secure cookies
C. You forgot to set SECURE_SSL_REDIRECT = True
D. You need to set CSRF_COOKIE_SECURE = False
Solution
Step 1: Understand the role of SECURE_SSL_REDIRECT
This setting forces HTTP requests to HTTPS, ensuring secure cookies are sent only over HTTPS.
Step 2: Identify why cookies are sent over HTTP
If SECURE_SSL_REDIRECT is not enabled, users can access the site over HTTP, so cookies may be sent insecurely despite SESSION_COOKIE_SECURE.
Final Answer:
You forgot to set SECURE_SSL_REDIRECT = True -> Option C
Quick Check:
Enable SECURE_SSL_REDIRECT to enforce HTTPS [OK]
Hint: Enable SECURE_SSL_REDIRECT to prevent HTTP cookie sending [OK]
Common Mistakes:
Assuming browser ignores secure cookie flag
Thinking CSRF_COOKIE_SECURE affects session cookies
Believing HTTPS is automatic without redirect
5. You want to secure your Django site so that session and CSRF cookies are only sent over HTTPS, and all HTTP requests redirect to HTTPS. Which combination of settings achieves this securely?
hard
A. SECURE_SSL_REDIRECT = True, SESSION_COOKIE_SECURE = True, CSRF_COOKIE_SECURE = True
B. SECURE_SSL_REDIRECT = False, SESSION_COOKIE_SECURE = True, CSRF_COOKIE_SECURE = True
C. SECURE_SSL_REDIRECT = True, SESSION_COOKIE_SECURE = False, CSRF_COOKIE_SECURE = False
D. SECURE_SSL_REDIRECT = False, SESSION_COOKIE_SECURE = False, CSRF_COOKIE_SECURE = False
Solution
Step 1: Ensure HTTP requests redirect to HTTPS
Setting SECURE_SSL_REDIRECT = True forces all HTTP requests to HTTPS, preventing insecure access.
Step 2: Secure cookies for session and CSRF
Setting both SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE to True ensures cookies are only sent over HTTPS connections.
Step 3: Evaluate other options
The other options fail to secure either redirection or cookies properly, leaving security gaps.