Skip to content

Commit 1bc03de

Browse files
pmalouinhjr3
authored andcommitted
fix(pg-connection-string): get closer to libpq semantics for sslmode
1 parent 5a8b1a7 commit 1bc03de

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

packages/pg-connection-string/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ Query parameters follow a `?` character, including the following special query p
8989
* `ssl=1`, `ssl=true`, `ssl=0`, `ssl=false` - sets `ssl` to true or false, accordingly
9090
* `sslmode=<sslmode>`
9191
* `sslmode=disable` - sets `ssl` to false
92-
* `sslmode=no-verify` - sets `ssl` to `{ rejectUnauthorized: false }`
93-
* `sslmode=prefer`, `sslmode=require`, `sslmode=verify-ca`, `sslmode=verify-full` - sets `ssl` to true
92+
* `sslmode=no-verify`, `sslmode=prefer`, - sets `ssl` to `{ rejectUnauthorized: false }`
93+
* `sslmode=require`, - sets `ssl` to `{ rejectUnauthorized: false }` unless `sslrootcert` is specified, in which case it behaves like `verify-ca`
94+
* `sslmode=verify-ca` - sets `ssl` to `{ checkServerIdentity: no-op}` (verify CA, but not server identity)
95+
* `sslmode=verify-full` - sets `ssl` to `{}` (verify CA and server identity)
9496
* `sslcert=<filename>` - reads data from the given file and includes the result as `ssl.cert`
9597
* `sslkey=<filename>` - reads data from the given file and includes the result as `ssl.key`
9698
* `sslrootcert=<filename>` - reads data from the given file and includes the result as `ssl.ca`

packages/pg-connection-string/index.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,26 @@ function parse(str) {
9393
break
9494
}
9595
case 'prefer':
96-
case 'require':
97-
case 'verify-ca':
98-
case 'verify-full': {
99-
break
100-
}
10196
case 'no-verify': {
10297
config.ssl.rejectUnauthorized = false
10398
break
10499
}
100+
case 'require': {
101+
if (config.sslrootcert) {
102+
// If a root CA is specified, behavior of `sslmode=require` will be the same as that of `verify-ca`
103+
config.ssl.checkServerIdentity = function () {}
104+
} else {
105+
config.ssl.rejectUnauthorized = false
106+
}
107+
break
108+
}
109+
case 'verify-ca': {
110+
config.ssl.checkServerIdentity = function () {}
111+
break
112+
}
113+
case 'verify-full': {
114+
break
115+
}
105116
}
106117

107118
return config

packages/pg-connection-string/test/parse.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,24 @@ describe('parse', function () {
258258
it('configuration parameter sslmode=prefer', function () {
259259
var connectionString = 'pg:///?sslmode=prefer'
260260
var subject = parse(connectionString)
261-
subject.ssl.should.eql({})
261+
subject.ssl.should.eql({
262+
rejectUnauthorized: false,
263+
})
262264
})
263265

264266
it('configuration parameter sslmode=require', function () {
265267
var connectionString = 'pg:///?sslmode=require'
266268
var subject = parse(connectionString)
267-
subject.ssl.should.eql({})
269+
subject.ssl.should.eql({
270+
rejectUnauthorized: false,
271+
})
268272
})
269273

270274
it('configuration parameter sslmode=verify-ca', function () {
271275
var connectionString = 'pg:///?sslmode=verify-ca'
272276
var subject = parse(connectionString)
273-
subject.ssl.should.eql({})
277+
subject.ssl.should.have.property('checkServerIdentity').that.is.a('function')
278+
expect(subject.ssl.checkServerIdentity()).be.undefined
274279
})
275280

276281
it('configuration parameter sslmode=verify-full', function () {
@@ -282,9 +287,9 @@ describe('parse', function () {
282287
it('configuration parameter ssl=true and sslmode=require still work with sslrootcert=/path/to/ca', function () {
283288
var connectionString = 'pg:///?ssl=true&sslrootcert=' + __dirname + '/example.ca&sslmode=require'
284289
var subject = parse(connectionString)
285-
subject.ssl.should.eql({
286-
ca: 'example ca\n',
287-
})
290+
subject.ssl.should.have.property('ca', 'example ca\n')
291+
subject.ssl.should.have.property('checkServerIdentity').that.is.a('function')
292+
expect(subject.ssl.checkServerIdentity()).be.undefined
288293
})
289294

290295
it('allow other params like max, ...', function () {

0 commit comments

Comments
 (0)