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

Make sure we set session cookie if saveUninitialized is false and maxAge is on #147

Closed
2 tasks done
rclmenezes opened this issue Aug 24, 2022 · 0 comments
Closed
2 tasks done

Comments

@rclmenezes
Copy link
Contributor

rclmenezes commented Aug 24, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4

Plugin version

9

Node.js version

16

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Description

Currently, if maxAge is on and saveUninitialized is false, we don't re-save the session.

See this for original discussion:
#144 (comment)

Fix is putting the cookie's expiration in the hash:

if (this === sess && key === 'cookie') {
        // we want `touch` to affect the hash of the session
        return sess.cookie.expires?.getTime()
      }
}

Steps to Reproduce

test('should set session cookie if saveUninitialized is false and maxAge is on', async (t) => {
  t.plan(2)
  const options = {
    cookie: {
      maxAge: 42
    },
    secret: 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk',
    saveUninitialized: false
  }
  const plugin = fastifyPlugin(async (fastify, opts) => {
    fastify.addHook('onRequest', (request, reply, done) => {
      request.sessionStore.set(DEFAULT_SESSION_ID, {
        // In this scenario, maxAge would have set expires in a previous request
        cookie: {
          expires: new Date(Date.now() + 1000)
        }
      }, done)
    })
  })
  const fastify = await buildFastify((request, reply) => reply.send(200), options, plugin)
  t.teardown(() => fastify.close())

  const response = await fastify.inject({
    url: '/',
    headers: { cookie: DEFAULT_COOKIE, 'x-forwarded-proto': 'https' }
  })

  t.equal(response.statusCode, 200)
  t.ok(response.headers['set-cookie'])
})

Expected Behavior

The session should re-save

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

2 participants