Skip to content

Commit

Permalink
fix: use request.protocol to check for HTTPS (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohd-akram committed Mar 21, 2024
1 parent c474dbe commit cb3346f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 9 deletions.
9 changes: 1 addition & 8 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function fastifyCookieSetCookie (reply, name, value, options) {
}

if (opts.secure === 'auto') {
if (isConnectionSecure(reply.request)) {
if (reply.request.protocol === 'https') {
opts.secure = true
} else {
opts.sameSite = 'lax'
Expand Down Expand Up @@ -187,13 +187,6 @@ function getHook (hook = 'onRequest') {
return hooks[hook]
}

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

const fastifyCookie = fp(plugin, {
fastify: '4.x',
name: '@fastify/cookie'
Expand Down
2 changes: 1 addition & 1 deletion test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ test('create signed cookie manually using signCookie decorator', async (t) => {
})

test('handle secure:auto of cookieOptions', async (t) => {
const fastify = Fastify()
const fastify = Fastify({ trustProxy: true })

await fastify.register(plugin)

Expand Down

0 comments on commit cb3346f

Please sign in to comment.