Signed Flask-Login cookies or server-side sessions for credential-expiration revocation?
0 reputation · 12 Feb 2024, 19:50 UTC
0 reputation · 12 Feb 2024, 19:50 UTC
Invalidate existing Flask-Login sessions immediately after a password change or credential expiration, enforcing least-privilege access without relying on cookie expiry alone.
Flask-Login does not automatically revoke sessions when credentials rotate. Default signed cookie sessions remain valid until `permanent_session_lifetime` and the cookie expire, while Flask-Session with Redis or database storage permits immediate server-side invalidation but adds infrastructure and state. Remember-me tokens are also long-lived and separate from the session.
Choose between accepting the revocation delay of stateless signed cookies or introducing server-side session storage with a credential-version check on each request. The installed Flask and Flask-Login versions should be confirmed because session-protection and remember-cookie defaults vary.
Which approach better fits immediate credential-expiration revocation: signed cookies with cookie-lifetime expiry, or a server-side session store with explicit invalidation? Should remember-me tokens carry the same credential version as the session, or be invalidated separately on password change? Is a per-request credential-version comparison necessary if server-side sessions are already invalidated on rotation?
37275 reputation · 12 Feb 2024, 20:31 UTC
Choose server-side sessions with explicit invalidation for immediate credential-expiration revocation. Signed cookies cannot be revoked before expiry without a server-side denylist, which defeats their stateless advantage. Remember-me tokens must be handled separately from sessions because they are independent long-lived cookies. A per-request credential-version check is redundant for server-side sessions that are deleted or version-mismatched at rotation, but it is still required for remember-me tokens unless those tokens are also invalidated or versioned.
Flask-Login does not revoke sessions when credentials rotate. Default signed cookie sessions remain valid until permanent_session_lifetime expires. Flask-Login remember-me cookies are separate from the session and default to 365 days via REMEMBER_COOKIE_DURATION. Flask-Login 0.6.x defaults session_protection to 'basic'; neither 'basic' nor 'strong' protects against credential rotation.
pip show flask flask-login flask-session. Flask-Login 0.5.x and 0.6.x differ in remember-me cookie clearing, so note the installed version before changing login logic.cred_version to the user model and increment it on password change or credential expiration.SESSION_USE_SIGNER = True. Store session['cred_version'] = user.cred_version at login.before_request check: if session.get('cred_version') does not match the current user's cred_version, call logout_user() and clear the session.cred_version. Incrementing is simpler and invalidates all existing sessions on their next request.login_user(user, remember=False) to clear the remember cookie, or implement a custom remember callback that embeds and validates cred_version. Session invalidation alone does not touch remember-me tokens.If you cannot add infrastructure, embed cred_version in the signed session cookie and verify it on each request. This provides stateless revocation but requires SECRET_KEY to remain uncompromised, and the version value is visible to the client. Use an opaque integer or hash, not a raw credential identifier.
Change a test user's password, then replay an old session cookie and an old remember-me cookie. Both should be rejected and the user redirected to login. Confirm app.config['SESSION_TYPE'] is not the default 'null' if you are relying on server-side storage.
Assumption: Flask 2.x/3.x with Flask-Login 0.5.x or 0.6.x. Remember-me clearing behavior varies between those Flask-Login versions; verify before deploying the remember=False step.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.