Sessions keep track of users when they use a website. Securing sessions helps protect user data and stops bad people from pretending to be someone else.
Session security considerations in Django
SESSION_COOKIE_SECURE = True SESSION_COOKIE_HTTPONLY = True SESSION_COOKIE_AGE = 1209600 # 2 weeks in seconds SESSION_EXPIRE_AT_BROWSER_CLOSE = False CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE makes sure cookies are sent only over HTTPS.
SESSION_COOKIE_HTTPONLY stops JavaScript from accessing session cookies, reducing some attacks.
SESSION_COOKIE_SECURE = TrueSESSION_COOKIE_HTTPONLY = TrueSESSION_EXPIRE_AT_BROWSER_CLOSE = TrueSESSION_COOKIE_AGE = 3600This example shows how to set important session security settings in Django and prints their values to confirm.
from django.conf import settings # Example settings for session security settings.SESSION_COOKIE_SECURE = True settings.SESSION_COOKIE_HTTPONLY = True settings.SESSION_COOKIE_AGE = 1800 # 30 minutes settings.SESSION_EXPIRE_AT_BROWSER_CLOSE = True print(f"SESSION_COOKIE_SECURE: {settings.SESSION_COOKIE_SECURE}") print(f"SESSION_COOKIE_HTTPONLY: {settings.SESSION_COOKIE_HTTPONLY}") print(f"SESSION_COOKIE_AGE (seconds): {settings.SESSION_COOKIE_AGE}") print(f"SESSION_EXPIRE_AT_BROWSER_CLOSE: {settings.SESSION_EXPIRE_AT_BROWSER_CLOSE}")
Always use HTTPS on your site to keep session cookies safe during transmission.
Set SESSION_COOKIE_HTTPONLY to prevent JavaScript access to cookies.
Consider expiring sessions after a reasonable time or when the browser closes to reduce risk.
Sessions help remember users but need protection to keep data safe.
Use settings like SESSION_COOKIE_SECURE and SESSION_COOKIE_HTTPONLY to improve security.
Expire sessions after some time or on browser close to reduce chances of misuse.
Practice
Solution
Step 1: Understand the purpose of
This setting makes sure cookies are only sent over HTTPS, protecting them from being sent over insecure connections.SESSION_COOKIE_SECUREStep 2: Compare with other settings
SESSION_COOKIE_HTTPONLYprevents JavaScript access,SESSION_EXPIRE_AT_BROWSER_CLOSEcontrols expiration, andSESSION_SAVE_EVERY_REQUESTsaves session on every request, none enforce HTTPS.Final Answer:
SESSION_COOKIE_SECURE -> Option CQuick Check:
Secure cookie = SESSION_COOKIE_SECURE [OK]
- Confusing HTTPOnly with secure flag
- Thinking expiration controls HTTPS
- Assuming saving every request affects security
Solution
Step 1: Identify the setting controlling JavaScript access
SESSION_COOKIE_HTTPONLYwhen 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.Final Answer:
SESSION_COOKIE_HTTPONLY = True -> Option AQuick Check:
HTTPOnly true blocks JavaScript [OK]
- Setting HTTPOnly to False expecting protection
- Confusing SESSION_COOKIE_SECURE with HTTPOnly
- Mixing expiration settings with cookie flags
SESSION_COOKIE_SECURE = True SESSION_COOKIE_HTTPONLY = True SESSION_EXPIRE_AT_BROWSER_CLOSE = True
What happens to the session cookie when the user closes their browser?
Solution
Step 1: Understand
This setting makes the session cookie expire when the browser closes, deleting it.SESSION_EXPIRE_AT_BROWSER_CLOSEStep 2: Check other settings' effects
SESSION_COOKIE_SECUREensures HTTPS only,SESSION_COOKIE_HTTPONLYblocks JavaScript access, neither affects expiration on close.Final Answer:
The session cookie is deleted, requiring login again. -> Option BQuick Check:
Expire at close = cookie deleted [OK]
- Thinking cookie persists after browser close
- Confusing secure flag with expiration
- Assuming HTTPOnly affects cookie lifetime
Solution
Step 1: Identify required settings for security
To block JavaScript access,SESSION_COOKIE_HTTPONLYmust be True. To send cookies only over HTTPS,SESSION_COOKIE_SECUREmust 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 AQuick Check:
Both flags False = insecure [OK]
- Thinking one flag alone is enough
- Confusing True/False meanings
- Ignoring HTTPS requirement for Secure flag
Solution
Step 1: Set secure and HTTPOnly flags
BothSESSION_COOKIE_SECUREandSESSION_COOKIE_HTTPONLYmust be True to protect cookies from insecure transport and JavaScript access.Step 2: Set session expiration time
SESSION_COOKIE_AGEcontrols session lifetime in seconds; 900 seconds equals 15 minutes, which matches the requirement.Step 3: Verify other options
The combination withSESSION_EXPIRE_AT_BROWSER_CLOSE = False(withoutSESSION_COOKIE_AGE) does not provide a 15-minute inactivity timeout. The one withSESSION_COOKIE_SECURE = Falseallows transmission over HTTP. The one withSESSION_COOKIE_HTTPONLY = FalseandSESSION_COOKIE_AGE = 3600permits JavaScript access and uses a 1-hour timeout.Final Answer:
SESSION_COOKIE_SECURE = True, SESSION_COOKIE_HTTPONLY = True, SESSION_COOKIE_AGE = 900 -> Option DQuick Check:
Secure + HTTPOnly + 15 min age = SESSION_COOKIE_SECURE = True, SESSION_COOKIE_HTTPONLY = True, SESSION_COOKIE_AGE = 900 [OK]
- Forgetting to set secure flag to True
- Using wrong expiration time units
- Disabling HTTPOnly accidentally
Ready to prove it?
Pass the test and this pattern moves unknown
