session icon indicating copy to clipboard operation
session copied to clipboard

Expire cookie immediately upon destroying session

Open JamesMGreene opened this issue 10 years ago • 24 comments

Force an existing session's cookie to expire immediately upon destroying the session.

Fixes #241.

JamesMGreene avatar Dec 10 '15 18:12 JamesMGreene

@dougwilson: I'm sure you're day-job-and-open-source-swamped like the rest of us but if there is any chance of getting PR #240 and this PR merged and released within the next 2 weeks or so, my team and I would appreciate it! :+1: :gift_heart:

JamesMGreene avatar Dec 11 '15 21:12 JamesMGreene

Hi James, sorry I haven't been on a computer since you made this. I will look at this pull request as soon as I can. I cannot comment on when this pull request would be accepted, because I need to understand first what the pull requests does and understand if this is a desired feature, if there are edges cases, etc.

I know you feel like it is a bug, but I need to look at it more, as to me, if feels like either a bug or a new feature.

dougwilson avatar Dec 11 '15 22:12 dougwilson

Sounds good, @dougwilson. Thanks! :+1:

If you do end up feeling it is a new feature and would rather implement it as an option rather than as the new default behavior, I'd be fine with refactoring it to work as such if you give me a bit of design suggestion/guidance on how you'd like it to work.

I'll keep an eye out for any feedback requiring changes on my part. :eyes:

JamesMGreene avatar Dec 12 '15 03:12 JamesMGreene

@dougwilson: As mentioned in the issue (#241):

Perhaps this should only be the expected behavior if the cookie would've been updated normally anyway, e.g. if the rolling: true option was enabled.

JamesMGreene avatar Dec 12 '15 03:12 JamesMGreene

@dougwilson: If there is any chance of this being merged and released before Monday, I'd :yellow_heart: you for it.

JamesMGreene avatar Jan 08 '16 04:01 JamesMGreene

Hi @JamesMGreene , I'm sorry, I really wanted to (even did a few commits to this repo tonight, yay!) get this done tonight, before Monday for you. I just didn't get to, but what I did do is at least published the changes to npm that we had so far, and then I really want to get this change done on Monday for you.

dougwilson avatar Jan 11 '16 04:01 dougwilson

@dougwilson: If it makes it in this week, I can probably still pull it into our release branch. Monday would be best if possible so I can just get it into the mainline branch, though. :+1:

JamesMGreene avatar Jan 11 '16 05:01 JamesMGreene

Yea, I'm working on it. Just looked at it and there are all kinds of nits, like the function in index.js is not alphabetically sorted like the rest, the tests are in the wrong location/their descriptions are too long. I'm working on adjusting.

dougwilson avatar Jan 11 '16 15:01 dougwilson

And a reassignment of an argument, which results in a v8 de-opt.

dougwilson avatar Jan 11 '16 15:01 dougwilson

Hi! It seems like if a user calls req.session.destroy() in their code, then it won't actually set the expired cookie. This seems very unexpected and one would expect that the method of destruction would not change if the cookie is expired or not. Example test app:

var app = express()
.use(session({ secret: 'keyboard cat', cookie: { maxAge: 86400000 } }))
.use(function(req, res, next){
  req.session.count = req.session.count || 0;
  req.session.count++;
  if (req.session.count === 2) {
    req.session.destroy(function(err) {
      if (err) return next(err);
      res.end();
    });
  } else {
    res.end();
  }
});

dougwilson avatar Jan 11 '16 15:01 dougwilson

Hi @JamesMGreene , I'm not in a rush, but I know you seemed to be, so I just wanted to put a note here just in case you missed my comment in https://github.com/expressjs/session/pull/242#issuecomment-170593341

dougwilson avatar Jan 16 '16 01:01 dougwilson

Thanks, @dougwilson. You caught me in the thralls of our busy release sprint, so I didn't have any time to address this.

I should be able to take a look this week. I'm OK with this change not going out with our next release; my PR #240 being merged into v1.13.0 was the more important change. :+1:

JamesMGreene avatar Jan 25 '16 15:01 JamesMGreene

the tests are in the wrong location/their descriptions are too long

@dougwilson: Can you fill me in more on this comment?

JamesMGreene avatar Jun 14 '16 15:06 JamesMGreene

It seems like if a user calls req.session.destroy() in their code, then it won't actually set the expired cookie. This seems very unexpected and one would expect that the method of destruction would not change if the cookie is expired or not.

Working through this issue now.

JamesMGreene avatar Jun 14 '16 16:06 JamesMGreene

Alright, so the problem with when the user calls req.session.destroy() — without also setting the unset: "destroy" option — is that shouldDestroy(req) returns false, ergo the isBeingDestroyed variable is also set to false:

// Is this an existing session that is being destroyed?
var isBeingDestroyed = !!cookieId && shouldDestroy(req);

Reminder of current shouldDestroy implementation:

// determine if session should be destroyed
function shouldDestroy(req) {
  return req.sessionID && unsetDestroy && req.session == null;
}

@dougwilson et al: Any advice on how to workaround this situation?


P.S. Note that if you do set the unset: "destroy" option, then it works as expected.

JamesMGreene avatar Jun 14 '16 17:06 JamesMGreene

Hi @JamesMGreene, sorry I didn't reply right away; I was just trying to focus on pushing out Express 4.14 :) So I think that that is probably just not a full understanding of the current code, that that's OK! At least how it is designed right now, the shouldDestroy and similar is only there to track if the "auto saving" part of this module should basically destroy it instead of save it. This functionality is completely independent of tracking if the session has been manually destroyed or not. You probably have to add manual tracking for this, similar to how we manually track if a session has manually been saved or not.

dougwilson avatar Jun 16 '16 17:06 dougwilson

OK, gotcha @dougwilson. I'll look into that further.

JamesMGreene avatar Jun 17 '16 04:06 JamesMGreene

@dougwilson: Finally found a bit of time to dig into this further and get it all working. Please re-review the new version when you get a chance. Thanks!

JamesMGreene avatar Sep 30 '16 11:09 JamesMGreene

Hi @JamesMGreene , it looks like the last commit was Dec 10, 2015. Is that the most recent version?

dougwilson avatar Oct 01 '16 00:10 dougwilson

@dougwilson: No, sorry, that was because I did a git commit --amend (so it maintained the old commit's date). I've pushed a new replacement commit now dated as today for better accuracy.

JamesMGreene avatar Oct 01 '16 04:10 JamesMGreene