Skip to content

Commit

Permalink
Fix resaving already-saved new session at end of request
Browse files Browse the repository at this point in the history
closes #849
  • Loading branch information
jneander authored and dougwilson committed May 11, 2022
1 parent a06b0be commit 99d6cde
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Fix resaving already-saved new session at end of request
* deps: [email protected]

1.17.2 / 2021-05-19
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ function session(options) {
return false;
}

return !saveUninitializedSession && cookieId !== req.sessionID
return !saveUninitializedSession && !savedHash && cookieId !== req.sessionID
? isModified(req.session)
: !isSaved(req.session)
}
Expand Down
25 changes: 25 additions & 0 deletions test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,31 @@ describe('session()', function(){
.expect(200, 'saved', done)
})
})

describe('when saveUninitialized is false', function () {
it('should prevent end-of-request save', function (done) {
var store = new session.MemoryStore()
var server = createServer({ saveUninitialized: false, store: store }, function (req, res) {
req.session.hit = true
req.session.save(function (err) {
if (err) return res.end(err.message)
res.end('saved')
})
})

request(server)
.get('/')
.expect(shouldSetSessionInStore(store))
.expect(200, 'saved', function (err, res) {
if (err) return done(err)
request(server)
.get('/')
.set('Cookie', cookie(res))
.expect(shouldSetSessionInStore(store))
.expect(200, 'saved', done)
})
})
})
})

describe('.touch()', function () {
Expand Down

0 comments on commit 99d6cde

Please sign in to comment.