session: Cookie should expire immediately when session is destroyed

I believe that the session cookie should be forced to immediately expire when the session is destroyed (i.e. when the user logs out) on the server-side.

While the Session Store I am using is correctly handling this such that a subsequent request from the same client would create a new cookie/session, having the existing cookie for the now-destroyed session be forcibly expired keeps things much cleaner and clearer on the client-side.

Doing so also avoids wasting some bytes on the network bandwidth of every outgoing request from the client by not including the irrelevant cookie. If the session has been destroyed but the cookie’s normal expiration date has not yet been reached, this can contribute an undesirable amount of unnecessary upload bytes incurred from the client’s outgoing requests due to always being required to include that irrelevant cookie in the Cookie header until it naturally expires.

When combined with my changes in PR #240 to fix the interaction between rolling: true and saveUnitialized: false, fixing this would keep things significantly cleaner on the client-side.

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Reactions: 13
  • Comments: 25 (16 by maintainers)

Commits related to this issue

Most upvoted comments

What is the state of this PR? I’d love to see a express-session version where this works, as we are experiencing quite some database requests with session IDs from expired sessions.

The hacks mentioned above do not apply for us, because sessions can expire at any point without the user hitting logout (e.g. user being locked, etc.), so an automatic cookie cleanup mechanism would be very helpful here.

Looking forward to this. Right now, I manually delete the client’s cookie on logout like this:

  const expireCookie = new expressSession.Cookie(req.session.cookie);
  expireCookie.expires = new Date(0);
  const cookies = cookie.parse(req.headers.cookie);
  res.header('set-cookie', expireCookie.serialize(appName, cookies[appName]));

appName is equal to the name option of express-session. Maybe there’s a more elegant way than going through the Cookie constructor.

I was able to create a working unit test that is setup the same as your most recent code example above, which has now been pushed into the existing PR #242.

Please review the PR as soon as you can, thanks! 💝

I’m helping out with a code-base where the Session ID (SID) has been used to tie resources in the DB. We’ve got to the point where we are now handling killing off the session, simply to generate a new SID (as we do not want any further records to be persisted with the same SID).

The caveat is that we need to maintain any logged in users (via req.session.user a la Passport.js) and doing so via the regenerate() callback, does not help as the Cookie’s SID in the browser is still the old SID.

  1. calling destroy and regenerate - causes the user to be logged out, SID changes though.
  2. calling regenerate - we are able to login the existing user via Passports logIn() facility, however the session SID stays the same.

Thoughts?