Expire cookie immediately upon destroying session
Force an existing session's cookie to expire immediately upon destroying the session.
Fixes #241.
@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:
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.
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:
@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: trueoption was enabled.
@dougwilson: If there is any chance of this being merged and released before Monday, I'd :yellow_heart: you for it.
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: 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:
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.
And a reassignment of an argument, which results in a v8 de-opt.
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();
}
});
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
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:
the tests are in the wrong location/their descriptions are too long
@dougwilson: Can you fill me in more on this comment?
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.
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.
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.
OK, gotcha @dougwilson. I'll look into that further.
@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!
Hi @JamesMGreene , it looks like the last commit was Dec 10, 2015. Is that the most recent version?
@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.