Skip to content

Commit

Permalink
Do not crash if responding from a previously-registered onRequest hook
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed Aug 2, 2023
1 parent 9449eb2 commit dfb7b22
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
27 changes: 15 additions & 12 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const kReplySetCookies = Symbol('fastify.reply.setCookies')
const kReplySetCookiesHookRan = Symbol('fastify.reply.setCookiesHookRan')

function fastifyCookieSetCookie (reply, name, value, options) {
parseCookies(reply.context.server, reply.request, reply)

const opts = Object.assign({}, options)

if (opts.expires && Number.isInteger(opts.expires)) {
Expand Down Expand Up @@ -46,24 +48,25 @@ function fastifyCookieClearCookie (reply, name, options) {
return fastifyCookieSetCookie(reply, name, '', opts)
}

function parseCookies (fastify, request, reply) {
if (reply[kReplySetCookies]) return

request.cookies = {} // New container per request. Issue #53
const cookieHeader = request.raw.headers.cookie
if (cookieHeader) {
request.cookies = fastify.parseCookie(cookieHeader)
}
reply[kReplySetCookies] = new Map()
}

function onReqHandlerWrapper (fastify, hook) {
return hook === 'preParsing'
? function fastifyCookieHandler (fastifyReq, fastifyRes, payload, done) {
fastifyReq.cookies = {} // New container per request. Issue #53
const cookieHeader = fastifyReq.raw.headers.cookie
if (cookieHeader) {
fastifyReq.cookies = fastify.parseCookie(cookieHeader)
}
fastifyRes[kReplySetCookies] = new Map()
parseCookies(fastify, fastifyReq, fastifyRes)
done()
}
: function fastifyCookieHandler (fastifyReq, fastifyRes, done) {
fastifyReq.cookies = {} // New container per request. Issue #53
const cookieHeader = fastifyReq.raw.headers.cookie
if (cookieHeader) {
fastifyReq.cookies = fastify.parseCookie(cookieHeader)
}
fastifyRes[kReplySetCookies] = new Map()
parseCookies(fastify, fastifyReq, fastifyRes)
done()
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1226,3 +1226,29 @@ test('cookies get set correctly if set inside multiple onSends', (t) => {
t.equal(cookies[1].path, '/')
})
})

test('cookies get set correctly if set inside onRequest', (t) => {
t.plan(7)
const fastify = Fastify()
fastify.addHook('onRequest', async (req, reply) => {
reply.setCookie('foo', 'foo', { path: '/' })
return reply.send({ hello: 'world' })
})

fastify.register(plugin)

fastify.inject({
method: 'GET',
url: '/test1'
}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.same(JSON.parse(res.body), { hello: 'world' })

const cookies = res.cookies
t.equal(cookies.length, 1)
t.equal(cookies[0].name, 'foo')
t.equal(cookies[0].value, 'foo')
t.equal(cookies[0].path, '/')
})
})

0 comments on commit dfb7b22

Please sign in to comment.