Skip to content

Commit 37be2a3

Browse files
committed
fix: update IP address literals detection routines
The simpler regexps used earlier supported one of numerous IPv6 literal formats. The updated implementation uses a more comprehensive library.
1 parent 954644b commit 37be2a3

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"fs-xattr": "0.3.1",
6565
"glob": "^11.0.0",
6666
"ioredis": "^5.2.4",
67+
"ip-address": "^10.0.1",
6768
"jsonwebtoken": "^9.0.2",
6869
"knex": "^3.1.0",
6970
"lru-cache": "^10.2.0",

src/internal/database/util.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { logger } from '@internal/monitoring'
22
import { ConnectionOptions } from 'tls'
3+
import ipAddr from 'ip-address'
34

45
export function getSslSettings({
56
connectionString,
@@ -27,10 +28,7 @@ export function getSslSettings({
2728
}
2829

2930
export function isIpAddress(ip: string) {
30-
const ipv4Pattern = /^(\d{1,3}\.){3}\d{1,3}$/
31-
const ipv6Pattern = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/
32-
33-
// IP might be URL-encoded and won't match the regex unless we decode first
31+
// IP might be URL-encoded
3432
const decodedIp = decodeURIComponent(ip)
35-
return ipv4Pattern.test(decodedIp) || ipv6Pattern.test(decodedIp)
33+
return ipAddr.Address6.isValid(decodedIp) || ipAddr.Address4.isValid(decodedIp)
3634
}

0 commit comments

Comments
 (0)