Skip to content

Commit f9ea21c

Browse files
kevcodezdarora
andauthored
fix: disable hostname verification if connecting over an IP literal (#643)
* fix: disable hostname verification if connecting over an IP literal * Update util.test.ts --------- Co-authored-by: Div Arora <[email protected]>
1 parent fcf8b79 commit f9ea21c

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/internal/database/util.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { logger } from '@internal/monitoring'
2+
import { ConnectionOptions } from 'tls'
23

34
export function getSslSettings({
45
connectionString,
56
databaseSSLRootCert,
67
}: {
78
connectionString: string
89
databaseSSLRootCert: string | undefined
9-
}): { ca: string } | undefined {
10+
}): ConnectionOptions | undefined {
1011
if (!databaseSSLRootCert) return undefined
1112

1213
try {
@@ -15,7 +16,7 @@ export function getSslSettings({
1516
// in case the hostname is an IP address
1617
const url = new URL(connectionString)
1718
if (url.hostname && isIpAddress(url.hostname)) {
18-
return undefined
19+
return { ca: databaseSSLRootCert, rejectUnauthorized: false }
1920
}
2021
} catch (err) {
2122
// ignore to ensure this never breaks the connection in case of an invalid URL

src/test/database/util.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ describe('database utils', () => {
2323
).toBeUndefined()
2424
})
2525

26-
test('should return no SSL settings if hostname is an IP address', () => {
26+
test('should return SSL settings if hostname is an IP address', () => {
2727
expect(
2828
getSslSettings({
2929
connectionString: 'postgres://foo:[email protected]:5432/postgres',
3030
databaseSSLRootCert: '<cert>',
3131
})
32-
).toBeUndefined()
32+
).toStrictEqual({ ca: '<cert>', rejectUnauthorized: false })
3333
})
3434

3535
test('should return SSL settings if hostname is not an IP address', () => {

0 commit comments

Comments
 (0)