Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: IP blacklisting feature issues with IPv6

Blue Mouseb61b2786dc5a
diff --git a/backend/apps/cloud/src/common/constants.ts b/backend/apps/cloud/src/common/constants.ts
index 8e075989e..9e1f32eda 100644
--- a/backend/apps/cloud/src/common/constants.ts
+++ b/backend/apps/cloud/src/common/constants.ts
@@ -32,8 +32,6 @@ const { TWO_FACTOR_AUTHENTICATION_APP_NAME } = process.env
const ORIGINS_REGEX =
/^(?=.{1,255}$)([0-9A-Za-z*:](?:(?:[0-9A-Za-z*:]|-){0,61}[0-9A-Za-z*:])?(?:\.[0-9A-Za-z*:](?:(?:[0-9A-Za-z*-]|-){0,61}[0-9A-Za-z*:])?)*)?$/
-const IP_REGEX =
- /^(([12]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])(\.|\/)){4}([1-2]?[0-9]|3[0-2])$/
const PID_REGEX = /^(?!.*--)[a-zA-Z0-9-]{12}$/
const isValidPID = (pid: string) => PID_REGEX.test(pid)
@@ -250,7 +248,6 @@ export {
SEND_WARNING_AT_PERC,
PROJECT_INVITE_EXPIRE,
TWO_FACTOR_AUTHENTICATION_APP_NAME,
- IP_REGEX,
ORIGINS_REGEX,
EMAIL_ACTION_ENCRYPTION_KEY,
isDevelopment,
diff --git a/backend/apps/cloud/src/common/ip-range.spec.ts b/backend/apps/cloud/src/common/ip-range.spec.ts
new file mode 100644
index 000000000..2aa0df2be
--- /dev/null
+++ b/backend/apps/cloud/src/common/ip-range.spec.ts
@@ -0,0 +1,51 @@
+import { isIpInRange, isValidIpRange } from './ip-range'
+
+describe('IP range validation', () => {
+ it.each([
+ ['203.0.113.42', '203.0.113.42'],
+ ['203.0.113.0/24', '203.0.113.42'],
+ ['203.0.113.42/32', '203.0.113.42'],
+ ['0.0.0.0/0', '203.0.113.42'],
+ ['2001:db8::1', '2001:0DB8:0:0:0:0:0:1'],
+ ['2001:db8::/64', '2001:db8::42'],
+ ['2001:db8::1/128', '2001:db8::1'],
+ ['::/0', '2001:db8::42'],
+ ['::1', '::1'],
+ ['::ffff:203.0.113.42', '203.0.113.42'],
+ ['::ffff:203.0.113.0/120', '::ffff:203.0.113.42'],
+ ])('accepts %s and matches %s', (range, address) => {
+ expect(isValidIpRange(range)).toBe(true)
+ expect(isIpInRange(address, range)).toBe(true)
+ })
+
+ it.each([
+ '',
+ '203.0.113.0.24',
+ '203/0/113/0/24',
+ '203.0.113.256',
+ '203.000.113.42',
+ '203.0.113.0/33',
+ '203.0.113.0/-1',
+ '203.0.113.0/24.5',
+ '203.0.113.0/',
+ '203.0.113.0/24/1',
+ '203.0.113.0/24junk',
+ '2001:db8::/129',
+ '2001:db8::/-1',
+ '2001:db8::/',
+ '2001:db8::/64/1',
+ '2001:db8:::1',
+ '::ffff:203.0.113.0/129',
+ ])('rejects malformed address or range %s', (range) => {
+ expect(isValidIpRange(range)).toBe(false)
+ })
+
+ it.each([
+ ['203.0.113.0/24', '203.0.114.42'],
+ ['2001:db8::/64', '2001:db8:0:1::42'],
+ ['2001:db8::1/128', '2001:db8::2'],
+ ])('does not match %s against %s', (range, address) => {
+ expect(isValidIpRange(range)).toBe(true)
+ expect(isIpInRange(address, range)).toBe(false)
+ })
+})
diff --git a/backend/apps/cloud/src/common/ip-range.ts b/backend/apps/cloud/src/common/ip-range.ts
index 805bedfd2..4d3386524 100644
--- a/backend/apps/cloud/src/common/ip-range.ts
+++ b/backend/apps/cloud/src/common/ip-range.ts
@@ -102,6 +102,18 @@ const parseIp = (
return null
}
+export const isValidIpRange = (range: string): boolean => {
+ const parts = range.split('/')
+ if (parts.length > 2) return false
+
+ const [address, prefix] = parts
+ const parsed = parseIp(address, { preserveIPv4Mapped: true })
+ if (!parsed) return false
+ if (prefix === undefined) return true
+
+ return /^\d+$/.test(prefix) && Number(prefix) <= parsed.bytes.length * 8
+}
+
const matchesPrefix = (
target: ParsedIp,
range: ParsedIp,
diff --git a/backend/apps/cloud/src/project/project.service.ts b/backend/apps/cloud/src/project/project.service.ts
index dac927f35..c37397cb2 100644
--- a/backend/apps/cloud/src/project/project.service.ts
+++ b/backend/apps/cloud/src/project/project.service.ts
@@ -1,4 +1,3 @@
-import net from 'net'
import {
ForbiddenException,
Injectable,
@@ -50,7 +49,6 @@ import {
redisProjectCountCacheTimeout,
getRedisUserCountKey,
redis,
- IP_REGEX,
ORIGINS_REGEX,
getRedisProjectKey,
redisProjectCacheTimeout,
@@ -63,6 +61,7 @@ import {
V2_VIEW_FILTER_DIMENSIONS,
} from '../common/constants'
import { clickhouse } from '../common/integrations/clickhouse'
+import { isValidIpRange } from '../common/ip-range'
import { IUsageInfoRedis } from '../user/interfaces'
import { ProjectSubscriber, Funnel, Annotation, PinnedProject } from './entity'
import { AddSubscriberType } from './types'
@@ -786,7 +785,7 @@ export class ProjectService {
'The list of allowed blacklisted IP addresses must be less than 300 characters.',
)
_map(projectDTO.ipBlacklist, (ip) => {
- if (!net.isIP(_trim(ip)) && !IP_REGEX.test(_trim(ip))) {
+ if (!isValidIpRange(_trim(ip))) {
throw new ConflictException(`IP address ${ip} is not correct`)
}
})
@@ -809,7 +808,7 @@ export class ProjectService {
'The list of whitelisted IP addresses must be less than 300 characters.',
)
_map(projectDTO.ipWhitelist, (ip) => {
- if (!net.isIP(_trim(ip)) && !IP_REGEX.test(_trim(ip))) {
+ if (!isValidIpRange(_trim(ip))) {
throw new ConflictException(`IP address ${ip} is not correct`)
}
})
diff --git a/backend/apps/community/src/common/constants.ts b/backend/apps/community/src/common/constants.ts
index c53bcce91..0214e5005 100644
--- a/backend/apps/community/src/common/constants.ts
+++ b/backend/apps/community/src/common/constants.ts
@@ -67,8 +67,6 @@ const OIDC_ONLY_AUTH = process.env.OIDC_ONLY_AUTH === 'true'
const ORIGINS_REGEX =
/^(?=.{1,255}$)([0-9A-Za-z*:](?:(?:[0-9A-Za-z*:]|-){0,61}[0-9A-Za-z*:])?(?:\.[0-9A-Za-z*:](?:(?:[0-9A-Za-z*-]|-){0,61}[0-9A-Za-z*:])?)*)?$/
-const IP_REGEX =
- /^(([12]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])(\.|\/)){4}([1-2]?[0-9]|3[0-2])$/
const PID_REGEX = /^(?!.*--)[a-zA-Z0-9-]{12}$/
const isValidPID = (pid: string) => PID_REGEX.test(pid)
@@ -194,7 +192,6 @@ export {
UNIQUE_SESSION_LIFE_TIME,
ONLINE_VISITORS_WINDOW_MINUTES,
TRAFFIC_METAKEY_COLUMNS,
- IP_REGEX,
ORIGINS_REGEX,
isDevelopment,
NUMBER_JWT_REFRESH_TOKEN_LIFETIME as JWT_REFRESH_TOKEN_LIFETIME,
diff --git a/backend/apps/community/src/common/ip-range.spec.ts b/backend/apps/community/src/common/ip-range.spec.ts
new file mode 100644
index 000000000..2aa0df2be
--- /dev/null
+++ b/backend/apps/community/src/common/ip-range.spec.ts
@@ -0,0 +1,51 @@
+import { isIpInRange, isValidIpRange } from './ip-range'
+
+describe('IP range validation', () => {
+ it.each([
+ ['203.0.113.42', '203.0.113.42'],
+ ['203.0.113.0/24', '203.0.113.42'],
+ ['203.0.113.42/32', '203.0.113.42'],
+ ['0.0.0.0/0', '203.0.113.42'],
+ ['2001:db8::1', '2001:0DB8:0:0:0:0:0:1'],
+ ['2001:db8::/64', '2001:db8::42'],
+ ['2001:db8::1/128', '2001:db8::1'],
+ ['::/0', '2001:db8::42'],
+ ['::1', '::1'],
+ ['::ffff:203.0.113.42', '203.0.113.42'],
+ ['::ffff:203.0.113.0/120', '::ffff:203.0.113.42'],
+ ])('accepts %s and matches %s', (range, address) => {
+ expect(isValidIpRange(range)).toBe(true)
+ expect(isIpInRange(address, range)).toBe(true)
+ })
+
+ it.each([
+ '',
+ '203.0.113.0.24',
+ '203/0/113/0/24',
+ '203.0.113.256',
+ '203.000.113.42',
+ '203.0.113.0/33',
+ '203.0.113.0/-1',
+ '203.0.113.0/24.5',
+ '203.0.113.0/',
+ '203.0.113.0/24/1',
+ '203.0.113.0/24junk',
+ '2001:db8::/129',
+ '2001:db8::/-1',
+ '2001:db8::/',
+ '2001:db8::/64/1',
+ '2001:db8:::1',
+ '::ffff:203.0.113.0/129',
+ ])('rejects malformed address or range %s', (range) => {
+ expect(isValidIpRange(range)).toBe(false)
+ })
+
+ it.each([
+ ['203.0.113.0/24', '203.0.114.42'],
+ ['2001:db8::/64', '2001:db8:0:1::42'],
+ ['2001:db8::1/128', '2001:db8::2'],
+ ])('does not match %s against %s', (range, address) => {
+ expect(isValidIpRange(range)).toBe(true)
+ expect(isIpInRange(address, range)).toBe(false)
+ })
+})
diff --git a/backend/apps/community/src/common/ip-range.ts b/backend/apps/community/src/common/ip-range.ts
index 805bedfd2..4d3386524 100644
--- a/backend/apps/community/src/common/ip-range.ts
+++ b/backend/apps/community/src/common/ip-range.ts
@@ -102,6 +102,18 @@ const parseIp = (
return null
}
+export const isValidIpRange = (range: string): boolean => {
+ const parts = range.split('/')
+ if (parts.length > 2) return false
+
+ const [address, prefix] = parts
+ const parsed = parseIp(address, { preserveIPv4Mapped: true })
+ if (!parsed) return false
+ if (prefix === undefined) return true
+
+ return /^\d+$/.test(prefix) && Number(prefix) <= parsed.bytes.length * 8
+}
+
const matchesPrefix = (
target: ParsedIp,
range: ParsedIp,
diff --git a/backend/apps/community/src/project/project.service.ts b/backend/apps/community/src/project/project.service.ts
index 0bc163a30..eaa9bf037 100644
--- a/backend/apps/community/src/project/project.service.ts
+++ b/backend/apps/community/src/project/project.service.ts
@@ -1,4 +1,3 @@
-import net from 'net'
import {
ForbiddenException,
Injectable,
@@ -28,13 +27,13 @@ import { ProjectDTO } from './dto/project.dto'
import {
isValidPID,
redis,
- IP_REGEX,
ORIGINS_REGEX,
getRedisProjectKey,
redisProjectCacheTimeout,
V2_VIEW_FILTER_DIMENSIONS,
} from '../common/constants'
import { clickhouse } from '../common/integrations/clickhouse'
+import { isValidIpRange } from '../common/ip-range'
import {
createProjectShareClickhouse,
findProjectSharesByProjectClickhouse,
@@ -602,7 +601,7 @@ export class ProjectService {
'The list of allowed blacklisted IP addresses must be less than 300 characters.',
)
_map(projectDTO.ipBlacklist, (ip) => {
- if (!net.isIP(_trim(ip)) && !IP_REGEX.test(_trim(ip))) {
+ if (!isValidIpRange(_trim(ip))) {
throw new ConflictException(`IP address ${ip} is not correct`)
}
})
@@ -623,7 +622,7 @@ export class ProjectService {
'The list of whitelisted IP addresses must be less than 300 characters.',
)
_map(projectDTO.ipWhitelist, (ip) => {
- if (!net.isIP(_trim(ip)) && !IP_REGEX.test(_trim(ip))) {
+ if (!isValidIpRange(_trim(ip))) {
throw new ConflictException(`IP address ${ip} is not correct`)
}
})