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

enhance checkOptions to reject invalid signer objects #241

Merged
merged 1 commit into from
Apr 18, 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
18 changes: 10 additions & 8 deletions lib/fastifySession.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,16 @@ function fastifySession (fastify, options, next) {
}

function checkOptions (options) {
if (!options.secret) {
return new Error('the secret option is required!')
}
if (typeof options.secret === 'string' && options.secret.length < 32) {
return new Error('the secret must have length 32 or greater')
}
if (Array.isArray(options.secret) && options.secret.length === 0) {
return new Error('at least one secret is required')
if (typeof options.secret === 'string') {
if (options.secret.length < 32) {
return new Error('the secret must have length 32 or greater')
}
} else if (Array.isArray(options.secret)) {
if (options.secret.length === 0) {
return new Error('at least one secret is required')
}
} else if (!(options.secret && typeof options.secret.sign === 'function' && typeof options.secret.unsign === 'function')) {
return new Error('the secret option is required, and must be a String, Array of Strings, or a signer object with .sign and .unsign methods')
}
}

Expand Down
38 changes: 37 additions & 1 deletion test/fastifySession.checkOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('fastifySession.checkOptions: register should fail if no secret is specifie
fastify.register(fastifyCookie)
fastify.register(fastifySession, options)

await t.rejects(fastify.ready(), new Error('the secret option is required!'))
await t.rejects(fastify.ready(), new Error('the secret option is required, and must be a String, Array of Strings, or a signer object with .sign and .unsign methods'))
})

test('fastifySession.checkOptions: register should succeed if secret with 32 characters is specified', async t => {
Expand Down Expand Up @@ -73,3 +73,39 @@ test('fastifySession.checkOptions: register should fail if no secret is present
fastify.register(fastifySession, { secret: [] })
await t.rejects(fastify.ready(), new Error('at least one secret is required'))
})

test('fastifySession.checkOptions: register should fail if a Buffer is passed', async t => {
t.plan(1)
const fastify = Fastify()

fastify.register(fastifyCookie)
fastify.register(fastifySession, { secret: crypto.randomBytes(32) })
await t.rejects(fastify.ready(), new Error('the secret option is required, and must be a String, Array of Strings, or a signer object with .sign and .unsign methods'))
})

test('fastifySession.checkOptions: register should fail if a signer missing unsign is passed', async t => {
t.plan(1)
const fastify = Fastify()

const invalidSigner = {
sign: (x) => x,
unsign: true
}

fastify.register(fastifyCookie)
fastify.register(fastifySession, { secret: invalidSigner })
await t.rejects(fastify.ready(), new Error('the secret option is required, and must be a String, Array of Strings, or a signer object with .sign and .unsign methods'))
})

test('fastifySession.checkOptions: register should fail if a signer missing sign is passed', async t => {
t.plan(1)
const fastify = Fastify()

const invalidSigner = {
unsign: (x) => true
}

fastify.register(fastifyCookie)
fastify.register(fastifySession, { secret: invalidSigner })
await t.rejects(fastify.ready(), new Error('the secret option is required, and must be a String, Array of Strings, or a signer object with .sign and .unsign methods'))
})
Loading