Skip to content

Commit

Permalink
simplify cookie handling (fastify#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Sep 9, 2022
1 parent 6324dc8 commit 5c85d24
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 58 deletions.
48 changes: 0 additions & 48 deletions lib/cookie.js

This file was deleted.

11 changes: 3 additions & 8 deletions lib/fastifySession.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fp = require('fastify-plugin')
const idGenerator = require('./idGenerator')()
const Store = require('./store')
const Session = require('./session')
const isConnectionSecure = require('./isConnectionSecure')

function fastifySession (fastify, options, next) {
const error = checkOptions(options)
Expand Down Expand Up @@ -165,7 +166,7 @@ function fastifySession (fastify, options, next) {

const cookieSessionId = getCookieSessionId(request)
const saveSession = shouldSaveSession(request, cookieSessionId, saveUninitializedSession, rollingSessions)
const isInsecureConnection = cookieOpts.secure === true && !isConnectionSecure(request)
const isInsecureConnection = cookieOpts.secure === true && isConnectionSecure(request) === false
if (!saveSession || isInsecureConnection) {
// if a session cookie is set, but has a different ID, clear it
if (cookieSessionId && cookieSessionId !== session.encryptedSessionId) {
Expand All @@ -183,7 +184,7 @@ function fastifySession (fastify, options, next) {
reply.setCookie(
cookieName,
hasCookiePrefix ? `${cookiePrefix}${session.encryptedSessionId}` : session.encryptedSessionId,
session.cookie.options(isConnectionSecure(request))
session.cookie
)
done()
})
Expand Down Expand Up @@ -219,12 +220,6 @@ function fastifySession (fastify, options, next) {
return opts
}

function isConnectionSecure (request) {
return (
request.raw.socket?.encrypted === true || request.headers['x-forwarded-proto'] === 'https'
)
}

function shouldSaveSession (request, cookieId, saveUninitializedSession, rollingSessions) {
return cookieId !== request.session.encryptedSessionId
? saveUninitializedSession || request.session.isModified()
Expand Down
35 changes: 35 additions & 0 deletions lib/getCookieOpts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const isConnectionSecure = require('./isConnectionSecure')

module.exports = function getCookieOpts (cookieOpts, request) {
const originalMaxAge = cookieOpts.originalMaxAge || cookieOpts.maxAge || null
let secure = cookieOpts.secure || null
let sameSite = cookieOpts.sameSite || null
let expires = null

if (originalMaxAge) {
expires = new Date(Date.now() + originalMaxAge)
} else if (cookieOpts.expires) {
expires = new Date(cookieOpts.expires)
}

if (secure === 'auto') {
if (isConnectionSecure(request)) {
secure = true
} else {
sameSite = 'Lax'
secure = false
}
}

return {
expires,
originalMaxAge,
sameSite,
secure,
path: cookieOpts.path || '/',
httpOnly: cookieOpts.httpOnly !== undefined ? cookieOpts.httpOnly : true,
domain: cookieOpts.domain || null
}
}
8 changes: 8 additions & 0 deletions lib/isConnectionSecure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = function isConnectionSecure (request) {
return (
request.raw.socket?.encrypted === true ||
request.headers['x-forwarded-proto'] === 'https'
)
}
4 changes: 2 additions & 2 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const crypto = require('crypto')

const Cookie = require('./cookie')
const getCookieOpts = require('./getCookieOpts')
const { configure: configureStringifier } = require('safe-stable-stringify')

const stringify = configureStringifier({ bigint: false })
Expand Down Expand Up @@ -38,7 +38,7 @@ module.exports = class Session {
prevSession[sessionIdKey] === sessionId &&
prevSession[encryptedSessionIdKey]
) || cookieSigner.sign(this.sessionId)
this.cookie = new Cookie(prevSession?.cookie || cookieOpts)
this.cookie = getCookieOpts(prevSession?.cookie || cookieOpts, request)

if (prevSession) {
// Copy over values from the previous session
Expand Down

0 comments on commit 5c85d24

Please sign in to comment.