Skip to content

Commit

Permalink
set sameSite: lax by default (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday authored Jan 26, 2024
1 parent 79bed72 commit b3a2f4d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const kReplySetCookiesHookRan = Symbol('fastify.reply.setCookiesHookRan')
function fastifyCookieSetCookie (reply, name, value, options) {
parseCookies(reply.server, reply.request, reply)

const opts = Object.assign({}, options)
const opts = Object.assign({ sameSite: 'lax' }, options)

if (opts.expires && Number.isInteger(opts.expires)) {
opts.expires = new Date(opts.expires)
Expand All @@ -25,7 +25,6 @@ function fastifyCookieSetCookie (reply, name, value, options) {
if (isConnectionSecure(reply.request)) {
opts.secure = true
} else {
opts.sameSite = 'lax'
opts.secure = false
}
}
Expand All @@ -45,6 +44,7 @@ function fastifyCookieClearCookie (reply, name, options) {
signed: undefined,
maxAge: undefined
})

return fastifyCookieSetCookie(reply, name, '', opts)
}

Expand Down
6 changes: 3 additions & 3 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ test('should set multiple cookies', (t) => {
t.equal(cookies[2].name, 'wee')
t.equal(cookies[2].value, 'woo')

t.equal(res.headers['set-cookie'][1], 'bar=test; Partitioned')
t.equal(res.headers['set-cookie'][2], 'wee=woo; Secure; Partitioned')
t.equal(res.headers['set-cookie'][1], 'bar=test; Partitioned; SameSite=Lax')
t.equal(res.headers['set-cookie'][2], 'wee=woo; Secure; Partitioned; SameSite=Lax')
})
})

Expand Down Expand Up @@ -957,7 +957,7 @@ test('result in an error if hook-option is set to an invalid value', (t) => {
const fastify = Fastify()

t.rejects(
() => fastify.register(plugin, { hook: true }),
async () => fastify.register(plugin, { hook: true }),
new Error("@fastify/cookie: Invalid value provided for the hook-option. You can set the hook-option only to false, 'onRequest' , 'preParsing' , 'preValidation' or 'preHandler'")
)
})
Expand Down

0 comments on commit b3a2f4d

Please sign in to comment.