You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that changing the configuration in production/deployed mode of express-session cookies can lead to duplicate connect.sid cookie stored in the browser with the old and the new configuration - which can be very problematic to retrieve the right session afterwards.
I agree. It can create session ambiguity in cases when duplicate cookies are sent to the server.
However, its not a bug. When you change cookie attributes (e.g., secure, domain, or path) or rename the cookie, browsers treat the new configuration as a separate cookie while still retaining the old one until it expires. This is standard cookie behavior, not specific to express-session.
In order address your issue, you can name your cookies,
app.use((req,res,next)=>{constoldCookie=req.cookies['connect.sid'];constnewCookie=req.cookies['connect.sid.v2'];if(oldCookie&&!newCookie){// Attempt to migrate the old sessionreq.sessionStore.get(oldCookie,(err,session)=>{if(err||!session){// Old session doesn't exist or failed to loadreturnnext();}// Save the old session under the new cookie namereq.sessionStore.set(newCookie,session,(err)=>{if(err)console.error('Failed to migrate session:',err);// Clear the old cookie and proceedres.clearCookie('connect.sid');next();});});}else{next();}});
What I can agree on is, this is rather a feature request or idea. Perhaps we need something like,
Migration support for cookie configuration change
Cookie conflict resolution support
For the meantime, I am going to label this as a idea and let's see how the discussion continues. Personally, I am 👍 for adding a migration support or something like that.
I noticed that changing the configuration in production/deployed mode of express-session cookies can lead to duplicate connect.sid cookie stored in the browser with the old and the new configuration - which can be very problematic to retrieve the right session afterwards.
New configuration :
Old configuration :
To fix this, I had no choice but to change the name of the session cookie stored in the browser.
The text was updated successfully, but these errors were encountered: