From dfb7b2285a0a28ba214bc6f091ab06ed0ea468bc Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 2 Aug 2023 15:12:54 +0200 Subject: [PATCH] Do not crash if responding from a previously-registered onRequest hook Signed-off-by: Matteo Collina --- plugin.js | 27 +++++++++++++++------------ test/cookie.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/plugin.js b/plugin.js index 8989e54..66a418a 100644 --- a/plugin.js +++ b/plugin.js @@ -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)) { @@ -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() } } diff --git a/test/cookie.test.js b/test/cookie.test.js index 0dacac1..48b2f94 100644 --- a/test/cookie.test.js +++ b/test/cookie.test.js @@ -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, '/') + }) +})