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

fix: use request.protocol to check for HTTPS #282

Merged
merged 1 commit into from
Mar 21, 2024
Merged
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
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