settings: Refresh the session cookie on every request #3
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/session-sliding-expiry"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Hearth configured nothing about sessions, so Django defaults applied and the session cookie age was fixed at login time. An active user was still logged out 14 days after logging in, because nothing ever re-saved the session.
SESSION_SAVE_EVERY_REQUEST = TruemakesSESSION_COOKIE_AGEa sliding window that refreshes on use.Root cause
hearth/settings.pyset noSESSION_ENGINE,SESSION_COOKIE_AGE, orSESSION_SAVE_EVERY_REQUEST, so Django's defaults applied (backends.db,1209600,False).django.contrib.auth.login()never callsset_expiry(), soSessionBase.get_expiry_age()falls back to the constantget_session_cookie_age()= 1209600 s. The expiry is anchored to the moment the cookie was issued and never moves with activity.SESSION_SAVE_EVERY_REQUEST = False,SessionMiddleware.process_response()re-issues the cookie only whenrequest.session.modifiedis true. Ordinary browsing does not modify the session, so the window never slid.Nothing else was clearing sessions:
docker/entrypoint.shonly runsmigrateand starts Daphne,seed_hearthdeletes only recipe ingredients, andhearth_datapersists across the container stops performed bydocker-volume-backup.Change
hearth/settings.py— one setting plus a comment:Verification
python manage.py check:python manage.py test core:ruff check hearth/settings.pyandruff format --check hearth/settings.py: all checks passed, already formatted.git diff --check: clean.Behavioural check on a throwaway test database (log in, then issue one plain authenticated GET that writes nothing, and compare both setting values):
So with the flag on, each request re-issues the session cookie and pushes the DB expiry out by another
SESSION_COOKIE_AGE; with the flag off (current production behaviour) both stay frozen at login.Risk and rollback
Every request now writes one
django_sessionrow (SQLite, WAL,busy_timeout=5000). At this scale the extra write is negligible;cached_dbis available later if it ever matters. Rollback is deleting the one line.Not covered
Sessions are still only 14 days from the last request, and
hearth.marut.networkand the LAN IP192.168.50.200remain separate cookie jars, so switching hosts still requires a fresh login. RaisingSESSION_COOKIE_AGEor addingSESSION_COOKIE_SECURE/__Host-hardening would be a follow-up.