Skip to content

Commit 7f80980

Browse files
committed
Add support for sslnegotiation=direct - fixes #1104
1 parent 36a53f6 commit 7f80980

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ const sql = postgres('postgres://username:password@host:port/database', {
988988
username : '', // Username of database user
989989
password : '', // Password of database user
990990
ssl : false, // true, prefer, require, tls.connect options
991+
sslnegotiation : null, // direct
991992
max : 10, // Max number of connections
992993
max_lifetime : null, // Max lifetime in seconds (more info below)
993994
idle_timeout : 0, // Idle connection timeout in seconds

src/connection.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const errorFields = {
5151

5252
function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose = noop } = {}) {
5353
const {
54+
sslnegotiation,
5455
ssl,
5556
max,
5657
user,
@@ -262,25 +263,29 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
262263
}
263264

264265
async function secure() {
265-
write(SSLRequest)
266-
const canSSL = await new Promise(r => socket.once('data', x => r(x[0] === 83))) // S
267-
268-
if (!canSSL && ssl === 'prefer')
269-
return connected()
270-
271-
socket.removeAllListeners()
272-
socket = tls.connect({
266+
if (sslnegotiation !== 'direct') {
267+
write(SSLRequest)
268+
const canSSL = await new Promise(r => socket.once('data', x => r(x[0] === 83))) // S
269+
270+
if (!canSSL && ssl === 'prefer')
271+
return connected()
272+
}
273+
274+
const options = {
273275
socket,
274276
servername: net.isIP(socket.host) ? undefined : socket.host,
275-
...(ssl === 'require' || ssl === 'allow' || ssl === 'prefer'
276-
? { rejectUnauthorized: false }
277-
: ssl === 'verify-full'
278-
? {}
279-
: typeof ssl === 'object'
280-
? ssl
281-
: {}
282-
)
283-
})
277+
}
278+
279+
if (sslnegotiation === 'direct')
280+
options.ALPNProtocols = ['postgresql']
281+
282+
if (ssl === 'require' || ssl === 'allow' || ssl === 'prefer')
283+
options.rejectUnauthorized = false
284+
else if (typeof ssl === 'object')
285+
Object.assign(options, ssl)
286+
287+
socket.removeAllListeners()
288+
socket = tls.connect(options)
284289
socket.on('secureConnect', connected)
285290
socket.on('error', error)
286291
socket.on('close', closed)

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ function parseOptions(a, b) {
448448
const defaults = {
449449
max : 10,
450450
ssl : false,
451+
sslnegotiation : null,
451452
idle_timeout : null,
452453
connect_timeout : 30,
453454
max_lifetime : max_lifetime,

tests/bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ exec('psql', ['-c', 'alter database postgres_js_test owner to postgres_js_test']
2020

2121
export function exec(cmd, args) {
2222
const { stderr } = spawnSync(cmd, args, { stdio: 'pipe', encoding: 'utf8' })
23-
if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist'))
23+
if (stderr && !stderr.includes('already exists') && !stderr.includes('does not exist') && !stderr.includes('WARNING:'))
2424
throw stderr
2525
}
2626

tests/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ t('Connect using SSL require', async() =>
401401
}))]
402402
)
403403

404+
t('Connect using SSL direct', async() => {
405+
const [{ supported }] = await sql`select current_setting('server_version_num')::int >= 180000 as supported`
406+
return [true, !supported || (await new Promise((resolve, reject) => {
407+
postgres({
408+
ssl: 'require',
409+
sslnegotiation: 'direct',
410+
idle_timeout
411+
})`select 1`.then(() => resolve(true), reject)
412+
}))]
413+
})
414+
404415
t('Connect using SSL prefer', async() => {
405416
await exec('psql', ['-c', 'alter system set ssl=off'])
406417
await exec('psql', ['-c', 'select pg_reload_conf()'])

0 commit comments

Comments
 (0)