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: no check on null or undefined values passed as fn #4367

Merged
merged 2 commits into from Oct 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions fastify.js
Expand Up @@ -566,17 +566,21 @@ function fastify (options) {
function addHook (name, fn) {
throwIfAlreadyStarted('Cannot call "addHook" when fastify instance is already started!')

if (fn == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only expect function.

Suggested change
if (fn == null) {
if (typeof fn !== 'function') {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's open a good first issue for that wdyt?

throw new errorCodes.FST_ERR_HOOK_INVALID_HANDLER(name, fn)
}

if (name === 'onSend' || name === 'preSerialization' || name === 'onError' || name === 'preParsing') {
if (fn.constructor.name === 'AsyncFunction' && fn.length === 4) {
throw new Error('Async function has too many arguments. Async hooks should not use the \'done\' argument.')
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
} else if (name === 'onReady') {
if (fn.constructor.name === 'AsyncFunction' && fn.length !== 0) {
throw new Error('Async function has too many arguments. Async hooks should not use the \'done\' argument.')
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
} else {
if (fn.constructor.name === 'AsyncFunction' && fn.length === 3) {
throw new Error('Async function has too many arguments. Async hooks should not use the \'done\' argument.')
throw new errorCodes.FST_ERR_HOOK_INVALID_ASYNC_HANDLER()
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/errors.js
Expand Up @@ -101,6 +101,12 @@ const codes = {
500,
TypeError
),
FST_ERR_HOOK_INVALID_ASYNC_HANDLER: createError(
'FST_ERR_HOOK_INVALID_ASYNC_HANDLER',
'Async function has too many arguments. Async hooks should not use the \'done\' argument.',
500,
TypeError
),

/**
* Middlewares
Expand Down
8 changes: 6 additions & 2 deletions test/hooks-async.test.js
Expand Up @@ -585,33 +585,37 @@ test('preHandler respond with a stream', t => {

test('Should log a warning if is an async function with `done`', t => {
t.test('3 arguments', t => {
t.plan(1)
t.plan(2)
const fastify = Fastify()

try {
fastify.addHook('onRequest', async (req, reply, done) => {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
})

t.test('4 arguments', t => {
t.plan(3)
t.plan(6)
const fastify = Fastify()

try {
fastify.addHook('onSend', async (req, reply, payload, done) => {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
try {
fastify.addHook('preSerialization', async (req, reply, payload, done) => {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
try {
fastify.addHook('onError', async (req, reply, payload, done) => {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
})
Expand Down
3 changes: 2 additions & 1 deletion test/hooks.on-ready.test.js
Expand Up @@ -291,12 +291,13 @@ t.test('onReady cannot add lifecycle hooks', t => {
})

t.test('onReady throw loading error', t => {
t.plan(1)
t.plan(2)
const fastify = Fastify()

try {
fastify.addHook('onReady', async function (done) {})
} catch (e) {
t.ok(e.code, 'FST_ERR_HOOK_INVALID_ASYNC_HANDLER')
t.ok(e.message === 'Async function has too many arguments. Async hooks should not use the \'done\' argument.')
}
})
Expand Down
18 changes: 17 additions & 1 deletion test/hooks.test.js
Expand Up @@ -23,7 +23,7 @@ function getUrl (app) {
}

test('hooks', t => {
t.plan(43)
t.plan(49)
const fastify = Fastify({ exposeHeadRoutes: false })

try {
Expand All @@ -41,6 +41,22 @@ test('hooks', t => {
t.fail()
}

try {
fastify.addHook('preHandler', null)
} catch (e) {
t.equal(e.code, 'FST_ERR_HOOK_INVALID_HANDLER')
t.equal(e.message, 'preHandler hook should be a function, instead got null')
t.pass()
}

try {
fastify.addHook('preParsing')
} catch (e) {
t.equal(e.code, 'FST_ERR_HOOK_INVALID_HANDLER')
t.equal(e.message, 'preParsing hook should be a function, instead got undefined')
t.pass()
}

try {
fastify.addHook('preParsing', function (request, reply, payload, done) {
request.preParsing = true
Expand Down