fix: avoid duplicate save after regenerate - #1145
Open
deepakganesh78 wants to merge 1 commit into
Open
Conversation
Wrap regenerated sessions so manual saves update the request save tracking. Track the explicitly saved session id separately from the original id so modification and Set-Cookie semantics remain unchanged after session id rotation. Fixes expressjs#935 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
deepakganesh78
force-pushed
the
fix/issue935-regenerate-save
branch
from
August 2, 2026 17:01
9700baa to
b1e56d4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #935
Reproduction
On current
master, a request that callsreq.session.regenerate(), mutates the new session, and then callsreq.session.save()writes the same regenerated session twice. A minimal supertest reproduction countedMemoryStore#setcalls and showedtotal saves after response: 2.Root cause
regenerate()replacesreq.sessionwith a newSession, but the middleware only wrapped the original session's methods. As a result, a manualsave()on the regenerated session did not update the request's saved-session tracking hash, so the response finalizer treated it as unsaved and wrote it again.Fix
Wrap
regenerate()likereload()so the replacement session gets the middleware method wrappers. Track the explicitly saved session id in a separatesavedIdclosure variable alongsidesavedHash, and use that pair only forisSaved().Cookie-setting and modification semantics are unchanged:
originalIdis not mutated bysave(), so session id rotation still makesisModified()true and still emitsSet-Cookiefor the new id even when the regenerated session has no data changes.Compatibility / security notes
This is a conservative behavior fix: explicit session regeneration and saving still persists and sends the new session cookie, but avoids an extra store write that could overwrite concurrent updates. No dependencies or public API are changed.
Validation
0 passing, 1 failing, with2 !== 1store writes.0 passing, 1 failing, missing the expectedSet-Cookieheader.2 passing.175 passing (3s).npm run lint: passed.npm teston Windows fails before running tests because npm/cmd cannot execute./test/support/gencert.sh('.' is not recognized...); reran the same script/test command through Git Bash withMSYS_NO_PATHCONV=1successfully.