Skip to content

Commit

Permalink
Fix issue where Set-Cookie Expires was not always updated
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 14, 2016
1 parent 1940ce9 commit 50cdae2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Fix issue where `Set-Cookie` `Expires` was not always updated
* Methods are no longer enumerable on `req.session` object
* deps: [email protected]
- Add `sameSite` option
Expand Down
17 changes: 8 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,21 @@ function session(options){
return;
}

var cookie = req.session.cookie;
if (!shouldSetCookie(req)) {
return;
}

// only send secure cookies via https
if (cookie.secure && !issecure(req, trustProxy)) {
if (req.session.cookie.secure && !issecure(req, trustProxy)) {
debug('not secured');
return;
}

if (!shouldSetCookie(req)) {
return;
}
// touch session
req.session.touch();

setcookie(res, name, req.sessionID, secrets[0], cookie.data);
// set cookie
setcookie(res, name, req.sessionID, secrets[0], req.session.cookie.data);
});

// proxy end() to commit the session
Expand Down Expand Up @@ -285,9 +287,6 @@ function session(options){
return _end.call(res, chunk, encoding);
}

// touch session
req.session.touch();

if (shouldSave(req)) {
req.session.save(function onsave(err) {
if (err) {
Expand Down
28 changes: 28 additions & 0 deletions test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,34 @@ describe('session()', function(){
})
})

it('should update cookie expiration when slow write', function (done) {
var app = express();
app.use(session({ rolling: true, secret: 'keyboard cat', cookie: { maxAge: min }}));
app.use(function (req, res, next) {
req.session.user = 'bob';
res.write('hello, ');
setTimeout(function () {
res.end('world!');
}, 200);
});

request(app)
.get('/')
.expect(shouldSetCookie('connect.sid'))
.expect(200, function (err, res) {
if (err) return done(err);
var originalExpires = expires(res);
setTimeout(function () {
request(app)
.get('/')
.set('Cookie', cookie(res))
.expect(shouldSetCookie('connect.sid'))
.expect(function (res) { assert.notEqual(originalExpires, expires(res)); })
.expect(200, done);
}, (1000 - (Date.now() % 1000) + 200));
});
});

describe('when response ended', function () {
it('should have saved session', function (done) {
var saved = false
Expand Down

0 comments on commit 50cdae2

Please sign in to comment.