Repository navigation
fix: save session when replaced with empty object - #226
Open
RaphaelFakhri wants to merge 1 commit into
Open
RaphaelFakhri wants to merge 1 commit into
RaphaelFakhri wants to merge 1 commit into
Conversation
yxshee
reviewed
Nov 5, 2026
yxshee
left a comment
There was a problem hiding this comment.
Ran this locally: 43 passing, and the new "when replaced with {}" test fails on master (42 passing, 1 failing). The repo's pinned mocha doesn't start on Node 26 (its yargs is ESM-only), so I used npx mocha@11 with the same flags.
I also checked the end-to-end flow from #225 against an Express app:
/login→/reset(req.session = {}) now sendssession=e30=plus a fresh.sig, and the next request sees{}, where master still returned{"user":"alice"}.req.session = {}with no incoming cookie still sends noSet-Cookie, so empty new sessions aren't written. 👍- With a tampered/unsigned incoming cookie,
tryGetSessionreturns nothing, so no replacement cookie is written. That seems right: there was no valid session to replace.
One small thing: tryGetSession decodes and verifies the request cookie again on every assignment, even when req.session was already read earlier in the request. It's cheap enough that it probably doesn't matter, but the parsed session from the getter could be reused if you want to avoid the second signature check. LGTM either way.
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.
Assigning
req.session = {}on a request that carries a valid session cookie sends noSet-Cookieheader, so the old session data stays in the browser and comes back on the next request.req.session = {}creates a new session, and a new session is saved only when it is populated. This change records, at assignment time, whether the request carried a valid session. If it did, the replacement session is saved even when empty, which matches the behavior of deleting every key from an existing session.Behavior without a request session is unchanged:
req.session = {}on a request with no session cookie still sends noSet-Cookieheader (covered by the existingwhen session = {}test).Tests
saved session when replaced with {} should Set-Cookie with empty session.master(noSet-Cookieheader), passes with this change.npm testandnpm run lintpass.Fixes #225