Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pasieronen opened this issue Mar 14, 2023 · 1 comment
Open

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

pasieronen opened this issue Mar 14, 2023 · 1 comment

Comments

@pasieronen
Copy link

PR #849 attempted to fix resaving an already-saved new session at the end of the request, but there's one corner case it missed. If session.regenerate() has been called before session.save(), then the request is still saved again at the end of the request.

This can actually lead to a race condition: if another request modifies the session after session.save() but before the end of the first request, then those modifications get overwritten when the first request ends. (And yes, this can happen in real world...)

How to reproduce: This code prints "saving" twice for one request (also verified by adding logging inside express-session)

const MemoryStore = require('express-session/session/memory')

const _set = MemoryStore.prototype.set
MemoryStore.prototype.set = function set() {
  console.log('saving')
  _set.apply(this, arguments)
}

const express = require('express')
const session = require('express-session')
const app = express()

app.use(session({
  secret: 'cat',
  resave: false,
  saveUninitialized: false
}))

app.get('/', (req, res) => {
  req.session.regenerate(() => {
    req.session.value = 'foo'
    req.session.save(() => {
      res.send('hello')
    })
  })
})

app.listen(3000)
@pasieronen
Copy link
Author

Outline of a possible fix:

  • In index.js wrapMethods, also wrap regenerate (same way as reload)
  • In the wrapped save method, set originalId = this.id (so that isSaved returns true and isModified false, unless more modifications happen after saving)

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

No branches or pull requests

1 participant