🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Ratelimits for cloud (#248)
Bill Yang57fd0cfe3655
diff --git a/Caddyfile b/Caddyfileindex 4141123f9..6760c2c57 100644--- a/Caddyfile+++ b/Caddyfile@@ -5,12 +5,24 @@encode zstd gziphandle_path /api/* {+ # Forward the real client IP and other proxy information+ header_up X-Forwarded-For {remote_host}+ header_up X-Real-IP {remote_host}+ header_up X-Forwarded-Proto {scheme}+ header_up X-Forwarded-Host {host}+reverse_proxy backend:3001}# Proxy all other requests to the client servicehandle {+ # Forward the real client IP and other proxy information+ header_up X-Forwarded-For {remote_host}+ header_up X-Real-IP {remote_host}+ header_up X-Forwarded-Proto {scheme}+ header_up X-Forwarded-Host {host}+reverse_proxy client:3002}diff --git a/docker-compose.dev.yml b/docker-compose.dev.ymlindex 04a0b69af..dfd5a0239 100644--- a/docker-compose.dev.yml+++ b/docker-compose.dev.yml@@ -61,8 +61,8 @@ services:environment:- NODE_ENV=production- CLICKHOUSE_HOST=http://clickhouse:8123- - CLICKHOUSE_DB=analytics- - CLICKHOUSE_PASSWORD=frog+ - CLICKHOUSE_DB=${CLICKHOUSE_DB:-analytics}+ - CLICKHOUSE_PASSWORD=${CLICKHOUSE_PASSWORD:-frog}- POSTGRES_HOST=postgres- POSTGRES_PORT=5432- POSTGRES_DB=${POSTGRES_DB:-analytics}diff --git a/server/package-lock.json b/server/package-lock.jsonindex 62e0080f6..0cbb04ad8 100644--- a/server/package-lock.json+++ b/server/package-lock.json@@ -12,6 +12,7 @@"@clickhouse/client": "1.11.1","@fastify/cors": "11.0.1","@fastify/one-line-logger": "2.0.2",+ "@fastify/rate-limit": "^10.2.2","@fastify/static": "8.1.1","@maxmind/geoip2-node": "6.1.0","better-auth": "1.2.7",@@ -1110,6 +1111,26 @@"ipaddr.js": "^2.1.0"}},+ "node_modules/@fastify/rate-limit": {+ "version": "10.2.2",+ "resolved": "https://registry.npmjs.org/@fastify/rate-limit/-/rate-limit-10.2.2.tgz",+ "integrity": "sha512-45vXZImiYthKlMohF4XoHXYiBXCyRYY+zmtjLZuQrGraW0Zj9hYPYNOIa47012+5A65M0KJQxIVbzYCNP90hcg==",+ "funding": [+ {+ "type": "github",+ "url": "https://github.com/sponsors/fastify"+ },+ {+ "type": "opencollective",+ "url": "https://opencollective.com/fastify"+ }+ ],+ "dependencies": {+ "@lukeed/ms": "^2.0.2",+ "fastify-plugin": "^5.0.0",+ "toad-cache": "^3.7.0"+ }+ },"node_modules/@fastify/send": {"version": "3.3.1","resolved": "https://registry.npmjs.org/@fastify/send/-/send-3.3.1.tgz",diff --git a/server/package.json b/server/package.jsonindex a98732a13..12cccde7b 100644--- a/server/package.json+++ b/server/package.json@@ -20,6 +20,7 @@"@clickhouse/client": "1.11.1","@fastify/cors": "11.0.1","@fastify/one-line-logger": "2.0.2",+ "@fastify/rate-limit": "^10.2.2","@fastify/static": "8.1.1","@maxmind/geoip2-node": "6.1.0","better-auth": "1.2.7",diff --git a/server/src/index.ts b/server/src/index.tsindex c0e9e77dc..fdc25bdd3 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -1,4 +1,5 @@import cors from "@fastify/cors";+import rateLimit from "@fastify/rate-limit";import fastifyStatic from "@fastify/static";import { toNodeHandler } from "better-auth/node";import Fastify from "fastify";@@ -7,8 +8,8 @@ import { Headers, HeadersInit } from "undici";import { fileURLToPath } from "url";import { createFunnel } from "./api/analytics/createFunnel.js";import { createGoal } from "./api/analytics/createGoal.js";-import { deleteGoal } from "./api/analytics/deleteGoal.js";import { deleteFunnel } from "./api/analytics/deleteFunnel.js";+import { deleteGoal } from "./api/analytics/deleteGoal.js";import { getEventNames } from "./api/analytics/getEventNames.js";import { getEventProperties } from "./api/analytics/getEventProperties.js";import { getEvents } from "./api/analytics/getEvents.js";@@ -55,9 +56,8 @@ import { extractSiteId, isSitePublic, normalizeOrigin } from "./utils.js";import { createCheckoutSession } from "./api/stripe/createCheckoutSession.js";import { createPortalSession } from "./api/stripe/createPortalSession.js";import { getSubscription } from "./api/stripe/getSubscription.js";-import { handleWebhook } from "./api/stripe/webhook.js";-import { IS_CLOUD } from "./lib/const.js";import { addUserToOrganization } from "./api/user/addUserToOrganization.js";+import { IS_CLOUD } from "./lib/const.js";const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);@@ -72,6 +72,22 @@ const server = Fastify({trustProxy: true,});+if (IS_CLOUD) {+ server.register(rateLimit, {+ max: 100,+ timeWindow: "1 minute",+ allowList: (req) => {+ const url = req.raw.url || "";+ return (+ url.startsWith("/api/auth") ||+ url.startsWith("/auth") ||+ req.ip === "127.0.0.1" ||+ req.ip === "::1"+ );+ },+ });+}+server.register(cors, {origin: (origin, callback) => {if (!origin || allowList.includes(normalizeOrigin(origin))) {@@ -249,11 +265,11 @@ if (IS_CLOUD) {server.post("/stripe/create-checkout-session", createCheckoutSession);server.post("/stripe/create-portal-session", createPortalSession);server.get("/stripe/subscription", getSubscription);- server.post(- "/api/stripe/webhook",- { config: { rawBody: true } },- handleWebhook- ); // Use rawBody parser config for webhook+ // server.post(+ // "/api/stripe/webhook",+ // { config: { rawBody: true } },+ // handleWebhook+ // ); // Use rawBody parser config for webhook}server.post("/track", trackEvent);diff --git a/server/src/tracker/trackEvent.ts b/server/src/tracker/trackEvent.tsindex e3c46fcd6..4192164ac 100644--- a/server/src/tracker/trackEvent.ts+++ b/server/src/tracker/trackEvent.ts@@ -1,5 +1,12 @@+import { eq } from "drizzle-orm";import { FastifyReply, FastifyRequest } from "fastify";import { z, ZodError } from "zod";+import { db } from "../db/postgres/postgres.js";+import { activeSessions } from "../db/postgres/schema.js";+import { siteConfig } from "../lib/siteConfig.js";+import { getDeviceType } from "../utils.js";+import { DISABLE_ORIGIN_CHECK } from "./const.js";+import { pageviewQueue } from "./pageviewQueue.js";import {clearSelfReferrer,createBasePayload,@@ -7,13 +14,6 @@ import {isSiteOverLimit,TotalTrackingPayload,} from "./trackingUtils.js";-import { db } from "../db/postgres/postgres.js";-import { activeSessions } from "../db/postgres/schema.js";-import { eq } from "drizzle-orm";-import { getDeviceType } from "../utils.js";-import { pageviewQueue } from "./pageviewQueue.js";-import { siteConfig } from "../lib/siteConfig.js";-import { DISABLE_ORIGIN_CHECK } from "./const.js";// Define Zod schema for validationexport const trackingPayloadSchema = z.discriminatedUnion("type", [@@ -164,8 +164,7 @@ async function validateOrigin(siteId: string, requestOrigin?: string) {if (!siteDomain) {return {- success: false,- error: "Site not found or has no registered domain",+ success: true,};}@@ -226,21 +225,21 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {// Use validated dataconst validatedPayload = validationResult.data;- // Validate that the request is coming from the expected origin- const originValidation = await validateOrigin(- validatedPayload.site_id,- request.headers.origin as string- );+ // Validate that the request is coming from the expected origin. I removed this because people can just spoof the header+ // const originValidation = await validateOrigin(+ // validatedPayload.site_id,+ // request.headers.origin as string+ // );- if (!originValidation.success) {- console.warn(- `[Tracking] Request rejected for site ${validatedPayload.site_id}: ${originValidation.error}`- );- return reply.status(403).send({- success: false,- error: originValidation.error,- });- }+ // if (!originValidation.success) {+ // console.warn(+ // `[Tracking] Request rejected for site ${validatedPayload.site_id}: ${originValidation.error}`+ // );+ // return reply.status(403).send({+ // success: false,+ // error: originValidation.error,+ // });+ // }// Check if the site has exceeded its monthly limitif (isSiteOverLimit(validatedPayload.site_id)) {