📈 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
IP forwarding
Blue Mouse7907373fa801
diff --git a/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts b/backend/apps/cloud/src/analytics/protection/analytics-read.util.tsindex f47c15e97..780eaffd0 100644--- a/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts+++ b/backend/apps/cloud/src/analytics/protection/analytics-read.util.ts@@ -2,16 +2,56 @@ 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(',')+ .map((entry) => entry.trim())+ .filter(Boolean),+)++const firstHeaderValue = (value: unknown): string => {+ if (_isString(value) && value) {+ return value.trim()+ }+ if (Array.isArray(value) && _isString(value[0]) && value[0]) {+ return value[0].trim()+ }+ return ''+}+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()+ // 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: prefer the forwarded real visitor IP. This is the+ // app-level equivalent of nginx realip and works even when realip isn't set up.+ if (connectionIp && TRUSTED_PROXY_IPS.has(connectionIp)) {+ const forwarded = firstHeaderValue(req.headers['x-client-ip-address'])+ if (forwarded) {+ return forwarded+ }}- if (Array.isArray(realIp) && _isString(realIp[0]) && realIp[0]) {- return realIp[0].trim()++ if (connectionIp) {+ return connectionIp}return getIPFromHeaders(req.headers) || req.ip || ''diff --git a/web/app/utils/analyticsProxy.server.ts b/web/app/utils/analyticsProxy.server.tsindex 917311a50..635c0f3f4 100644--- a/web/app/utils/analyticsProxy.server.ts+++ b/web/app/utils/analyticsProxy.server.ts@@ -1,3 +1,5 @@+import { getClientIP } from '~/api/api.server'+const SWETRIX_API_BASE_URL = 'https://api.swetrix.com/log'const PROXY_BASE_PATH = '/_internal_data_inngest_proxy'const SWETRIX_LOG_PREFIX = '/log'@@ -48,6 +50,16 @@ export const proxySwetrixAnalyticsRequest = async (request: Request) => {headers.delete('host')headers.delete('content-length')+ // Forward the real visitor IP, same as serverFetch does for dashboard loaders.+ // Without this the API sees every first-party beacon as the web app's egress+ // IP and lumps them into one rate-limit bucket. getClientIP reads the+ // front-end nginx's X-Real-IP, so a browser can't spoof it; set() overwrites+ // any client-supplied X-Client-IP-Address.+ const clientIP = getClientIP(request)+ if (clientIP) {+ headers.set('X-Client-IP-Address', clientIP)+ }+if (!bodyText) {return jsonError(400, 'Missing request body')}