🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Exclude ips (#527)
Bill Yangd8dc8ae2a66d
diff --git a/client/package-lock.json b/client/package-lock.jsonindex 1987197f1..fc19a03a3 100644--- a/client/package-lock.json+++ b/client/package-lock.json@@ -51,6 +51,7 @@"date-fns": "^4.1.0","framer-motion": "12.11.0","geojson": "0.5.0",+ "ip-address": "^10.0.1","jotai": "2.12.4","ldrs": "1.1.7","leaflet": "1.9.4",@@ -8331,6 +8332,15 @@"node": ">=12"}},+ "node_modules/ip-address": {+ "version": "10.0.1",+ "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.0.1.tgz",+ "integrity": "sha512-NWv9YLW4PoW2B7xtzaS3NCot75m6nK7Icdv0o3lfMceJVRfSoQwqD4wEH5rLwoKJwUiZ/rfpiVBhnaF0FK4HoA==",+ "license": "MIT",+ "engines": {+ "node": ">= 12"+ }+ },"node_modules/is-arrayish": {"version": "0.2.1","resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz",diff --git a/client/package.json b/client/package.jsonindex 6447674c8..ef7cdf5d0 100644--- a/client/package.json+++ b/client/package.json@@ -53,6 +53,7 @@"date-fns": "^4.1.0","framer-motion": "12.11.0","geojson": "0.5.0",+ "ip-address": "^10.0.1","jotai": "2.12.4","ldrs": "1.1.7","leaflet": "1.9.4",diff --git a/client/src/api/admin/excludedIPs.ts b/client/src/api/admin/excludedIPs.tsnew file mode 100644index 000000000..530b10a0e--- /dev/null+++ b/client/src/api/admin/excludedIPs.ts@@ -0,0 +1,70 @@+import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";+import { toast } from "sonner";+import { authedFetch } from "../utils";++export interface ExcludedIPsResponse {+ success: boolean;+ excludedIPs: string[];+ error?: string;+}++export interface UpdateExcludedIPsRequest {+ siteId: number;+ excludedIPs: string[];+}++export interface UpdateExcludedIPsResponse {+ success: boolean;+ message: string;+ excludedIPs: string[];+ error?: string;+ details?: string[];+}++// Fetch excluded IPs for a site+export const fetchExcludedIPs = async (siteId: string): Promise<ExcludedIPsResponse> => {+ return await authedFetch<ExcludedIPsResponse>(`/site/${siteId}/excluded-ips`);+};++// Update excluded IPs for a site+export const updateExcludedIPs = async (siteId: number, excludedIPs: string[]): Promise<UpdateExcludedIPsResponse> => {+ return await authedFetch<UpdateExcludedIPsResponse>(+ `/site/${siteId}/excluded-ips`,+ undefined,+ {+ method: "POST",+ data: {+ siteId: siteId.toString(),+ excludedIPs,+ },+ }+ );+};++// Hook to fetch excluded IPs+export const useGetExcludedIPs = (siteId: number) => {+ return useQuery({+ queryKey: ["excludedIPs", siteId],+ queryFn: () => fetchExcludedIPs(siteId.toString()),+ enabled: !!siteId,+ });+};++// Hook to update excluded IPs+export const useUpdateExcludedIPs = () => {+ const queryClient = useQueryClient();++ return useMutation<UpdateExcludedIPsResponse, Error, UpdateExcludedIPsRequest>({+ mutationFn: ({ siteId, excludedIPs }: UpdateExcludedIPsRequest) => updateExcludedIPs(siteId, excludedIPs),+ onSuccess: (_: UpdateExcludedIPsResponse, variables: UpdateExcludedIPsRequest) => {+ toast.success("Excluded IPs updated successfully");+ // Invalidate and refetch excluded IPs data+ queryClient.invalidateQueries({+ queryKey: ["excludedIPs", variables.siteId],+ });+ },+ onError: (error: Error) => {+ toast.error(error.message || "Failed to update excluded IPs");+ },+ });+};diff --git a/client/src/components/SiteSettings/IPExclusionManager.tsx b/client/src/components/SiteSettings/IPExclusionManager.tsxnew file mode 100644index 000000000..2dd6cb522--- /dev/null+++ b/client/src/components/SiteSettings/IPExclusionManager.tsx@@ -0,0 +1,176 @@+"use client";++import { Minus, Plus } from "lucide-react";+import React, { useState } from "react";+import { toast } from "sonner";++import { Button } from "@/components/ui/button";+import { Input } from "@/components/ui/input";+import { Label } from "@/components/ui/label";++import { useGetExcludedIPs, useUpdateExcludedIPs } from "@/api/admin/excludedIPs";+import { validateIPPattern } from "@/lib/ipValidation";++interface IPExclusionManagerProps {+ siteId: number;+ disabled?: boolean;+}++export function IPExclusionManager({ siteId, disabled = false }: IPExclusionManagerProps) {+ const { data: excludedIPsData, isLoading } = useGetExcludedIPs(siteId);+ const updateExcludedIPsMutation = useUpdateExcludedIPs();++ const [ipList, setIpList] = useState<string[]>([]);+ const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);++ // Initialize IP list when data is loaded+ React.useEffect(() => {+ if (excludedIPsData?.excludedIPs) {+ setIpList(excludedIPsData.excludedIPs.length > 0 ? excludedIPsData.excludedIPs : [""]);+ setHasUnsavedChanges(false);+ } else if (!isLoading) {+ setIpList([""]);+ }+ }, [excludedIPsData, isLoading]);++ const addIPField = () => {+ setIpList([...ipList, ""]);+ setHasUnsavedChanges(true);+ };++ const removeIPField = (index: number) => {+ if (ipList.length > 1) {+ const newList = ipList.filter((_, i) => i !== index);+ setIpList(newList);+ setHasUnsavedChanges(true);+ }+ };++ const updateIPField = (index: number, value: string) => {+ const newList = [...ipList];+ newList[index] = value;+ setIpList(newList);+ setHasUnsavedChanges(true);+ };++ const handleSave = async () => {+ // Filter out empty entries and validate+ const filteredIPs = ipList.filter((ip) => ip.trim() !== "");+ const invalidIPs: string[] = [];+ const validationErrors: string[] = [];++ filteredIPs.forEach((ip) => {+ const validation = validateIPPattern(ip);+ if (!validation.valid) {+ invalidIPs.push(ip);+ if (validation.error) {+ validationErrors.push(`${ip}: ${validation.error}`);+ }+ }+ });++ if (invalidIPs.length > 0) {+ const errorMessage =+ validationErrors.length > 0+ ? `Invalid IP patterns:\n${validationErrors.join("\n")}`+ : `Invalid IP patterns: ${invalidIPs.join(", ")}`;+ toast.error(errorMessage);+ return;+ }++ try {+ await updateExcludedIPsMutation.mutateAsync({+ siteId,+ excludedIPs: filteredIPs,+ });+ setHasUnsavedChanges(false);+ } catch (error) {+ // Error handling is done in the mutation+ }+ };++ const handleReset = () => {+ if (excludedIPsData?.excludedIPs) {+ setIpList(excludedIPsData.excludedIPs.length > 0 ? excludedIPsData.excludedIPs : [""]);+ } else {+ setIpList([""]);+ }+ setHasUnsavedChanges(false);+ };++ if (isLoading) {+ return <div className="text-sm text-muted-foreground">Loading IP exclusions...</div>;+ }++ return (+ <div className="space-y-4">+ <div>+ <Label className="text-sm font-medium text-foreground block">IP Exclusions</Label>+ <p className="text-xs text-muted-foreground mt-1">+ Exclude traffic from specific IP addresses or ranges. Supports single IPs (192.168.1.1), CIDR notation+ (192.168.1.0/24), and ranges (192.168.1.1-192.168.1.10).+ </p>+ </div>++ <div className="space-y-2">+ {ipList.map((ip, index) => (+ <div key={index} className="flex items-center space-x-2">+ <Input+ value={ip}+ onChange={(e) => updateIPField(index, e.target.value)}+ placeholder="e.g., 192.168.1.1 or 10.0.0.0/24"+ disabled={disabled}+ className={!validateIPPattern(ip).valid && ip.trim() !== "" ? "border-red-500" : ""}+ />+ {ipList.length > 1 && (+ <Button+ type="button"+ variant="outline"+ size="icon"+ onClick={() => removeIPField(index)}+ disabled={disabled}+ className="flex-shrink-0"+ >+ <Minus className="h-4 w-4" />+ </Button>+ )}+ </div>+ ))}+ </div>++ <div className="flex items-center space-x-2">+ <Button+ type="button"+ variant="outline"+ size="sm"+ onClick={addIPField}+ disabled={disabled || ipList.length >= 100}+ className="flex items-center space-x-1"+ >+ <Plus className="h-4 w-4" />+ <span>Add IP</span>+ </Button>++ {ipList.length >= 100 && (+ <span className="text-xs text-muted-foreground">Maximum 100 IP exclusions allowed</span>+ )}+ </div>++ {hasUnsavedChanges && (+ <div className="flex items-center space-x-2 pt-2">+ <Button onClick={handleSave} disabled={disabled || updateExcludedIPsMutation.isPending} size="sm">+ {updateExcludedIPsMutation.isPending ? "Saving..." : "Save Changes"}+ </Button>+ <Button+ variant="outline"+ onClick={handleReset}+ disabled={disabled || updateExcludedIPsMutation.isPending}+ size="sm"+ >+ Reset+ </Button>+ </div>+ )}+ </div>+ );+}diff --git a/client/src/components/SiteSettings/SiteConfiguration.tsx b/client/src/components/SiteSettings/SiteConfiguration.tsxindex 85688a63e..0360844cc 100644--- a/client/src/components/SiteSettings/SiteConfiguration.tsx+++ b/client/src/components/SiteSettings/SiteConfiguration.tsx@@ -31,6 +31,7 @@ import {useGetSitesFromOrg,} from "@/api/admin/sites";import { normalizeDomain } from "@/lib/utils";+import { IPExclusionManager } from "./IPExclusionManager";interface SiteConfigurationProps {siteMetadata: SiteResponse;@@ -214,6 +215,9 @@ export function SiteConfiguration({/></div>+ {/* IP Exclusions Section */}+ <IPExclusionManager siteId={siteMetadata.siteId} disabled={disabled} />+{/* Domain Settings Section */}<div className="space-y-3"><div>diff --git a/client/src/lib/ipValidation.ts b/client/src/lib/ipValidation.tsnew file mode 100644index 000000000..8518954c2--- /dev/null+++ b/client/src/lib/ipValidation.ts@@ -0,0 +1,130 @@+/**+ * Client-side IP validation utility using ip-address library+ * This matches the server-side validation logic in server/src/lib/ipUtils.ts+ */++import { Address4, Address6 } from "ip-address";++export interface IPValidationResult {+ valid: boolean;+ error?: string;+}++/**+ * Validate if an IP pattern is valid+ * Supports:+ * - Single IPv4: 192.168.1.1+ * - Single IPv6: 2001:db8::1, ::1, 2001::1+ * - CIDR notation: 192.168.1.0/24, 2001:db8::/32+ * - Range notation: 192.168.1.1-192.168.1.10 (IPv4 only, IPv6 ranges not supported)+ */+export function validateIPPattern(pattern: string): IPValidationResult {+ try {+ const trimmedPattern = pattern.trim();++ if (!trimmedPattern) {+ return { valid: true }; // Empty is valid (will be filtered out)+ }++ // Single IP+ if (!trimmedPattern.includes("/") && !trimmedPattern.includes("-")) {+ return validateSingleIP(trimmedPattern);+ }++ // CIDR notation+ if (trimmedPattern.includes("/")) {+ return validateCIDR(trimmedPattern);+ }++ // Range notation (IPv4 only)+ if (trimmedPattern.includes("-")) {+ return validateRange(trimmedPattern);+ }++ return { valid: false, error: "Unknown IP pattern format" };+ } catch (error) {+ return { valid: false, error: `Validation error: ${error}` };+ }+}++/**+ * Validate a single IP address (IPv4 or IPv6)+ */+function validateSingleIP(ip: string): IPValidationResult {+ // Try IPv4 first+ try {+ new Address4(ip);+ return { valid: true };+ } catch {+ // Try IPv6+ try {+ new Address6(ip);+ return { valid: true };+ } catch {+ return { valid: false, error: "Invalid IP address format" };+ }+ }+}++/**+ * Validate CIDR notation+ */+function validateCIDR(cidr: string): IPValidationResult {+ try {+ // Try IPv4 CIDR first+ try {+ new Address4(cidr);+ return { valid: true };+ } catch {+ // Try IPv6 CIDR+ try {+ new Address6(cidr);+ return { valid: true };+ } catch {+ return { valid: false, error: "Invalid CIDR notation" };+ }+ }+ } catch (error) {+ return { valid: false, error: `Error validating CIDR: ${error}` };+ }+}++/**+ * Validate range notation (IPv4 only)+ *+ * NOTE: Range notation is only supported for IPv4 addresses.+ * IPv6 range notation is not supported due to the complexity of proper+ * numerical comparison of 128-bit addresses. Use CIDR notation instead+ * for IPv6 (e.g., 2001:db8::/32).+ */+function validateRange(range: string): IPValidationResult {+ const parts = range.split("-").map(ip => ip.trim());+ if (parts.length !== 2) {+ return { valid: false, error: "Invalid range format" };+ }++ const [startIP, endIP] = parts;++ if (!startIP || !endIP) {+ return { valid: false, error: "Invalid range format" };+ }++ // Try IPv4 range+ try {+ new Address4(startIP);+ new Address4(endIP);+ return { valid: true };+ } catch {+ // Check if these are IPv6 addresses to provide a better error message+ try {+ new Address6(startIP);+ new Address6(endIP);+ return {+ valid: false,+ error: "IPv6 range notation not supported. Use CIDR notation instead (e.g., 2001:db8::/32)"+ };+ } catch {+ return { valid: false, error: "Invalid IP addresses in range" };+ }+ }+}\ No newline at end of filediff --git a/server/package-lock.json b/server/package-lock.jsonindex 9746bcc9b..cf1a563ca 100644--- a/server/package-lock.json+++ b/server/package-lock.json@@ -24,6 +24,7 @@"drizzle-orm": "0.43.1","fastify": "5.3.3","fastify-better-auth": "1.1.0",+ "ip-address": "^10.0.1","isbot": "^5.1.28","luxon": "3.6.1","node-cache": "^5.1.2",@@ -5811,6 +5812,15 @@"url": "https://opencollective.com/ioredis"}},+ "node_modules/ip-address": {+ "version": "10.0.1",+ "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.0.1.tgz",+ "integrity": "sha512-NWv9YLW4PoW2B7xtzaS3NCot75m6nK7Icdv0o3lfMceJVRfSoQwqD4wEH5rLwoKJwUiZ/rfpiVBhnaF0FK4HoA==",+ "license": "MIT",+ "engines": {+ "node": ">= 12"+ }+ },"node_modules/ipaddr.js": {"version": "2.2.0","resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.2.0.tgz",diff --git a/server/package.json b/server/package.jsonindex 816c57e94..00de631d1 100644--- a/server/package.json+++ b/server/package.json@@ -37,6 +37,7 @@"drizzle-orm": "0.43.1","fastify": "5.3.3","fastify-better-auth": "1.1.0",+ "ip-address": "^10.0.1","isbot": "^5.1.28","luxon": "3.6.1","node-cache": "^5.1.2",diff --git a/server/src/api/admin/getAdminOrganizations.ts b/server/src/api/admin/getAdminOrganizations.tsindex a7980d1ea..df25a1a2b 100644--- a/server/src/api/admin/getAdminOrganizations.ts+++ b/server/src/api/admin/getAdminOrganizations.ts@@ -5,7 +5,7 @@ import { clickhouse } from "../../db/clickhouse/clickhouse.js";import { db } from "../../db/postgres/postgres.js";import { member, sites, user } from "../../db/postgres/schema.js";import { getIsUserAdmin } from "../../lib/auth-utils.js";-import { DEFAULT_EVENT_LIMIT } from "../../lib/const.js";+import { logger } from "../../lib/logger/logger.js";import { getOrganizationSubscriptions } from "../../services/admin/subscriptionService.js";// Define event count result type@@ -14,11 +14,6 @@ interface EventCountResult {total_events: number;}--function getStartOfNextMonth() {- return DateTime.now().startOf("month").plus({ months: 1 }).toJSDate();-}-export interface AdminOrganizationData {id: string;name: string;@@ -134,7 +129,7 @@ export async function getAdminOrganizations(request: FastifyRequest, reply: FastsiteEventMap.set(Number(event.site_id), event.total_events);}} catch (clickhouseError) {- console.warn("ClickHouse query failed, continuing without event counts:", clickhouseError);+ logger.warn(clickhouseError as Error, "ClickHouse query failed, continuing without event counts");}// Create map of organization IDs to their sites with event countsdiff --git a/server/src/api/sessionReplay/recordSessionReplay.ts b/server/src/api/sessionReplay/recordSessionReplay.tsindex a09869108..c3ad1b02c 100644--- a/server/src/api/sessionReplay/recordSessionReplay.ts+++ b/server/src/api/sessionReplay/recordSessionReplay.ts@@ -1,10 +1,12 @@import { FastifyReply, FastifyRequest } from "fastify";import { z } from "zod";+import { siteConfig } from "../../lib/siteConfig.js";import { SessionReplayIngestService } from "../../services/replay/sessionReplayIngestService.js";import { validateApiKey, validateOrigin } from "../../services/shared/requestValidation.js";import { usageService } from "../../services/usageService.js";import { RecordSessionReplayRequest } from "../../types/sessionReplay.js";import { getIpAddress } from "../../utils.js";+import { logger } from "../../lib/logger/logger.js";const recordSessionReplaySchema = z.object({userId: z.string(),@@ -38,7 +40,7 @@ export async function recordSessionReplay(// Check if the site has exceeded its monthly limitif (usageService.isSiteOverLimit(Number(siteId))) {- console.log(`[SessionReplay] Skipping event for site ${siteId} - over monthly limit`);+ logger.info(`[SessionReplay] Skipping event for site ${siteId} - over monthly limit`);return reply.status(200).send("Site over monthly limit, event not tracked");}@@ -49,7 +51,7 @@ export async function recordSessionReplay(// If API key validation failed with an error, reject the requestif (apiKeyValidation.error) {- console.warn(`[SessionReplay] Request rejected for site ${siteId}: ${apiKeyValidation.error}`);+ logger.warn(`[SessionReplay] Request rejected for site ${siteId}: ${apiKeyValidation.error}`);return reply.status(403).send({success: false,error: apiKeyValidation.error,@@ -76,7 +78,7 @@ export async function recordSessionReplay(const originValidation = await validateOrigin(siteId, request.headers.origin as string);if (!originValidation.success) {- console.warn(`[SessionReplay] Request rejected for site ${siteId}: ${originValidation.error}`);+ logger.warn(`[SessionReplay] Request rejected for site ${siteId}: ${originValidation.error}`);return reply.status(403).send({success: false,error: originValidation.error,@@ -84,6 +86,20 @@ export async function recordSessionReplay(}}+ // Make sure the site config is loaded+ await siteConfig.ensureInitialized();++ // Check if the IP should be excluded from tracking+ const requestIP = getIpAddress(request);++ if (siteConfig.isIPExcluded(requestIP, siteId)) {+ logger.info(`[SessionReplay] IP ${requestIP} excluded from tracking for site ${siteId}`);+ return reply.status(200).send({+ success: true,+ message: "Session replay not recorded - IP excluded",+ });+ }+// Extract request metadata for trackingconst userAgent = request.headers["user-agent"] || "";const ipAddress = getIpAddress(request);@@ -103,7 +119,7 @@ export async function recordSessionReplay(if (error instanceof z.ZodError) {return reply.status(400).send({ error: error.errors });}- console.error("Error recording session replay:", error);+ logger.error(error as Error, "Error recording session replay");return reply.status(500).send({ error: "Internal server error" });}}diff --git a/server/src/api/sites/addSite.ts b/server/src/api/sites/addSite.tsindex d9e986d8a..71d476d3c 100644--- a/server/src/api/sites/addSite.ts+++ b/server/src/api/sites/addSite.ts@@ -102,6 +102,8 @@ export async function addSite(domain: newSite[0].domain,blockBots:newSite[0].blockBots === undefined ? true : newSite[0].blockBots,+ excludedIPs: Array.isArray(newSite[0].excludedIPs) ? newSite[0].excludedIPs : [],+ apiKey: newSite[0].apiKey,});return reply.status(201).send(newSite[0]);diff --git a/server/src/api/sites/getSiteExcludedIPs.ts b/server/src/api/sites/getSiteExcludedIPs.tsnew file mode 100644index 000000000..d7aa63d91--- /dev/null+++ b/server/src/api/sites/getSiteExcludedIPs.ts@@ -0,0 +1,74 @@+import { FastifyReply, FastifyRequest } from "fastify";+import { z } from "zod";+import { db } from "../../db/postgres/postgres.js";+import { sites } from "../../db/postgres/schema.js";+import { eq } from "drizzle-orm";+import { getUserHasAccessToSite } from "../../lib/auth-utils.js";++const getSiteExcludedIPsSchema = z.object({+ siteId: z.string().min(1),+});++export async function getSiteExcludedIPs(request: FastifyRequest, reply: FastifyReply) {+ try {+ const validationResult = getSiteExcludedIPsSchema.safeParse(request.params);++ if (!validationResult.success) {+ return reply.status(400).send({+ success: false,+ error: "Invalid site ID",+ details: validationResult.error.flatten(),+ });+ }++ const { siteId } = validationResult.data;+ const numericSiteId = Number(siteId);++ // Validate that siteId is a valid integer+ if (!Number.isInteger(numericSiteId) || isNaN(numericSiteId) || numericSiteId <= 0) {+ return reply.status(400).send({+ success: false,+ error: "Invalid site ID: must be a positive integer",+ });+ }++ // Check if user has access to this site+ const hasAccess = await getUserHasAccessToSite(request, numericSiteId);++ if (!hasAccess) {+ return reply.status(403).send({+ success: false,+ error: "Forbidden: You don't have access to this site",+ });+ }++ // Get the excluded IPs from the database+ const site = await db+ .select({+ excludedIPs: sites.excludedIPs,+ })+ .from(sites)+ .where(eq(sites.siteId, numericSiteId))+ .limit(1);++ if (site.length === 0) {+ return reply.status(404).send({+ success: false,+ error: "Site not found",+ });+ }++ const excludedIPs = Array.isArray(site[0].excludedIPs) ? site[0].excludedIPs : [];++ return reply.send({+ success: true,+ excludedIPs,+ });+ } catch (error) {+ console.error("Error getting excluded IPs:", error);+ return reply.status(500).send({+ success: false,+ error: "Failed to get excluded IPs",+ });+ }+}diff --git a/server/src/api/sites/updateSiteExcludedIPs.ts b/server/src/api/sites/updateSiteExcludedIPs.tsnew file mode 100644index 000000000..f5fe8dc19--- /dev/null+++ b/server/src/api/sites/updateSiteExcludedIPs.ts@@ -0,0 +1,92 @@+import { FastifyReply, FastifyRequest } from "fastify";+import { z } from "zod";+import { db } from "../../db/postgres/postgres.js";+import { sites } from "../../db/postgres/schema.js";+import { siteConfig } from "../../lib/siteConfig.js";+import { validateIPPattern } from "../../lib/ipUtils.js";+import { getUserHasAccessToSite } from "../../lib/auth-utils.js";+import { eq } from "drizzle-orm";++const updateExcludedIPsSchema = z.object({+ siteId: z.string().min(1),+ excludedIPs: z.array(z.string().trim().min(1)).max(100), // Limit to 100 IPs/ranges+});++export async function updateSiteExcludedIPs(request: FastifyRequest, reply: FastifyReply) {+ try {+ const validationResult = updateExcludedIPsSchema.safeParse(request.body);++ if (!validationResult.success) {+ return reply.status(400).send({+ success: false,+ error: "Invalid request data",+ details: validationResult.error.flatten(),+ });+ }++ const { siteId, excludedIPs } = validationResult.data;+ const numericSiteId = Number(siteId);++ // Validate that siteId is a valid integer+ if (!Number.isInteger(numericSiteId) || isNaN(numericSiteId) || numericSiteId <= 0) {+ return reply.status(400).send({+ success: false,+ error: "Invalid site ID: must be a positive integer",+ });+ }++ // Check if user has access to this site+ const hasAccess = await getUserHasAccessToSite(request, numericSiteId);++ if (!hasAccess) {+ return reply.status(403).send({+ success: false,+ error: "Forbidden: You don't have access to this site",+ });+ }++ // Validate each IP pattern+ const validationErrors: string[] = [];+ for (const ip of excludedIPs) {+ const validation = validateIPPattern(ip);+ if (!validation.valid) {+ validationErrors.push(`${ip}: ${validation.error}`);+ }+ }++ if (validationErrors.length > 0) {+ return reply.status(400).send({+ success: false,+ error: "Invalid IP patterns",+ details: validationErrors,+ });+ }++ // Update the database and cache atomically+ await db.transaction(async (tx) => {+ // Update the database+ await tx+ .update(sites)+ .set({+ excludedIPs: excludedIPs,+ updatedAt: new Date().toISOString(),+ })+ .where(eq(sites.siteId, numericSiteId));++ // Update the cache - if this fails, the transaction will rollback+ siteConfig.updateSiteExcludedIPs(numericSiteId, excludedIPs);+ });++ return reply.send({+ success: true,+ message: "Excluded IPs updated successfully",+ excludedIPs,+ });+ } catch (error) {+ console.error("Error updating excluded IPs:", error);+ return reply.status(500).send({+ success: false,+ error: "Failed to update excluded IPs",+ });+ }+}diff --git a/server/src/db/geolocation/geolocation.ts b/server/src/db/geolocation/geolocation.tsindex 49077b67e..881862e61 100644--- a/server/src/db/geolocation/geolocation.ts+++ b/server/src/db/geolocation/geolocation.ts@@ -2,6 +2,7 @@ import type { City, SubdivisionsRecord } from "@maxmind/geoip2-node";import { Reader } from "@maxmind/geoip2-node";import { readFile } from "fs/promises";import path from "path";+import { logger } from "../../lib/logger/logger.js";// Adjust path to find the database relative to project rootconst dbPath = path.join(process.cwd(), "GeoLite2-City.mmdb");@@ -27,7 +28,7 @@ interface ExtendedReader extends Reader {async function loadDatabase(dbPath: string) {const dbBuffer = await readFile(dbPath);reader = Reader.openBuffer(dbBuffer);- console.log("GeoIP database loaded successfully");+ logger.info("GeoIP database loaded successfully");}await loadDatabase(dbPath);diff --git a/server/src/db/postgres/schema.ts b/server/src/db/postgres/schema.tsindex c5939d09d..375362fcd 100644--- a/server/src/db/postgres/schema.ts+++ b/server/src/db/postgres/schema.ts@@ -63,6 +63,7 @@ export const sites = pgTable(public: boolean().default(false),saltUserIds: boolean().default(false),blockBots: boolean().default(true).notNull(),+ excludedIPs: jsonb("excluded_ips").default([]), // Array of IP addresses/ranges to excludeapiKey: text("api_key"), // Format: rb_{32_hex_chars} = 35 chars total},(table) => [diff --git a/server/src/index.ts b/server/src/index.tsindex 07f74d3da..9dfd12154 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -51,10 +51,12 @@ import { changeSiteSalt } from "./api/sites/changeSiteSalt.js";import { deleteSite } from "./api/sites/deleteSite.js";import { getSite } from "./api/sites/getSite.js";import { getSiteApiConfig } from "./api/sites/getSiteApiConfig.js";+import { getSiteExcludedIPs } from "./api/sites/getSiteExcludedIPs.js";import { getSiteHasData } from "./api/sites/getSiteHasData.js";import { getSiteIsPublic } from "./api/sites/getSiteIsPublic.js";import { getSitesFromOrg } from "./api/sites/getSitesFromOrg.js";import { updateSiteApiConfig } from "./api/sites/updateSiteApiConfig.js";+import { updateSiteExcludedIPs } from "./api/sites/updateSiteExcludedIPs.js";import { createCheckoutSession } from "./api/stripe/createCheckoutSession.js";import { createPortalSession } from "./api/stripe/createPortalSession.js";import { getSubscription } from "./api/stripe/getSubscription.js";@@ -115,23 +117,25 @@ const server = Fastify({level: process.env.LOG_LEVEL || "info",options: {colorize: true,- translateTime: "SYS:standard",- ignore: "pid,hostname",+ singleLine: true,+ translateTime: "HH:MM:ss",+ ignore: "pid,hostname,name",destination: 1, // stdout},},],}: process.env.NODE_ENV === "development"- ? {- target: "pino-pretty",- options: {- colorize: true,- translateTime: "SYS:standard",- ignore: "pid,hostname",- },- }- : undefined, // Production without Axiom - plain JSON to stdout+ ? {+ target: "pino-pretty",+ options: {+ colorize: true,+ singleLine: true,+ translateTime: "HH:MM:ss",+ ignore: "pid,hostname,name",+ },+ }+ : undefined, // Production without Axiom - plain JSON to stdoutserializers: {req(request) {return {@@ -155,7 +159,7 @@ const server = Fastify({});server.register(cors, {- origin: (origin, callback) => {+ origin: (_origin, callback) => {callback(null, true);// if (!origin || allowList.includes(normalizeOrigin(origin))) {@@ -353,6 +357,8 @@ server.get("/api/get-sites-from-org/:organizationId", getSitesFromOrg);server.get("/api/get-site/:id", getSite);server.get("/api/site/:siteId/api-config", getSiteApiConfig);server.post("/api/site/:siteId/api-config", updateSiteApiConfig);+server.get("/api/site/:siteId/excluded-ips", getSiteExcludedIPs);+server.post("/api/site/:siteId/excluded-ips", updateSiteExcludedIPs);server.get("/api/list-organization-members/:organizationId", listOrganizationMembers);server.get("/api/user/organizations", getUserOrganizations);server.post("/api/add-user-to-organization", addUserToOrganization);@@ -413,15 +419,15 @@ const start = async () => {}// Initialize uptime monitoring service in the background (non-blocking)- uptimeService- .initialize()- .then(() => {- server.log.info("Uptime monitoring service initialized successfully");- })- .catch((error) => {- server.log.error("Failed to initialize uptime service:", error);- // Continue running without uptime monitoring- });+ // uptimeService+ // .initialize()+ // .then(() => {+ // server.log.info("Uptime monitoring service initialized successfully");+ // })+ // .catch((error) => {+ // server.log.error("Failed to initialize uptime service:", error);+ // // Continue running without uptime monitoring+ // });} catch (err) {server.log.error(err);process.exit(1);diff --git a/server/src/lib/ipUtils.ts b/server/src/lib/ipUtils.tsnew file mode 100644index 000000000..30d92ba64--- /dev/null+++ b/server/src/lib/ipUtils.ts@@ -0,0 +1,149 @@+import { Address4, Address6 } from "ip-address";+import { logger } from "./logger/logger.js";++export interface IPRange {+ ip: string;+ type: "single" | "range" | "cidr";+}++/**+ * Validate if an IP pattern is valid+ */+export function validateIPPattern(pattern: string): { valid: boolean; error?: string } {+ try {+ const trimmedPattern = pattern.trim();++ // Empty or whitespace-only pattern is valid (matches client-side behavior)+ if (!trimmedPattern) {+ return { valid: true };+ }++ // Single IP+ if (!trimmedPattern.includes("/") && !trimmedPattern.includes("-")) {+ try {+ new Address4(trimmedPattern);+ return { valid: true };+ } catch {+ try {+ new Address6(trimmedPattern);+ return { valid: true };+ } catch {+ return { valid: false, error: "Invalid IP address format" };+ }+ }+ }++ // CIDR notation+ if (trimmedPattern.includes("/")) {+ try {+ new Address4(trimmedPattern);+ return { valid: true };+ } catch {+ try {+ new Address6(trimmedPattern);+ return { valid: true };+ } catch {+ return { valid: false, error: "Invalid CIDR notation" };+ }+ }+ }++ // Range notation (IPv4 only)+ if (trimmedPattern.includes("-")) {+ const [startIP, endIP] = trimmedPattern.split("-").map((ip) => ip.trim());+ if (!startIP || !endIP) {+ return { valid: false, error: "Invalid range format" };+ }++ try {+ new Address4(startIP);+ new Address4(endIP);+ return { valid: true };+ } catch {+ // Check if these are IPv6 addresses to provide a better error message+ try {+ new Address6(startIP);+ new Address6(endIP);+ return {+ valid: false,+ error: "IPv6 range notation not supported. Use CIDR notation instead (e.g., 2001:db8::/32)",+ };+ } catch {+ return { valid: false, error: "Invalid IP addresses in range" };+ }+ }+ }++ return { valid: false, error: "Unknown IP pattern format" };+ } catch (error) {+ return { valid: false, error: `Validation error: ${error}` };+ }+}++/**+ * Check if IP matches CIDR notation (e.g., 192.168.1.0/24)+ */+export function matchesCIDR(ipAddress: string, cidr: string): boolean {+ try {+ // Try IPv4 first+ try {+ const ipv4 = new Address4(ipAddress);+ const cidrv4 = new Address4(cidr);+ return ipv4.isInSubnet(cidrv4);+ } catch {+ // Try IPv6+ const ipv6 = new Address6(ipAddress);+ const cidrv6 = new Address6(cidr);+ return ipv6.isInSubnet(cidrv6);+ }+ } catch (error) {+ logger.warn(`Error matching CIDR ${cidr} for IP ${ipAddress}:`, error);+ return false;+ }+}++/**+ * Check if IP is in range (e.g., 192.168.1.1-192.168.1.10)+ *+ * NOTE: Range notation is only supported for IPv4 addresses.+ * IPv6 range notation is not supported due to the complexity of proper+ * numerical comparison of 128-bit addresses. Use CIDR notation instead+ * for IPv6 (e.g., 2001:db8::/32).+ */+export function matchesRange(ipAddress: string, range: string): boolean {+ try {+ const [startIP, endIP] = range.split("-").map((ip) => ip.trim());++ // Try IPv4 first+ try {+ const ip = new Address4(ipAddress);+ const start = new Address4(startIP);+ const end = new Address4(endIP);++ // Convert to 32-bit integers for comparison+ const ipInt = ip.toArray().reduce((acc, octet) => (acc << 8) + octet, 0) >>> 0; // Use unsigned right shift+ const startInt = start.toArray().reduce((acc, octet) => (acc << 8) + octet, 0) >>> 0;+ const endInt = end.toArray().reduce((acc, octet) => (acc << 8) + octet, 0) >>> 0;++ return ipInt >= startInt && ipInt <= endInt;+ } catch {+ // IPv6 range matching is not supported - reject IPv6 ranges+ // This prevents incorrect matches due to lexicographic comparison+ try {+ new Address6(ipAddress);+ new Address6(startIP);+ new Address6(endIP);++ // All addresses are valid IPv6, but we don't support range notation+ logger.warn(`IPv6 range notation not supported: ${range}. Use CIDR notation instead (e.g., 2001:db8::/32)`);+ return false;+ } catch {+ // Mixed or invalid IP addresses+ return false;+ }+ }+ } catch (error) {+ logger.warn(`Error matching range ${range} for IP ${ipAddress}:`, error);+ return false;+ }+}diff --git a/server/src/lib/logger/logger.ts b/server/src/lib/logger/logger.tsindex b41a50326..4c321021a 100644--- a/server/src/lib/logger/logger.ts+++ b/server/src/lib/logger/logger.ts@@ -29,8 +29,9 @@ export const createLogger = (name: string): FastifyBaseLogger => {level: process.env.LOG_LEVEL || "info",options: {colorize: true,- translateTime: "SYS:standard",- ignore: "pid,hostname",+ singleLine: true,+ translateTime: "HH:MM:ss",+ ignore: "pid,hostname,name",destination: 1, // stdout},},@@ -48,8 +49,9 @@ export const createLogger = (name: string): FastifyBaseLogger => {target: "pino-pretty",options: {colorize: true,- translateTime: "SYS:standard",- ignore: "pid,hostname",+ singleLine: true,+ translateTime: "HH:MM:ss",+ ignore: "pid,hostname,name",},},}) as FastifyBaseLogger;@@ -66,4 +68,4 @@ export const logger = createLogger("rybbit");export const createServiceLogger = (service: string) => {return logger.child({ service });-};\ No newline at end of file+};diff --git a/server/src/lib/siteConfig.ts b/server/src/lib/siteConfig.tsindex abfd59d5a..05bf86013 100644--- a/server/src/lib/siteConfig.ts+++ b/server/src/lib/siteConfig.ts@@ -1,5 +1,7 @@import { db } from "../db/postgres/postgres.js";import { sites } from "../db/postgres/schema.js";+import { logger } from "./logger/logger.js";+import { matchesCIDR, matchesRange } from "./ipUtils.js";// Site configuration interfaceinterface SiteConfigData {@@ -7,6 +9,7 @@ interface SiteConfigData {saltUserIds: boolean;domain: string;blockBots: boolean;+ excludedIPs: string[];apiKey?: string | null;}@@ -23,6 +26,7 @@ class SiteConfig {saltUserIds: sites.saltUserIds,domain: sites.domain,blockBots: sites.blockBots,+ excludedIPs: sites.excludedIPs,apiKey: sites.apiKey,}).from(sites);@@ -37,6 +41,7 @@ class SiteConfig {saltUserIds: site.saltUserIds || false,domain: site.domain || "",blockBots: site.blockBots === undefined ? true : site.blockBots,+ excludedIPs: Array.isArray(site.excludedIPs) ? site.excludedIPs : [],apiKey: site.apiKey,});}@@ -152,6 +157,26 @@ class SiteConfig {}}+ /**+ * Get excluded IPs for a site+ */+ getExcludedIPs(siteId: string | number): string[] {+ const numericSiteId = Number(siteId);+ const config = this.siteConfigMap.get(numericSiteId);+ return config?.excludedIPs || [];+ }++ /**+ * Update the excluded IPs of a site in the cache+ */+ updateSiteExcludedIPs(siteId: number, excludedIPs: string[]): void {+ const config = this.siteConfigMap.get(siteId);+ if (config) {+ config.excludedIPs = excludedIPs;+ this.siteConfigMap.set(siteId, config);+ }+ }+/*** Add a new site to the cache*/@@ -174,6 +199,58 @@ class SiteConfig {await this.loadSiteConfigs();}}++ /**+ * Check if an IP address matches any of the excluded IPs/ranges+ */+ isIPExcluded(ipAddress: string, siteId: string | number): boolean {+ const excludedIPs = this.getExcludedIPs(siteId);+ if (!excludedIPs || excludedIPs.length === 0) {+ return false;+ }++ for (const excludedPattern of excludedIPs) {+ if (this.matchesIPPattern(ipAddress, excludedPattern)) {+ return true;+ }+ }++ return false;+ }++ /**+ * Check if an IP address matches a specific pattern+ * Supports:+ * - Single IP: 192.168.1.1, 2001:db8::1+ * - CIDR notation: 192.168.1.0/24, 2001:db8::/32+ * - Range notation: 192.168.1.1-192.168.1.10 (IPv4 only, IPv6 ranges not supported)+ */+ private matchesIPPattern(ipAddress: string, pattern: string): boolean {+ try {+ const trimmedPattern = pattern.trim();++ // Single IP match+ if (!trimmedPattern.includes("/") && !trimmedPattern.includes("-")) {+ return ipAddress === trimmedPattern;+ }++ // CIDR notation+ if (trimmedPattern.includes("/")) {+ return matchesCIDR(ipAddress, trimmedPattern);+ }++ // Range notation+ if (trimmedPattern.includes("-")) {+ return matchesRange(ipAddress, trimmedPattern);+ }++ return false;+ } catch (error) {+ logger.warn(error as Error, `Invalid IP pattern: ${pattern}`);+ return false;+ }+ }+}// Singleton instancediff --git a/server/src/services/shared/requestValidation.test.ts b/server/src/services/shared/requestValidation.test.tsindex bee6ddfe1..4b1dcd1ca 100644--- a/server/src/services/shared/requestValidation.test.ts+++ b/server/src/services/shared/requestValidation.test.ts@@ -1,4 +1,4 @@-import { describe, it, expect, vi, beforeEach } from 'vitest';+import { describe, it, expect, vi, beforeEach } from "vitest";// Define the expected site config type to match SiteConfigDatainterface SiteConfigData {@@ -8,406 +8,418 @@ interface SiteConfigData {public: boolean;saltUserIds: boolean;blockBots: boolean;+ excludedIPs: string[];}// Mock dependencies-vi.mock('../../lib/rateLimiter.js', () => ({+vi.mock("../../lib/rateLimiter.js", () => ({apiKeyRateLimiter: {- isAllowed: vi.fn()- }+ isAllowed: vi.fn(),+ },}));-vi.mock('../../lib/siteConfig.js', () => ({+vi.mock("../../lib/siteConfig.js", () => ({siteConfig: {ensureInitialized: vi.fn(),- getSiteConfig: vi.fn()- }+ getSiteConfig: vi.fn(),+ },}));-vi.mock('../../utils.js', () => ({- normalizeOrigin: vi.fn()+vi.mock("../../utils.js", () => ({+ normalizeOrigin: vi.fn(),}));-import { validateApiKey, validateOrigin, checkApiKeyRateLimit } from './requestValidation.js';+import { validateApiKey, validateOrigin, checkApiKeyRateLimit } from "./requestValidation.js";// Import mocked modules-import { apiKeyRateLimiter } from '../../lib/rateLimiter.js';-import { siteConfig } from '../../lib/siteConfig.js';-import { normalizeOrigin } from '../../utils.js';+import { apiKeyRateLimiter } from "../../lib/rateLimiter.js";+import { siteConfig } from "../../lib/siteConfig.js";+import { normalizeOrigin } from "../../utils.js";-describe('validateApiKey', () => {+describe("validateApiKey", () => {beforeEach(() => {vi.clearAllMocks();});- it('should return success false when no API key is provided', async () => {+ it("should return success false when no API key is provided", async () => {const result = await validateApiKey(1);expect(result).toEqual({ success: false });});- it('should return success false when API key is empty string', async () => {- const result = await validateApiKey(1, '');+ it("should return success false when API key is empty string", async () => {+ const result = await validateApiKey(1, "");expect(result).toEqual({ success: false });});- it('should return success false when site is not found', async () => {+ it("should return success false when site is not found", async () => {vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(undefined);- const result = await validateApiKey(1, 'test-api-key');+ const result = await validateApiKey(1, "test-api-key");expect(siteConfig.ensureInitialized).toHaveBeenCalled();expect(siteConfig.getSiteConfig).toHaveBeenCalledWith(1);- expect(result).toEqual({ success: false, error: 'Site not found' });+ expect(result).toEqual({ success: false, error: "Site not found" });});- it('should return success true when API key matches', async () => {- const mockSite: SiteConfigData = {+ it("should return success true when API key matches", async () => {+ const mockSite: SiteConfigData = {id: 1,- apiKey: 'valid-api-key',- domain: 'example.com',+ apiKey: "valid-api-key",+ domain: "example.com",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);// Mock console.info to avoid output during tests- const consoleSpy = vi.spyOn(console, 'info').mockImplementation(() => {});+ const consoleSpy = vi.spyOn(console, "info").mockImplementation(() => {});- const result = await validateApiKey(1, 'valid-api-key');+ const result = await validateApiKey(1, "valid-api-key");expect(result).toEqual({ success: true });- expect(consoleSpy).toHaveBeenCalledWith('[Validation] Valid API key for site 1');-+ expect(consoleSpy).toHaveBeenCalledWith("[Validation] Valid API key for site 1");+consoleSpy.mockRestore();});- it('should return success false when API key does not match', async () => {- const mockSite: SiteConfigData = {+ it("should return success false when API key does not match", async () => {+ const mockSite: SiteConfigData = {id: 1,- apiKey: 'valid-api-key',- domain: 'example.com',+ apiKey: "valid-api-key",+ domain: "example.com",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);- const result = await validateApiKey(1, 'invalid-api-key');+ const result = await validateApiKey(1, "invalid-api-key");- expect(result).toEqual({ success: false, error: 'Invalid API key' });+ expect(result).toEqual({ success: false, error: "Invalid API key" });});- it('should handle string siteId by converting to number', async () => {- const mockSite: SiteConfigData = {+ it("should handle string siteId by converting to number", async () => {+ const mockSite: SiteConfigData = {id: 123,- apiKey: 'test-key',- domain: 'example.com',+ apiKey: "test-key",+ domain: "example.com",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);- await validateApiKey('123', 'test-key');+ await validateApiKey("123", "test-key");expect(siteConfig.getSiteConfig).toHaveBeenCalledWith(123);});- it('should handle site with no API key configured', async () => {- const mockSite: Partial<SiteConfigData> & Pick<SiteConfigData, 'id' | 'domain' | 'public' | 'saltUserIds' | 'blockBots'> = {+ it("should handle site with no API key configured", async () => {+ const mockSite: Partial<SiteConfigData> &+ Pick<SiteConfigData, "id" | "domain" | "public" | "saltUserIds" | "blockBots"> = {id: 1,- domain: 'example.com',+ domain: "example.com",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],}; // No apiKey propertyvi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite as SiteConfigData);- const result = await validateApiKey(1, 'any-key');+ const result = await validateApiKey(1, "any-key");- expect(result).toEqual({ success: false, error: 'Invalid API key' });+ expect(result).toEqual({ success: false, error: "Invalid API key" });});- it('should handle errors during validation', async () => {- vi.mocked(siteConfig.ensureInitialized).mockRejectedValue(new Error('Database error'));-- const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});+ it("should handle errors during validation", async () => {+ vi.mocked(siteConfig.ensureInitialized).mockRejectedValue(new Error("Database error"));++ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});++ const result = await validateApiKey(1, "test-key");- const result = await validateApiKey(1, 'test-key');+ expect(result).toEqual({ success: false, error: "Failed to validate API key" });+ expect(consoleSpy).toHaveBeenCalledWith("Error validating API key:", expect.any(Error));- expect(result).toEqual({ success: false, error: 'Failed to validate API key' });- expect(consoleSpy).toHaveBeenCalledWith('Error validating API key:', expect.any(Error));-consoleSpy.mockRestore();});});-describe('validateOrigin', () => {+describe("validateOrigin", () => {beforeEach(() => {vi.clearAllMocks();vi.unstubAllEnvs();});- it('should return success true when origin checking is disabled', async () => {+ it("should return success true when origin checking is disabled", async () => {// Set environment variable before any imports happen- vi.stubEnv('DISABLE_ORIGIN_CHECK', 'true');-+ vi.stubEnv("DISABLE_ORIGIN_CHECK", "true");+// Re-import the module to pick up the new environment variableawait vi.resetModules();-+// Re-establish the mocks after reset- vi.doMock('../../lib/siteConfig.js', () => ({+ vi.doMock("../../lib/siteConfig.js", () => ({siteConfig: {ensureInitialized: vi.fn(),- getSiteConfig: vi.fn()- }+ getSiteConfig: vi.fn(),+ },}));-- vi.doMock('../../utils.js', () => ({- normalizeOrigin: vi.fn()++ vi.doMock("../../utils.js", () => ({+ normalizeOrigin: vi.fn(),}));-- const { validateOrigin } = await import('./requestValidation.js');- const consoleSpy = vi.spyOn(console, 'info').mockImplementation(() => {});+ const { validateOrigin } = await import("./requestValidation.js");- const result = await validateOrigin(1, 'https://example.com');+ const consoleSpy = vi.spyOn(console, "info").mockImplementation(() => {});++ const result = await validateOrigin(1, "https://example.com");expect(result).toEqual({ success: true });expect(consoleSpy).toHaveBeenCalledWith(- '[Validation] Origin check disabled. Allowing request for site 1 from origin: https://example.com'+ "[Validation] Origin check disabled. Allowing request for site 1 from origin: https://example.com",);-+consoleSpy.mockRestore();vi.unstubAllEnvs();});- it('should return success false when site is not found', async () => {+ it("should return success false when site is not found", async () => {vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(undefined);- const result = await validateOrigin(1, 'https://example.com');+ const result = await validateOrigin(1, "https://example.com");- expect(result).toEqual({ success: false, error: 'Site not found' });+ expect(result).toEqual({ success: false, error: "Site not found" });});- it('should return success false when no origin header is provided', async () => {- const mockSite: SiteConfigData = {+ it("should return success false when no origin header is provided", async () => {+ const mockSite: SiteConfigData = {id: 1,- domain: 'example.com',- apiKey: 'test-key',+ domain: "example.com",+ apiKey: "test-key",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);const result = await validateOrigin(1);- expect(result).toEqual({ success: false, error: 'Origin header required' });+ expect(result).toEqual({ success: false, error: "Origin header required" });});- it('should return success true for exact domain match', async () => {- const mockSite: SiteConfigData = {+ it("should return success true for exact domain match", async () => {+ const mockSite: SiteConfigData = {id: 1,- domain: 'example.com',- apiKey: 'test-key',+ domain: "example.com",+ apiKey: "test-key",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);vi.mocked(normalizeOrigin)- .mockReturnValueOnce('example.com') // for request origin- .mockReturnValueOnce('example.com'); // for site domain+ .mockReturnValueOnce("example.com") // for request origin+ .mockReturnValueOnce("example.com"); // for site domain- const result = await validateOrigin(1, 'https://example.com');+ const result = await validateOrigin(1, "https://example.com");- expect(normalizeOrigin).toHaveBeenCalledWith('https://example.com');- expect(normalizeOrigin).toHaveBeenCalledWith('https://example.com');+ expect(normalizeOrigin).toHaveBeenCalledWith("https://example.com");+ expect(normalizeOrigin).toHaveBeenCalledWith("https://example.com");expect(result).toEqual({ success: true });});- it('should return success true for subdomain (always allowed)', async () => {- const mockSite: SiteConfigData = {+ it("should return success true for subdomain (always allowed)", async () => {+ const mockSite: SiteConfigData = {id: 1,- domain: 'example.com',- apiKey: 'test-key',+ domain: "example.com",+ apiKey: "test-key",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);vi.mocked(normalizeOrigin)- .mockReturnValueOnce('sub.example.com') // for request origin- .mockReturnValueOnce('example.com'); // for site domain+ .mockReturnValueOnce("sub.example.com") // for request origin+ .mockReturnValueOnce("example.com"); // for site domain- const result = await validateOrigin(1, 'https://sub.example.com');+ const result = await validateOrigin(1, "https://sub.example.com");expect(result).toEqual({ success: true });});- it('should return success true for nested subdomain', async () => {- const mockSite: SiteConfigData = {+ it("should return success true for nested subdomain", async () => {+ const mockSite: SiteConfigData = {id: 1,- domain: 'example.com',- apiKey: 'test-key',+ domain: "example.com",+ apiKey: "test-key",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);vi.mocked(normalizeOrigin)- .mockReturnValueOnce('deep.nested.sub.example.com') // for request origin- .mockReturnValueOnce('example.com'); // for site domain+ .mockReturnValueOnce("deep.nested.sub.example.com") // for request origin+ .mockReturnValueOnce("example.com"); // for site domain- const result = await validateOrigin(1, 'https://deep.nested.sub.example.com');+ const result = await validateOrigin(1, "https://deep.nested.sub.example.com");expect(result).toEqual({ success: true });});- it('should return success false for different domain', async () => {- const mockSite: SiteConfigData = {+ it("should return success false for different domain", async () => {+ const mockSite: SiteConfigData = {id: 1,- domain: 'example.com',- apiKey: 'test-key',+ domain: "example.com",+ apiKey: "test-key",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);vi.mocked(normalizeOrigin)- .mockReturnValueOnce('different.com') // for request origin- .mockReturnValueOnce('example.com'); // for site domain+ .mockReturnValueOnce("different.com") // for request origin+ .mockReturnValueOnce("example.com"); // for site domain- const result = await validateOrigin(1, 'https://different.com');+ const result = await validateOrigin(1, "https://different.com");- expect(result).toEqual({- success: false,- error: 'Origin mismatch. Received: https://different.com'+ expect(result).toEqual({+ success: false,+ error: "Origin mismatch. Received: https://different.com",});});- it('should return success false for domain that contains site domain but is not a subdomain', async () => {- const mockSite: SiteConfigData = {+ it("should return success false for domain that contains site domain but is not a subdomain", async () => {+ const mockSite: SiteConfigData = {id: 1,- domain: 'example.com',- apiKey: 'test-key',+ domain: "example.com",+ apiKey: "test-key",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);vi.mocked(normalizeOrigin)- .mockReturnValueOnce('notexample.com') // for request origin- .mockReturnValueOnce('example.com'); // for site domain+ .mockReturnValueOnce("notexample.com") // for request origin+ .mockReturnValueOnce("example.com"); // for site domain- const result = await validateOrigin(1, 'https://notexample.com');+ const result = await validateOrigin(1, "https://notexample.com");- expect(result).toEqual({- success: false,- error: 'Origin mismatch. Received: https://notexample.com'+ expect(result).toEqual({+ success: false,+ error: "Origin mismatch. Received: https://notexample.com",});});- it('should handle string siteId by converting to number', async () => {- const mockSite: SiteConfigData = {+ it("should handle string siteId by converting to number", async () => {+ const mockSite: SiteConfigData = {id: 123,- domain: 'example.com',- apiKey: 'test-key',+ domain: "example.com",+ apiKey: "test-key",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);- await validateOrigin('123', 'https://example.com');+ await validateOrigin("123", "https://example.com");expect(siteConfig.getSiteConfig).toHaveBeenCalledWith(123);});- it('should handle invalid origin format', async () => {- const mockSite: SiteConfigData = {+ it("should handle invalid origin format", async () => {+ const mockSite: SiteConfigData = {id: 1,- domain: 'example.com',- apiKey: 'test-key',+ domain: "example.com",+ apiKey: "test-key",public: true,saltUserIds: false,- blockBots: true+ blockBots: true,+ excludedIPs: [],};vi.mocked(siteConfig.ensureInitialized).mockResolvedValue(undefined);vi.mocked(siteConfig.getSiteConfig).mockResolvedValue(mockSite);vi.mocked(normalizeOrigin).mockImplementation(() => {- throw new Error('Invalid URL');+ throw new Error("Invalid URL");});- const result = await validateOrigin(1, 'invalid-origin');+ const result = await validateOrigin(1, "invalid-origin");- expect(result).toEqual({- success: false,- error: 'Invalid origin format: invalid-origin'+ expect(result).toEqual({+ success: false,+ error: "Invalid origin format: invalid-origin",});});- it('should handle errors during validation', async () => {- vi.mocked(siteConfig.ensureInitialized).mockRejectedValue(new Error('Database error'));-- const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});+ it("should handle errors during validation", async () => {+ vi.mocked(siteConfig.ensureInitialized).mockRejectedValue(new Error("Database error"));++ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});- const result = await validateOrigin(1, 'https://example.com');+ const result = await validateOrigin(1, "https://example.com");++ expect(result).toEqual({ success: false, error: "Internal error validating origin" });+ expect(consoleSpy).toHaveBeenCalledWith("Error validating origin:", expect.any(Error));- expect(result).toEqual({ success: false, error: 'Internal error validating origin' });- expect(consoleSpy).toHaveBeenCalledWith('Error validating origin:', expect.any(Error));-consoleSpy.mockRestore();});});-describe('checkApiKeyRateLimit', () => {+describe("checkApiKeyRateLimit", () => {beforeEach(() => {vi.clearAllMocks();});- it('should return true when rate limiter allows the request', () => {+ it("should return true when rate limiter allows the request", () => {vi.mocked(apiKeyRateLimiter.isAllowed).mockReturnValue(true);- const result = checkApiKeyRateLimit('test-api-key');+ const result = checkApiKeyRateLimit("test-api-key");- expect(apiKeyRateLimiter.isAllowed).toHaveBeenCalledWith('test-api-key');+ expect(apiKeyRateLimiter.isAllowed).toHaveBeenCalledWith("test-api-key");expect(result).toBe(true);});- it('should return false when rate limiter blocks the request', () => {+ it("should return false when rate limiter blocks the request", () => {vi.mocked(apiKeyRateLimiter.isAllowed).mockReturnValue(false);- const result = checkApiKeyRateLimit('test-api-key');+ const result = checkApiKeyRateLimit("test-api-key");- expect(apiKeyRateLimiter.isAllowed).toHaveBeenCalledWith('test-api-key');+ expect(apiKeyRateLimiter.isAllowed).toHaveBeenCalledWith("test-api-key");expect(result).toBe(false);});- it('should handle different API keys', () => {- vi.mocked(apiKeyRateLimiter.isAllowed)- .mockReturnValueOnce(true)- .mockReturnValueOnce(false);+ it("should handle different API keys", () => {+ vi.mocked(apiKeyRateLimiter.isAllowed).mockReturnValueOnce(true).mockReturnValueOnce(false);- expect(checkApiKeyRateLimit('key1')).toBe(true);- expect(checkApiKeyRateLimit('key2')).toBe(false);+ expect(checkApiKeyRateLimit("key1")).toBe(true);+ expect(checkApiKeyRateLimit("key2")).toBe(false);- expect(apiKeyRateLimiter.isAllowed).toHaveBeenNthCalledWith(1, 'key1');- expect(apiKeyRateLimiter.isAllowed).toHaveBeenNthCalledWith(2, 'key2');+ expect(apiKeyRateLimiter.isAllowed).toHaveBeenNthCalledWith(1, "key1");+ expect(apiKeyRateLimiter.isAllowed).toHaveBeenNthCalledWith(2, "key2");});-});\ No newline at end of file+});diff --git a/server/src/services/tracker/trackEvent.ts b/server/src/services/tracker/trackEvent.tsindex b366277bd..c01321129 100644--- a/server/src/services/tracker/trackEvent.ts+++ b/server/src/services/tracker/trackEvent.ts@@ -172,7 +172,10 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {// If API key validation failed with an error, reject the requestif (apiKeyValidation.error) {- logger.warn({ siteId: validatedPayload.site_id, error: apiKeyValidation.error }, "Request rejected - API key validation failed");+ logger.warn(+ { siteId: validatedPayload.site_id, error: apiKeyValidation.error },+ "Request rejected - API key validation failed",+ );return reply.status(403).send({success: false,error: apiKeyValidation.error,@@ -184,7 +187,7 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {if (!checkApiKeyRateLimit(validatedPayload.api_key)) {logger.warn({ apiKey: validatedPayload.api_key, siteId: validatedPayload.site_id },- "Rate limit exceeded for API key"+ "Rate limit exceeded for API key",);return reply.status(429).send({success: false,@@ -198,7 +201,10 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {const originValidation = await validateOrigin(validatedPayload.site_id, request.headers.origin as string);if (!originValidation.success) {- logger.warn({ siteId: validatedPayload.site_id, error: originValidation.error }, "Request rejected - origin validation failed");+ logger.warn(+ { siteId: validatedPayload.site_id, error: originValidation.error },+ "Request rejected - origin validation failed",+ );return reply.status(403).send({success: false,error: originValidation.error,@@ -215,10 +221,7 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {// Use custom user agent if provided, otherwise fall back to headerconst userAgent = validatedPayload.user_agent || (request.headers["user-agent"] as string);if (userAgent && isbot(userAgent)) {- logger.info(- { siteId: validatedPayload.site_id, userAgent },- "Bot request filtered"- );+ logger.info({ siteId: validatedPayload.site_id, userAgent }, "Bot request filtered");return reply.status(200).send({success: true,message: "Event not tracked - bot detected",@@ -232,6 +235,18 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {return reply.status(200).send("Site over monthly limit, event not tracked");}+ // Check if the IP should be excluded from tracking+ // Use custom IP if provided in payload, otherwise get from request+ const requestIP = validatedPayload.ip_address || request.ip || "";++ if (siteConfig.isIPExcluded(requestIP, validatedPayload.site_id)) {+ logger.info({ siteId: validatedPayload.site_id, ip: requestIP }, "IP excluded from tracking");+ return reply.status(200).send({+ success: true,+ message: "Event not tracked - IP excluded",+ });+ }+// Create base payload for the event using validated dataconst payload = createBasePayload(request, // Pass request for IP/UA