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!

Ease rate limits for authed users

Blue Mouse84d47f71495f
diff --git a/backend/.env.example b/backend/.env.example
index 868d3e65d..a50c10c95 100644
--- a/backend/.env.example
+++ b/backend/.env.example
@@ -147,3 +147,6 @@ DEMO_DATA_ENABLED=false
# S3 prefix containing demo replay folders, for example demo-replays/1/*.json.gz.
# Every direct replay folder under this prefix is grouped into one demo replay.
DEMO_REPLAY_OBJECT_PREFIX=
+
+# The web app IPs - rate limits are ignored for these IP addresses
+TRUSTED_PROXY_IPS=
diff --git a/backend/apps/cloud/src/analytics/protection/analytics-read.guard.ts b/backend/apps/cloud/src/analytics/protection/analytics-read.guard.ts
index 0e9dd11bb..ba671db91 100644
--- a/backend/apps/cloud/src/analytics/protection/analytics-read.guard.ts
+++ b/backend/apps/cloud/src/analytics/protection/analytics-read.guard.ts
@@ -11,7 +11,17 @@ 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
+// Per-IP read rate limit. OPT-IN: unset env => disabled. This guard runs BEHIND
+// the SSR proxy, so a careless per-IP limit keys on the proxy's IP and lumps
+// every dashboard user into one bucket β€” which is exactly what 429'd everyone.
+// So it's off by default (the front-end nginx already rate-limits per real IP),
+// and even when on it only fires for PUBLIC projects with a resolved REAL client
+// IP (see getTrustworthyIp, which returns '' rather than a proxy IP). Turn on
+// only once the real client IP is confirmed to reach the API (realip /
+// TRUSTED_PROXY_IPS), e.g. ANALYTICS_READ_RATE_LIMIT=120.
+const READ_RATE_LIMIT = process.env.ANALYTICS_READ_RATE_LIMIT
+ ? Number(process.env.ANALYTICS_READ_RATE_LIMIT)
+ : null
const READ_RATE_WINDOW = Number(process.env.ANALYTICS_READ_RATE_WINDOW) || 60 // seconds
const BLOCK_DATACENTER_READS =
@@ -20,10 +30,12 @@ const BLOCK_DATACENTER_READS =
/**
* 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.
+ * Scope: PUBLIC projects only (e.g. /demo) β€” the unauthenticated, expensive-to-
+ * serve endpoints an attacker can hit for free. Private dashboard reads (the
+ * bulk of legitimate traffic) are returned untouched before any limiting, so
+ * this guard can never throttle a logged-in user's own dashboard.
+ *
+ * Every check fails OPEN: a protection bug must never take analytics down.
*/
@Injectable()
export class AnalyticsReadGuard implements CanActivate {
@@ -35,53 +47,26 @@ export class AnalyticsReadGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<boolean> {
const req = context.switchToHttp().getRequest()
+ // Only police read (GET) traffic. Ingestion has its own defenses.
if (req.method !== 'GET') {
return true
}
- // API key have their separate limits
+ // API-key requests are metered separately, per plan β€” never touch them here.
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,
- )
- }
-
+ // Resolve the project first. Anything that isn't a single PUBLIC project β€”
+ // including every private dashboard read β€” leaves the guard immediately.
+ let isPublicProject = false
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',
- )
+ if (pid) {
+ const project = await this.projectService.getRedisProject(pid)
+ isPublicProject = Boolean(project?.public)
}
} catch (reason) {
- if (reason instanceof ForbiddenException) {
- throw reason
- }
this.logger.warn(
`AnalyticsReadGuard soft-failed: ${reason}`,
'AnalyticsReadGuard',
@@ -89,6 +74,49 @@ export class AnalyticsReadGuard implements CanActivate {
return true
}
+ if (!isPublicProject) {
+ return true
+ }
+
+ // --- Public-project protections only below this line ---
+
+ // Bot user-agents never legitimately view a public dashboard.
+ const userAgent = String(req.headers['user-agent'] || '')
+ if (userAgent && isbot(userAgent)) {
+ throw new ForbiddenException(
+ 'Automated traffic is not permitted for this resource',
+ )
+ }
+
+ // The real visitor IP, or '' when only a proxy IP is visible (so we never
+ // rate-limit or geo-check the shared proxy address).
+ const ip = getTrustworthyIp(req)
+
+ if (BLOCK_DATACENTER_READS && ip) {
+ try {
+ if (getIPDetails(ip).isHosting) {
+ throw new ForbiddenException(
+ 'Automated traffic is not permitted for this resource',
+ )
+ }
+ } catch (reason) {
+ if (reason instanceof ForbiddenException) {
+ throw reason
+ }
+ // GeoIP lookup failed β€” ignore, fail open.
+ }
+ }
+
+ // Opt-in per-IP limit, public projects only, real client IP only.
+ if (READ_RATE_LIMIT && ip) {
+ await checkRateLimit(
+ ip,
+ 'analytics-read',
+ READ_RATE_LIMIT,
+ READ_RATE_WINDOW,
+ )
+ }
+
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
index 780eaffd0..9564ea7f8 100644
--- a/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts
+++ b/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts
@@ -41,13 +41,13 @@ export const getTrustworthyIp = (req: {
// the proxy hop for proxied traffic.
const connectionIp = firstHeaderValue(req.headers['x-real-ip'])
- // Behind a trusted proxy: prefer the forwarded real visitor IP. This is the
- // app-level equivalent of nginx realip and works even when realip isn't set up.
+ // Behind a trusted proxy: the real visitor IP is in X-Client-IP-Address (the
+ // app-level equivalent of nginx realip; works even when realip isn't set up).
+ // If it's missing, return '' β€” NEVER fall back to the proxy IP, or a per-IP
+ // limit would key on the shared proxy address and lump every user into one
+ // bucket. An empty result tells callers "real client unknown β†’ don't limit".
if (connectionIp && TRUSTED_PROXY_IPS.has(connectionIp)) {
- const forwarded = firstHeaderValue(req.headers['x-client-ip-address'])
- if (forwarded) {
- return forwarded
- }
+ return firstHeaderValue(req.headers['x-client-ip-address'])
}
if (connectionIp) {