📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Add rate-limit & bot checks
Blue Mouse7fb787435c3b
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.tsindex 928f15a9b..2e89bd281 100644--- a/backend/apps/cloud/src/analytics/analytics.controller.ts+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts@@ -3311,10 +3311,27 @@ export class AnalyticsController {@Body() dto: IdentifyDto,@Headers() headers,@Ip() reqIP,- ): Promise<{ profileId: string }> {+ ): Promise<{ profileId: string } | typeof BOT_RESPONSE> {const { 'user-agent': userAgent, origin } = headersconst ip = getIPFromHeaders(headers) || reqIP || ''+ await checkRateLimit(ip, 'identify', 120, 60)+ await checkRateLimit(dto.pid, 'identify', 2000, 60)++ const botResult = await this.analyticsService.checkBot(+ dto.pid,+ userAgent,+ headers,+ ip,+ headers.referer || headers.referrer,+ null,+ 'identify',+ )++ if (botResult.isBot) {+ return BOT_RESPONSE+ }+this.analyticsService.validateUserSuppliedProfileId(dto.profileId)await this.analyticsService.validate(dto, origin, ip)diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex b6a7913e1..cd2051a23 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -2458,6 +2458,11 @@ export class AnalyticsService {return `pfa:${pid}:${anonProfileId}`}+ // Sentinel cached for anonymous profiles with no alias, so unlinked (the+ // overwhelmingly common case) lookups don't hit ClickHouse every time.+ // Cannot collide with a real value - those always start with usr_.+ private static readonly PROFILE_ALIAS_NONE = '-'+/*** Looks up the identified (usr_) profile an anonymous (anon_) profile has* been linked to. First identification wins, so the mapping is resolved@@ -2472,7 +2477,7 @@ export class AnalyticsService {const cached = await redis.get(cacheKey)if (cached) {- return cached+ return cached === AnalyticsService.PROFILE_ALIAS_NONE ? null : cached}const query = `@@ -2493,6 +2498,11 @@ export class AnalyticsService {if (userProfileId) {await redis.set(cacheKey, userProfileId, 'EX', 3600)+ } else {+ // Short TTL: a link created concurrently on another node (linkProfiles+ // uses async_insert, so the row may not be SELECTable yet) must become+ // visible shortly after this sentinel expires.+ await redis.set(cacheKey, AnalyticsService.PROFILE_ALIAS_NONE, 'EX', 60)}return userProfileIddiff --git a/backend/apps/cloud/src/analytics/bot-detection.service.ts b/backend/apps/cloud/src/analytics/bot-detection.service.tsindex c05da8e4f..c6f85889a 100644--- a/backend/apps/cloud/src/analytics/bot-detection.service.ts+++ b/backend/apps/cloud/src/analytics/bot-detection.service.ts@@ -23,6 +23,7 @@ export type BotEndpoint =| 'error'| 'feature_flag'| 'heartbeat'+ | 'identify'| 'noscript'| 'session_replay'diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.tsindex a84596c3a..e1f18c5b6 100644--- a/backend/apps/community/src/analytics/analytics.controller.ts+++ b/backend/apps/community/src/analytics/analytics.controller.ts@@ -2802,10 +2802,27 @@ export class AnalyticsController {@Body() dto: IdentifyDto,@Headers() headers,@Ip() reqIP,- ): Promise<{ profileId: string }> {+ ): Promise<{ profileId: string } | typeof BOT_RESPONSE> {const { 'user-agent': userAgent, origin } = headersconst ip = getIPFromHeaders(headers) || reqIP || ''+ await checkRateLimit(ip, 'identify', 120, 60)+ await checkRateLimit(dto.pid, 'identify', 2000, 60)++ const botResult = await this.analyticsService.checkBot(+ dto.pid,+ userAgent,+ headers,+ ip,+ headers.referer || headers.referrer,+ null,+ 'identify',+ )++ if (botResult.isBot) {+ return BOT_RESPONSE+ }+this.analyticsService.validateUserSuppliedProfileId(dto.profileId)await this.analyticsService.validate(dto, origin, ip)diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.tsindex 2c4fdfc7c..9321b44b4 100644--- a/backend/apps/community/src/analytics/analytics.service.ts+++ b/backend/apps/community/src/analytics/analytics.service.ts@@ -2099,6 +2099,11 @@ export class AnalyticsService {return `pfa:${pid}:${anonProfileId}`}+ // Sentinel cached for anonymous profiles with no alias, so unlinked (the+ // overwhelmingly common case) lookups don't hit ClickHouse every time.+ // Cannot collide with a real value - those always start with usr_.+ private static readonly PROFILE_ALIAS_NONE = '-'+/*** Looks up the identified (usr_) profile an anonymous (anon_) profile has* been linked to. First identification wins, so the mapping is resolved@@ -2113,7 +2118,7 @@ export class AnalyticsService {const cached = await redis.get(cacheKey)if (cached) {- return cached+ return cached === AnalyticsService.PROFILE_ALIAS_NONE ? null : cached}const query = `@@ -2134,6 +2139,11 @@ export class AnalyticsService {if (userProfileId) {await redis.set(cacheKey, userProfileId, 'EX', 3600)+ } else {+ // Short TTL: a link created concurrently on another node (linkProfiles+ // uses async_insert, so the row may not be SELECTable yet) must become+ // visible shortly after this sentinel expires.+ await redis.set(cacheKey, AnalyticsService.PROFILE_ALIAS_NONE, 'EX', 60)}return userProfileIddiff --git a/backend/apps/community/src/analytics/bot-detection.service.ts b/backend/apps/community/src/analytics/bot-detection.service.tsindex 4af374566..510e0bfbd 100644--- a/backend/apps/community/src/analytics/bot-detection.service.ts+++ b/backend/apps/community/src/analytics/bot-detection.service.ts@@ -23,6 +23,7 @@ export type BotEndpoint =| 'error'| 'feature_flag'| 'heartbeat'+ | 'identify'| 'noscript'interface BotDetectionInput {