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!

private projects rate limits

Blue Moused75c6b5e07e3
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 ba671db91..dd760bc8b 100644
--- a/backend/apps/cloud/src/analytics/protection/analytics-read.guard.ts
+++ b/backend/apps/cloud/src/analytics/protection/analytics-read.guard.ts
@@ -11,32 +11,13 @@ import { checkRateLimit, getIPDetails } from '../../common/utils'
import { AppLoggerService } from '../../logger/logger.service'
import { getTrustworthyIp, getSinglePid } from './analytics-read.util'
-// 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 READ_RATE_LIMIT = 300 // requests per rate window
+const AUTHED_READ_RATE_LIMIT = 3000 // requests per rate window
+const 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.
- *
- * 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 {
constructor(
@@ -47,18 +28,30 @@ 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.
+ // Only police read (GET) traffic
if (req.method !== 'GET') {
return true
}
- // API-key requests are metered separately, per plan β€” never touch them here.
+ // API-key requests are metered separately, per plan
if (req.headers['x-api-key']) {
return true
}
- // Resolve the project first. Anything that isn't a single PUBLIC project β€”
- // including every private dashboard read β€” leaves the guard immediately.
+ // Logged-in requests - limiting by userId on a more generous limit
+ const uid = req.user?.sub || req.user?.id || null
+ if (uid) {
+ if (AUTHED_READ_RATE_LIMIT > 0) {
+ await checkRateLimit(
+ String(uid),
+ 'analytics-read-user',
+ AUTHED_READ_RATE_LIMIT,
+ READ_RATE_WINDOW,
+ )
+ }
+ return true
+ }
+
let isPublicProject = false
try {
const pid = getSinglePid(req.query || {})
@@ -80,7 +73,7 @@ export class AnalyticsReadGuard implements CanActivate {
// --- Public-project protections only below this line ---
- // Bot user-agents never legitimately view a public dashboard.
+ // Bot user-agents never legitimately view a public dashboard
const userAgent = String(req.headers['user-agent'] || '')
if (userAgent && isbot(userAgent)) {
throw new ForbiddenException(
@@ -89,7 +82,7 @@ export class AnalyticsReadGuard implements CanActivate {
}
// The real visitor IP, or '' when only a proxy IP is visible (so we never
- // rate-limit or geo-check the shared proxy address).
+ // rate-limit or geo-check the shared proxy address)
const ip = getTrustworthyIp(req)
if (BLOCK_DATACENTER_READS && ip) {
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 9564ea7f8..2b3fe7f0d 100644
--- a/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts
+++ b/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts
@@ -2,19 +2,6 @@ import _isString from 'lodash/isString'
import { getIPFromHeaders } from '../../common/utils'
-// IPs of our OWN reverse proxies / SSR front-end (e.g. the web app egress IP).
-// Comma-separated, e.g. TRUSTED_PROXY_IPS=167.235.69.100,167.235.69.101.
-//
-// Why this matters: the web app's SSR loaders call this API on behalf of end
-// users, so without this every proxied request looks like it comes from the
-// single web-app IP β€” and a per-IP limit then lumps the whole dashboard (plus
-// any attack proxied through it) into ONE bucket and 429s everyone. When a
-// request arrives FROM a trusted proxy, the real visitor IP is in
-// X-Client-IP-Address (set by web/app/api/api.server.ts), so we key on that.
-//
-// Trusting the header ONLY from these IPs is what makes it safe: a direct
-// attacker hitting the API cannot spoof X-Client-IP-Address to dodge limits,
-// because their connection IP isn't in this set.
const TRUSTED_PROXY_IPS = new Set(
(process.env.TRUSTED_PROXY_IPS || '')
.split(',')
@@ -36,16 +23,8 @@ export const getTrustworthyIp = (req: {
headers: Record<string, unknown>
ip?: string
}): string => {
- // The real TCP peer as nginx sees it (proxy.conf sets X-Real-IP = $remote_addr).
- // If nginx realip is configured this is already the real visitor; if not, it's
- // the proxy hop for proxied traffic.
const connectionIp = firstHeaderValue(req.headers['x-real-ip'])
- // 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)) {
return firstHeaderValue(req.headers['x-client-ip-address'])
}
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
index 2f657220b..d15f9bd5a 100644
--- a/backend/apps/cloud/src/analytics/protection/public-project-cache.interceptor.ts
+++ b/backend/apps/cloud/src/analytics/protection/public-project-cache.interceptor.ts
@@ -17,7 +17,7 @@ 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
+ '', // getData - dashboard overview
'chart',
'birdseye',
'performance',