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 expressjs#849

(cherry picked from commit 99d6cde)
  • Loading branch information
jneander authored and SamTV12345 committed Jul 3, 2023
1 parent d754b74 commit 827bd4f
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: cookie@0.4.2

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 @@ -478,7 +478,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 @@ -1961,6 +1961,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 827bd4f

Please sign in to comment.