Skip to content

Commit

Permalink
Prevents bad ssl credentials from causing a crash
Browse files Browse the repository at this point in the history
Fixes: #2307
Fixes: #2004
  • Loading branch information
chris--young committed Aug 12, 2020
1 parent 316bec3 commit f4d123b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/pg/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ class Connection extends EventEmitter {
if (net.isIP(host) === 0) {
options.servername = host
}
self.stream = tls.connect(options)
try {
self.stream = tls.connect(options)
} catch (err) {
return self.emit('error', err)
}
self.attachListeners(self.stream)
self.stream.on('error', reportStreamError)

Expand Down
24 changes: 24 additions & 0 deletions packages/pg/test/integration/gh-issues/2307-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict'

const pg = require('../../../lib')
const helper = require('../test-helper')

const suite = new helper.Suite()

suite.test('bad ssl credentials do not cause crash', (done) => {
const config = {
ssl: {
ca: 'invalid_value',
key: 'invalid_value',
cert: 'invalid_value',
},
}

const client = new pg.Client(config)

client.connect((err) => {
assert(err)
client.end()
done()
})
})

0 comments on commit f4d123b

Please sign in to comment.