Skip to content

fix: avoid duplicate save after regenerate - #1145

Open
deepakganesh78 wants to merge 1 commit into
expressjs:masterfrom
deepakganesh78:fix/issue935-regenerate-save
Open

fix: avoid duplicate save after regenerate#1145
deepakganesh78 wants to merge 1 commit into
expressjs:masterfrom
deepakganesh78:fix/issue935-regenerate-save

Conversation

@deepakganesh78

@deepakganesh78 deepakganesh78 commented Aug 2, 2026

Copy link
Copy Markdown

Fixes #935

Reproduction

On current master, a request that calls req.session.regenerate(), mutates the new session, and then calls req.session.save() writes the same regenerated session twice. A minimal supertest reproduction counted MemoryStore#set calls and showed total saves after response: 2.

Root cause

regenerate() replaces req.session with a new Session, but the middleware only wrapped the original session's methods. As a result, a manual save() 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() like reload() so the replacement session gets the middleware method wrappers. Track the explicitly saved session id in a separate savedId closure variable alongside savedHash, and use that pair only for isSaved().

Cookie-setting and modification semantics are unchanged: originalId is not mutated by save(), so session id rotation still makes isModified() true and still emits Set-Cookie for 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

  • Original regression test fails without the fix: 0 passing, 1 failing, with 2 !== 1 store writes.
  • Added no-data-change regression test fails under the first version of this fix: 0 passing, 1 failing, missing the expected Set-Cookie header.
  • Targeted regenerate/save tests pass with this rework: 2 passing.
  • Full suite via Git Bash on Windows: 175 passing (3s).
  • npm run lint: passed.
  • Direct npm test on 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 with MSYS_NO_PATHCONV=1 successfully.

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
deepakganesh78 force-pushed the fix/issue935-regenerate-save branch from 9700baa to b1e56d4 Compare August 2, 2026 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regenerated session is re-saved even if not modified since save

1 participant