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!

Implement caching & analytics read guards

Blue Mouse6be098ac28f3
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.ts
index 51f238bd8..b8b858dc0 100644
--- a/backend/apps/cloud/src/analytics/analytics.controller.ts
+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts
@@ -14,6 +14,7 @@ import {
Query,
Param,
UseGuards,
+ UseInterceptors,
UsePipes,
ValidationPipe,
Get,
@@ -54,6 +55,8 @@ import { GetUserFlowDto } from './dto/getUserFlow.dto'
import { GetFunnelsDto } from './dto/getFunnels.dto'
import { GetFunnelSessionsDto } from './dto/get-funnel-sessions.dto'
import { AppLoggerService } from '../logger/logger.service'
+import { AnalyticsReadGuard } from './protection/analytics-read.guard'
+import { PublicProjectCacheInterceptor } from './protection/public-project-cache.interceptor'
import {
redis,
REDIS_USERS_COUNT_KEY,
@@ -234,7 +237,8 @@ const getPageFromReferrer = (referrer?: string | string[] | null) => {
}
@ApiTags('Analytics')
-@UseGuards(OptionalJwtAccessTokenGuard, AuthenticationGuard)
+@UseGuards(OptionalJwtAccessTokenGuard, AuthenticationGuard, AnalyticsReadGuard)
+@UseInterceptors(PublicProjectCacheInterceptor)
@UsePipes(
new ValidationPipe({
transform: true,
diff --git a/backend/apps/cloud/src/analytics/analytics.module.ts b/backend/apps/cloud/src/analytics/analytics.module.ts
index 7adc7bcfa..ec3de0c61 100644
--- a/backend/apps/cloud/src/analytics/analytics.module.ts
+++ b/backend/apps/cloud/src/analytics/analytics.module.ts
@@ -5,6 +5,8 @@ import { BullModule } from '@nestjs/bullmq'
import { AnalyticsService } from './analytics.service'
import { AnalyticsController } from './analytics.controller'
import { BotDetectionService } from './bot-detection.service'
+import { AnalyticsReadGuard } from './protection/analytics-read.guard'
+import { PublicProjectCacheInterceptor } from './protection/public-project-cache.interceptor'
import { HeartbeatGateway } from './heartbeat.gateway'
import { SaltService } from './salt.service'
import { Salt } from './entities/salt.entity'
@@ -38,6 +40,8 @@ import { SessionReplayExportProcessor } from './session-replay-export.processor'
SessionReplayExportService,
SessionReplayExportProcessor,
HeartbeatGateway,
+ AnalyticsReadGuard,
+ PublicProjectCacheInterceptor,
],
exports: [AnalyticsService, BotDetectionService, SaltService],
controllers: [AnalyticsController],
diff --git a/backend/apps/cloud/src/analytics/protection/analytics-read.guard.ts b/backend/apps/cloud/src/analytics/protection/analytics-read.guard.ts
new file mode 100644
index 000000000..0e9dd11bb
--- /dev/null
+++ b/backend/apps/cloud/src/analytics/protection/analytics-read.guard.ts
@@ -0,0 +1,94 @@
+import {
+ CanActivate,
+ ExecutionContext,
+ ForbiddenException,
+ Injectable,
+} from '@nestjs/common'
+import { isbot } from 'isbot'
+
+import { ProjectService } from '../../project/project.service'
+import { checkRateLimit, getIPDetails } from '../../common/utils'
+import { AppLoggerService } from '../../logger/logger.service'
+import { getTrustworthyIp, getSinglePid } from './analytics-read.util'
+
+const READ_RATE_LIMIT = Number(process.env.ANALYTICS_READ_RATE_LIMIT) || 600 // requests
+const READ_RATE_WINDOW = Number(process.env.ANALYTICS_READ_RATE_WINDOW) || 60 // seconds
+
+const BLOCK_DATACENTER_READS =
+ process.env.ANALYTICS_BLOCK_DATACENTER_READS === 'true'
+
+/**
+ * Guards the read (GET) surface of the analytics controller.
+ *
+ * Why this exists: the query endpoints run expensive ClickHouse aggregations,
+ * require no auth for public projects (e.g. /demo), and unlike the ingestion
+ * endpoints - had no rate limiting or bot detection. That made them the cheap
+ * L7 DDoS target behind the CPU spikes.
+ */
+@Injectable()
+export class AnalyticsReadGuard implements CanActivate {
+ constructor(
+ private readonly projectService: ProjectService,
+ private readonly logger: AppLoggerService,
+ ) {}
+
+ async canActivate(context: ExecutionContext): Promise<boolean> {
+ const req = context.switchToHttp().getRequest()
+
+ if (req.method !== 'GET') {
+ return true
+ }
+
+ // API key have their separate limits
+ if (req.headers['x-api-key']) {
+ return true
+ }
+
+ const ip = getTrustworthyIp(req)
+
+ if (ip) {
+ await checkRateLimit(
+ ip,
+ 'analytics-read',
+ READ_RATE_LIMIT,
+ READ_RATE_WINDOW,
+ )
+ }
+
+ try {
+ const pid = getSinglePid(req.query || {})
+ if (!pid) {
+ return true
+ }
+
+ const project = await this.projectService.getRedisProject(pid)
+ if (!project || !project.public) {
+ return true
+ }
+
+ const userAgent = String(req.headers['user-agent'] || '')
+ if (userAgent && isbot(userAgent)) {
+ throw new ForbiddenException(
+ 'Automated traffic is not permitted for this resource',
+ )
+ }
+
+ if (BLOCK_DATACENTER_READS && ip && getIPDetails(ip).isHosting) {
+ throw new ForbiddenException(
+ 'Automated traffic is not permitted for this resource',
+ )
+ }
+ } catch (reason) {
+ if (reason instanceof ForbiddenException) {
+ throw reason
+ }
+ this.logger.warn(
+ `AnalyticsReadGuard soft-failed: ${reason}`,
+ 'AnalyticsReadGuard',
+ )
+ return true
+ }
+
+ return true
+ }
+}
diff --git a/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts b/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts
new file mode 100644
index 000000000..f47c15e97
--- /dev/null
+++ b/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts
@@ -0,0 +1,46 @@
+import _isString from 'lodash/isString'
+
+import { getIPFromHeaders } from '../../common/utils'
+
+export const getTrustworthyIp = (req: {
+ headers: Record<string, unknown>
+ ip?: string
+}): string => {
+ const realIp = req.headers['x-real-ip']
+ if (_isString(realIp) && realIp) {
+ return realIp.trim()
+ }
+ if (Array.isArray(realIp) && _isString(realIp[0]) && realIp[0]) {
+ return realIp[0].trim()
+ }
+
+ return getIPFromHeaders(req.headers) || req.ip || ''
+}
+
+export const getSinglePid = (query: Record<string, unknown>): string | null => {
+ const { pid, pids } = query
+
+ if (_isString(pid) && pid) {
+ return pid
+ }
+
+ if (_isString(pids) && pids) {
+ try {
+ const parsed = JSON.parse(pids)
+ if (
+ Array.isArray(parsed) &&
+ parsed.length === 1 &&
+ _isString(parsed[0])
+ ) {
+ return parsed[0]
+ }
+ } catch {
+ return null
+ }
+ }
+
+ return null
+}
+
+export const getAnalyticsRoute = (path: string): string =>
+ path.replace(/^\/(v1\/)?log\/?/, '').replace(/\/$/, '')
diff --git a/backend/apps/cloud/src/analytics/protection/public-project-cache.interceptor.ts b/backend/apps/cloud/src/analytics/protection/public-project-cache.interceptor.ts
new file mode 100644
index 000000000..2f657220b
--- /dev/null
+++ b/backend/apps/cloud/src/analytics/protection/public-project-cache.interceptor.ts
@@ -0,0 +1,112 @@
+import {
+ CallHandler,
+ ExecutionContext,
+ Injectable,
+ NestInterceptor,
+} from '@nestjs/common'
+import { Observable, of } from 'rxjs'
+import { tap } from 'rxjs/operators'
+
+import { ProjectService } from '../../project/project.service'
+import { AppLoggerService } from '../../logger/logger.service'
+import { redis } from '../../common/constants'
+import { getSinglePid, getAnalyticsRoute } from './analytics-read.util'
+
+const CACHE_TTL = Number(process.env.PUBLIC_PROJECT_CACHE_TTL) || 10 // seconds
+
+const CACHE_PREFIX = 'arc' // analytics read cache
+
+const CACHEABLE_ROUTES = new Set([
+ '', // getData — dashboard overview
+ 'chart',
+ 'birdseye',
+ 'performance',
+ 'performance/chart',
+ 'performance/birdseye',
+ 'sessions',
+ 'profiles',
+ 'errors',
+ 'error-overview',
+ 'funnel',
+ 'funnel-sessions',
+ 'user-flow',
+ 'custom-events',
+ 'keywords',
+ 'gsc-dashboard',
+ 'gsc-details',
+])
+
+/**
+ * Caches read responses for PUBLIC projects in Redis for a short window.
+ */
+@Injectable()
+export class PublicProjectCacheInterceptor implements NestInterceptor {
+ constructor(
+ private readonly projectService: ProjectService,
+ private readonly logger: AppLoggerService,
+ ) {}
+
+ async intercept(
+ context: ExecutionContext,
+ next: CallHandler,
+ ): Promise<Observable<unknown>> {
+ const req = context.switchToHttp().getRequest()
+
+ if (req.method !== 'GET') {
+ return next.handle()
+ }
+
+ const route = getAnalyticsRoute(req.path || '')
+ if (!CACHEABLE_ROUTES.has(route)) {
+ return next.handle()
+ }
+
+ const pid = getSinglePid(req.query || {})
+ if (!pid) {
+ return next.handle()
+ }
+
+ let isPublic = false
+ try {
+ const project = await this.projectService.getRedisProject(pid)
+ isPublic = Boolean(project?.public)
+ } catch {
+ return next.handle()
+ }
+
+ if (!isPublic) {
+ return next.handle()
+ }
+
+ const key = `${CACHE_PREFIX}:${req.originalUrl}`
+
+ try {
+ const cached = await redis.get(key)
+ if (cached !== null) {
+ return of(JSON.parse(cached))
+ }
+ } catch (reason) {
+ this.logger.warn(
+ `PublicProjectCacheInterceptor read failed: ${reason}`,
+ 'PublicProjectCacheInterceptor',
+ )
+ return next.handle()
+ }
+
+ return next.handle().pipe(
+ tap((body) => {
+ if (body === undefined || body === null) {
+ return
+ }
+ redis
+ .set(key, JSON.stringify(body), 'EX', CACHE_TTL)
+ .catch((reason) => {
+ this.logger.warn(
+ `PublicProjectCacheInterceptor write failed: ${reason}`,
+ 'PublicProjectCacheInterceptor',
+ )
+ })
+ }),
+ )
+ }
+}