🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Revert "Ratelimits for cloud (#248)"
Bill Yang8484290e284f
diff --git a/Caddyfile b/Caddyfileindex 6760c2c57..4141123f9 100644--- a/Caddyfile+++ b/Caddyfile@@ -5,24 +5,12 @@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 dfd5a0239..04a0b69af 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=${CLICKHOUSE_DB:-analytics}- - CLICKHOUSE_PASSWORD=${CLICKHOUSE_PASSWORD:-frog}+ - CLICKHOUSE_DB=analytics+ - 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 0cbb04ad8..62e0080f6 100644--- a/server/package-lock.json+++ b/server/package-lock.json@@ -12,7 +12,6 @@"@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",@@ -1111,26 +1110,6 @@"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 12cccde7b..a98732a13 100644--- a/server/package.json+++ b/server/package.json@@ -20,7 +20,6 @@"@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 fdc25bdd3..c0e9e77dc 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -1,5 +1,4 @@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";@@ -8,8 +7,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 { deleteFunnel } from "./api/analytics/deleteFunnel.js";import { deleteGoal } from "./api/analytics/deleteGoal.js";+import { deleteFunnel } from "./api/analytics/deleteFunnel.js";import { getEventNames } from "./api/analytics/getEventNames.js";import { getEventProperties } from "./api/analytics/getEventProperties.js";import { getEvents } from "./api/analytics/getEvents.js";@@ -56,8 +55,9 @@ 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 { addUserToOrganization } from "./api/user/addUserToOrganization.js";+import { handleWebhook } from "./api/stripe/webhook.js";import { IS_CLOUD } from "./lib/const.js";+import { addUserToOrganization } from "./api/user/addUserToOrganization.js";const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);@@ -72,22 +72,6 @@ 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))) {@@ -265,11 +249,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 4192164ac..e3c46fcd6 100644--- a/server/src/tracker/trackEvent.ts+++ b/server/src/tracker/trackEvent.ts@@ -1,12 +1,5 @@-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,@@ -14,6 +7,13 @@ 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,7 +164,8 @@ async function validateOrigin(siteId: string, requestOrigin?: string) {if (!siteDomain) {return {- success: true,+ success: false,+ error: "Site not found or has no registered domain",};}@@ -225,21 +226,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. I removed this because people can just spoof the header- // const originValidation = await validateOrigin(- // validatedPayload.site_id,- // request.headers.origin as string- // );+ // Validate that the request is coming from the expected origin+ 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)) {