Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function fastifySession (fastify, options, next) {

const cookieSessionId = getCookieSessionId(request)
const saveSession = shouldSaveSession(request, cookieSessionId, saveUninitializedSession, rollingSessions)
const isInsecureConnection = session.cookie.secure === true && request.protocol !== 'https'
const isInsecureConnection = session.cookie.secure === true && request.protocol !== 'https' && request.hostname !== 'localhost'
const sessionIdWithPrefix = hasCookiePrefix ? `${cookiePrefix}${session.encryptedSessionId}` : session.encryptedSessionId
if (!saveSession || isInsecureConnection) {
// if a session cookie is set, but has a different ID, clear it
Expand Down
3 changes: 2 additions & 1 deletion lib/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ module.exports = class Cookie {
}

if (this.secure === 'auto') {
if (request.protocol === 'https') {
if (request.protocol === 'https' || request.hostname === 'localhost') {
this.secure = true
} else {
this.sameSite = 'lax'
this.partitioned = false
this.secure = false
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ test('should use session cookie secure override when saving secure cookie', asyn
t.after(() => { fastify.close() })

const response = await fastify.inject({
authority: 'fastify-session.test',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this now needed

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the other tests. It defaulted to localhost, but those are tests that explictly require a domain.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come? These are local and done via .inject()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. And as far as I remember when I debugged it. Inject defaulted to localhost as the authority, while the tests expected an insecure request, as the previously expected localhost to be insecure.

url: '/'
})

Expand Down Expand Up @@ -361,6 +362,7 @@ test('should set session cookie secureAuto', async (t) => {
t.after(() => { fastify.close() })

const response = await fastify.inject({
authority: 'fastify-session.test',
url: '/'
})

Expand Down Expand Up @@ -388,6 +390,7 @@ test('should set session cookie secureAuto change SameSite', async (t) => {
t.after(() => { fastify.close() })

const response = await fastify.inject({
authority: 'fastify-session.test',
url: '/'
})

Expand Down
1 change: 1 addition & 0 deletions test/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,7 @@ test('Override global options with regenerate', async t => {
fastify.register(fastifyCookie)
fastify.register(fastifySession, {
...DEFAULT_OPTIONS,
saveUninitialized: true,
cookie: {
secure: false,
maxAge: 42,
Expand Down
2 changes: 1 addition & 1 deletion test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fastifySession = require('..')
const TestStore = require('./TestStore')

const DEFAULT_SECRET = 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk'
const DEFAULT_OPTIONS = { secret: DEFAULT_SECRET }
const DEFAULT_OPTIONS = { secret: DEFAULT_SECRET, saveUninitialized: false }
const DEFAULT_SESSION_ID = 'Qk_XT2K7-clT-x1tVvoY6tIQ83iP72KN'
const DEFAULT_ENCRYPTED_SESSION_ID = `${DEFAULT_SESSION_ID}.B7fUDYXU9fXF9pNuL3qm4NVmSduLJ6kzCOPh5JhHGoE`
const DEFAULT_COOKIE_VALUE = `sessionId=${DEFAULT_ENCRYPTED_SESSION_ID};`
Expand Down
Loading