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
Session Security Considerations in Django
📖 Scenario: You are building a Django web application that needs to keep user sessions safe. Sessions help remember who the user is while they browse your site. But if sessions are not secure, bad people can steal them and pretend to be the user.We will learn how to set up session security settings in Django step-by-step.
🎯 Goal: Set up a Django project with secure session settings to protect user sessions from common risks like session hijacking and fixation.
📋 What You'll Learn
Create a Django settings variable for session cookie age
Add a setting to make session cookies only sent over HTTPS
Configure the session engine to use cached database sessions
Set the session cookie to be HttpOnly to prevent JavaScript access
💡 Why This Matters
🌍 Real World
Web applications need to keep user sessions safe to protect user data and prevent attackers from hijacking accounts.
💼 Career
Understanding and configuring session security is essential for backend developers working with Django to build secure web applications.
Progress0 / 4 steps
1
Set session cookie age
In your Django settings.py file, create a variable called SESSION_COOKIE_AGE and set it to 1200 (which means 20 minutes in seconds). This controls how long a session lasts before it expires.
Django
Hint
Use seconds for the session duration. 20 minutes equals 1200 seconds.
2
Make session cookie secure
Add a setting called SESSION_COOKIE_SECURE in settings.py and set it to True. This makes sure the session cookie is only sent over HTTPS connections, keeping it safe from being sent over insecure networks.
Django
Hint
This setting helps protect the cookie during transmission by using HTTPS only.
3
Use cached database session engine
Set the SESSION_ENGINE variable in settings.py to 'django.contrib.sessions.backends.cached_db'. This uses the database with caching for faster and safer session storage.
Django
Hint
This session engine stores sessions in the database and caches them for performance.
4
Set session cookie HTTPOnly
Add SESSION_COOKIE_HTTPONLY in settings.py and set it to True. This prevents JavaScript from accessing the session cookie, reducing the risk of cross-site scripting attacks stealing the session.
Django
Hint
This setting helps keep the session cookie safe from client-side scripts.
Practice
(1/5)
1. Which Django setting helps ensure session cookies are only sent over HTTPS connections?
easy
A. SESSION_EXPIRE_AT_BROWSER_CLOSE
B. SESSION_COOKIE_HTTPONLY
C. SESSION_COOKIE_SECURE
D. SESSION_SAVE_EVERY_REQUEST
Solution
Step 1: Understand the purpose of SESSION_COOKIE_SECURE
This setting makes sure cookies are only sent over HTTPS, protecting them from being sent over insecure connections.
Step 2: Compare with other settings
SESSION_COOKIE_HTTPONLY prevents JavaScript access, SESSION_EXPIRE_AT_BROWSER_CLOSE controls expiration, and SESSION_SAVE_EVERY_REQUEST saves session on every request, none enforce HTTPS.
Final Answer:
SESSION_COOKIE_SECURE -> Option C
Quick Check:
Secure cookie = SESSION_COOKIE_SECURE [OK]
Hint: Secure cookies only with SESSION_COOKIE_SECURE [OK]
Common Mistakes:
Confusing HTTPOnly with secure flag
Thinking expiration controls HTTPS
Assuming saving every request affects security
2. Which of the following is the correct way to set a session cookie to be inaccessible to JavaScript in Django's settings?
easy
A. SESSION_COOKIE_HTTPONLY = True
B. SESSION_COOKIE_HTTPONLY = False
C. SESSION_COOKIE_SECURE = False
D. SESSION_EXPIRE_AT_BROWSER_CLOSE = False
Solution
Step 1: Identify the setting controlling JavaScript access
SESSION_COOKIE_HTTPONLY when set to True prevents JavaScript from accessing the cookie.
Step 2: Confirm correct boolean value
Setting it to True enables this protection; False would allow JavaScript access.
The session cookie is deleted, requiring login again. -> Option B
Quick Check:
Expire at close = cookie deleted [OK]
Hint: Expire at browser close deletes session cookie [OK]
Common Mistakes:
Thinking cookie persists after browser close
Confusing secure flag with expiration
Assuming HTTPOnly affects cookie lifetime
4. You want to ensure that session cookies are not accessible via JavaScript and are only sent over HTTPS. Which of the following Django settings combinations sets both security flags to False?
medium
A. SESSION_COOKIE_HTTPONLY = False and SESSION_COOKIE_SECURE = False
B. SESSION_COOKIE_HTTPONLY = False and SESSION_COOKIE_SECURE = True
C. SESSION_COOKIE_HTTPONLY = True and SESSION_COOKIE_SECURE = False
D. SESSION_COOKIE_HTTPONLY = True and SESSION_COOKIE_SECURE = True
Solution
Step 1: Identify required settings for security
To block JavaScript access, SESSION_COOKIE_HTTPONLY must be True. To send cookies only over HTTPS, SESSION_COOKIE_SECURE must be True.
Step 2: Analyze each option
SESSION_COOKIE_HTTPONLY = False and SESSION_COOKIE_SECURE = False sets both to False, allowing JavaScript access and sending cookies over HTTP, which is insecure.
Final Answer:
SESSION_COOKIE_HTTPONLY = False and SESSION_COOKIE_SECURE = False -> Option A
Quick Check:
Both flags False = insecure [OK]
Hint: Both HTTPOnly and Secure must be True for safety [OK]
Common Mistakes:
Thinking one flag alone is enough
Confusing True/False meanings
Ignoring HTTPS requirement for Secure flag
5. You want to improve session security by expiring sessions after 15 minutes of inactivity and ensuring cookies are secure and inaccessible to JavaScript. Which Django settings combination achieves this correctly?
hard
A. SESSION_COOKIE_SECURE = True, SESSION_COOKIE_HTTPONLY = True, SESSION_EXPIRE_AT_BROWSER_CLOSE = False
B. SESSION_COOKIE_SECURE = False, SESSION_COOKIE_HTTPONLY = True, SESSION_COOKIE_AGE = 900
C. SESSION_COOKIE_SECURE = True, SESSION_COOKIE_HTTPONLY = False, SESSION_COOKIE_AGE = 3600
D. SESSION_COOKIE_SECURE = True, SESSION_COOKIE_HTTPONLY = True, SESSION_COOKIE_AGE = 900
Solution
Step 1: Set secure and HTTPOnly flags
Both SESSION_COOKIE_SECURE and SESSION_COOKIE_HTTPONLY must be True to protect cookies from insecure transport and JavaScript access.
Step 2: Set session expiration time
SESSION_COOKIE_AGE controls session lifetime in seconds; 900 seconds equals 15 minutes, which matches the requirement.
Step 3: Verify other options
The combination with SESSION_EXPIRE_AT_BROWSER_CLOSE = False (without SESSION_COOKIE_AGE) does not provide a 15-minute inactivity timeout. The one with SESSION_COOKIE_SECURE = False allows transmission over HTTP. The one with SESSION_COOKIE_HTTPONLY = False and SESSION_COOKIE_AGE = 3600 permits JavaScript access and uses a 1-hour timeout.