Skip to content

Commit b29adb6

Browse files
Liam-TaitCopilot
andauthored
feat: detect and report self-signed certificate error in tls connections (#236)
* feat: provide friendly error message for self-signed certificate in TLS connection error * Update index.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Liam Tait <Liam-Tait@users.noreply.github.com> * Update index.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Liam Tait <Liam-Tait@users.noreply.github.com> * remove custom error message for self signed cert in chain * test: add handling for SELF_SIGNED_CERT_IN_CHAIN error in Redis connection * use single quotes --------- Signed-off-by: Liam Tait <Liam-Tait@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 30ff8aa commit b29adb6

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ function fastifyRedis (fastify, options, next) {
9393
onEnd(err)
9494
return
9595
}
96+
if (err.code === 'SELF_SIGNED_CERT_IN_CHAIN') {
97+
// This error is not recoverable because ioredis will never be able to connect to the server unless the user changes the TLS options.
98+
onEnd(err)
99+
return
100+
}
96101

97102
// Swallow network errors to allow ioredis
98103
// to perform reconnection and emit 'end'

test/index.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,36 @@ test('catch .ping() errors', async (t) => {
405405
await t.assert.rejects(fastify.ready(), new Redis.ReplyError('ping error'))
406406
})
407407

408+
test('Should propagate SELF_SIGNED_CERT_IN_CHAIN error', async (t) => {
409+
t.plan(1)
410+
411+
const fastify = Fastify()
412+
t.after(() => fastify.close())
413+
414+
const fastifyRedis = proxyquire('..', {
415+
ioredis: function Redis () {
416+
this.ping = () => {
417+
const error = new Error('self signed certificate in certificate chain')
418+
error.code = 'SELF_SIGNED_CERT_IN_CHAIN'
419+
return Promise.reject(error)
420+
}
421+
this.quit = () => {}
422+
this.info = cb => cb(null, 'info')
423+
this.on = function () {
424+
return this
425+
}
426+
this.off = function () { return this }
427+
428+
return this
429+
}
430+
})
431+
fastify.register(fastifyRedis)
432+
433+
const error = new Error('self signed certificate in certificate chain')
434+
error.code = 'SELF_SIGNED_CERT_IN_CHAIN'
435+
await t.assert.rejects(fastify.ready(), error)
436+
})
437+
408438
setInterval(() => {
409439
whyIsNodeRunning()
410440
}, 5000).unref()

0 commit comments

Comments
 (0)