Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Add user ID salting feature and update site settings

Bill Yangafd3f68d5eb9
diff --git a/client/src/api/admin/sites.ts b/client/src/api/admin/sites.ts
index 43d14349d..5b8d70a1d 100644
--- a/client/src/api/admin/sites.ts
+++ b/client/src/api/admin/sites.ts
@@ -11,6 +11,7 @@ export type SiteResponse = {
updatedAt: string;
createdBy: string;
public: boolean;
+ saltUserIds: boolean;
isOwner: boolean;
};
@@ -22,6 +23,7 @@ export type GetSitesResponse = {
updatedAt: string;
createdBy: string;
public: boolean;
+ saltUserIds: boolean;
overMonthlyLimit?: boolean;
monthlyEventCount?: number;
eventLimit?: number;
@@ -127,3 +129,16 @@ export function useGetSite(siteId?: string | number) {
enabled: !!siteId,
});
}
+
+export function changeSiteSalt(siteId: number, saltUserIds: boolean) {
+ return authedFetch(`${BACKEND_URL}/change-site-salt`, {
+ method: "POST",
+ body: JSON.stringify({
+ siteId,
+ saltUserIds,
+ }),
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+}
diff --git a/client/src/components/SiteSettings/SiteSettings.tsx b/client/src/components/SiteSettings/SiteSettings.tsx
index 0e78019fd..adcd8627d 100644
--- a/client/src/components/SiteSettings/SiteSettings.tsx
+++ b/client/src/components/SiteSettings/SiteSettings.tsx
@@ -35,6 +35,7 @@ import { Switch } from "@/components/ui/switch";
import {
changeSiteDomain,
changeSitePublic,
+ changeSiteSalt,
deleteSite,
GetSitesResponse,
SiteResponse,
@@ -72,6 +73,8 @@ export function SiteSettingsInner({
const [isChangingDomain, setIsChangingDomain] = useState(false);
const [isPublic, setIsPublic] = useState(siteMetadata.public || false);
const [isChangingPublic, setIsChangingPublic] = useState(false);
+ const [isSalting, setIsSalting] = useState(siteMetadata.saltUserIds || false);
+ const [isChangingSalt, setIsChangingSalt] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const [dialogOpen, setDialogOpen] = useState(false);
@@ -151,6 +154,24 @@ export function SiteSettingsInner({
}
};
+ const handleSaltToggle = async (checked: boolean) => {
+ try {
+ setIsChangingSalt(true);
+ await changeSiteSalt(siteMetadata.siteId, checked);
+ setIsSalting(checked);
+ toast.success(
+ checked ? "User ID salting enabled" : "User ID salting disabled"
+ );
+ refetch();
+ } catch (error) {
+ console.error("Error changing salt setting:", error);
+ toast.error("Failed to update salting setting");
+ setIsSalting(!checked); // Revert UI state on error
+ } finally {
+ setIsChangingSalt(false);
+ }
+ };
+
if (!siteMetadata) {
return null;
}
@@ -262,6 +283,28 @@ export function SiteSettingsInner({
/>
</div>
+ {/* User ID Salting Section */}
+ <div className="flex items-center justify-between">
+ <div>
+ <Label
+ htmlFor="saltUserIds"
+ className="text-sm font-medium text-foreground block"
+ >
+ Enable User ID Salting
+ </Label>
+ <p className="text-xs text-muted-foreground mt-1">
+ When enabled, user IDs will be salted with a daily rotating key
+ for enhanced privacy
+ </p>
+ </div>
+ <Switch
+ id="saltUserIds"
+ checked={isSalting}
+ disabled={isChangingSalt}
+ onCheckedChange={handleSaltToggle}
+ />
+ </div>
+
{/* Domain Settings Section */}
<div className="space-y-3">
<div>
diff --git a/server/src/api/sites/addSite.ts b/server/src/api/sites/addSite.ts
index 0e100c406..d13b798d7 100644
--- a/server/src/api/sites/addSite.ts
+++ b/server/src/api/sites/addSite.ts
@@ -1,10 +1,9 @@
-import { fromNodeHeaders } from "better-auth/node";
import { FastifyReply, FastifyRequest } from "fastify";
import { db } from "../../db/postgres/postgres.js";
import { sites } from "../../db/postgres/schema.js";
import { loadAllowedDomains } from "../../lib/allowedDomains.js";
import { auth } from "../../lib/auth.js";
-import { publicSites } from "../../lib/publicSites.js";
+import { siteConfig } from "../../lib/siteConfig.js";
export async function addSite(
request: FastifyRequest<{
@@ -92,8 +91,11 @@ export async function addSite(
// Update allowed domains
await loadAllowedDomains();
- // Update publicSites cache with the new site
- publicSites.addSite(newSite[0].siteId, newSite[0].public || false);
+ // Update siteConfig cache with the new site
+ siteConfig.addSite(newSite[0].siteId, {
+ public: newSite[0].public || false,
+ saltUserIds: newSite[0].saltUserIds || false,
+ });
return reply.status(201).send(newSite[0]);
} catch (error) {
diff --git a/server/src/api/sites/changeSitePublic.ts b/server/src/api/sites/changeSitePublic.ts
index 375d61b77..e1e745160 100644
--- a/server/src/api/sites/changeSitePublic.ts
+++ b/server/src/api/sites/changeSitePublic.ts
@@ -3,7 +3,7 @@ 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";
-import { publicSites } from "../../lib/publicSites.js";
+import { siteConfig } from "../../lib/siteConfig.js";
interface ChangeSitePublicRequest {
Body: {
@@ -51,7 +51,7 @@ export async function changeSitePublic(
.where(eq(sites.siteId, siteId));
// Update the public sites cache
- publicSites.updateSitePublicStatus(siteId, isPublic);
+ siteConfig.updateSitePublicStatus(siteId, isPublic);
return reply.status(200).send({ success: true });
} catch (error) {
diff --git a/server/src/api/sites/changeSiteSalt.ts b/server/src/api/sites/changeSiteSalt.ts
new file mode 100644
index 000000000..4dc58354f
--- /dev/null
+++ b/server/src/api/sites/changeSiteSalt.ts
@@ -0,0 +1,56 @@
+import { FastifyRequest, FastifyReply } from "fastify";
+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";
+import { siteConfig } from "../../lib/siteConfig.js";
+
+interface ChangeSiteSaltRequest {
+ Body: {
+ siteId: number;
+ saltUserIds: boolean;
+ };
+}
+
+export async function changeSiteSalt(
+ request: FastifyRequest<ChangeSiteSaltRequest>,
+ reply: FastifyReply
+) {
+ const { siteId, saltUserIds } = request.body;
+
+ const userHasAccessToSite = await getUserHasAccessToSite(
+ request,
+ String(siteId)
+ );
+ if (!userHasAccessToSite) {
+ return reply.status(403).send({ error: "Forbidden" });
+ }
+
+ try {
+ // Fetch site to check it exists
+ const site = await db.query.sites.findFirst({
+ where: eq(sites.siteId, siteId),
+ });
+
+ if (!site) {
+ return reply.status(404).send({ error: "Site not found" });
+ }
+
+ // Update site salt setting
+ await db
+ .update(sites)
+ .set({
+ saltUserIds: saltUserIds,
+ updatedAt: new Date(),
+ })
+ .where(eq(sites.siteId, siteId));
+
+ // Update the site config cache
+ siteConfig.updateSiteSaltSetting(siteId, saltUserIds);
+
+ return reply.status(200).send({ success: true });
+ } catch (error) {
+ console.error("Error changing site salt setting:", error);
+ return reply.status(500).send({ error: "Internal server error" });
+ }
+}
diff --git a/server/src/api/sites/deleteSite.ts b/server/src/api/sites/deleteSite.ts
index e27a92064..636088f23 100644
--- a/server/src/api/sites/deleteSite.ts
+++ b/server/src/api/sites/deleteSite.ts
@@ -4,7 +4,7 @@ import { db } from "../../db/postgres/postgres.js";
import { sites } from "../../db/postgres/schema.js";
import { loadAllowedDomains } from "../../lib/allowedDomains.js";
import { getUserHasAccessToSite } from "../../lib/auth-utils.js";
-import { publicSites } from "../../lib/publicSites.js";
+import { siteConfig } from "../../lib/siteConfig.js";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
export async function deleteSite(
@@ -24,8 +24,8 @@ export async function deleteSite(
});
await loadAllowedDomains();
- // Remove the site from the publicSites cache
- publicSites.removeSite(Number(id));
+ // Remove the site from the siteConfig cache
+ siteConfig.removeSite(Number(id));
return reply.status(200).send();
}
diff --git a/server/src/api/sites/getSiteIsPublic.ts b/server/src/api/sites/getSiteIsPublic.ts
index 042386391..e7cfdcc75 100644
--- a/server/src/api/sites/getSiteIsPublic.ts
+++ b/server/src/api/sites/getSiteIsPublic.ts
@@ -1,11 +1,11 @@
import { FastifyRequest, FastifyReply } from "fastify";
-import { publicSites } from "../../lib/publicSites.js";
+import { siteConfig } from "../../lib/siteConfig.js";
export async function getSiteIsPublic(
request: FastifyRequest<{ Params: { site: string } }>,
reply: FastifyReply
) {
const { site } = request.params;
- const isPublic = publicSites.isSitePublic(site);
+ const isPublic = siteConfig.isSitePublic(site);
return reply.status(200).send({ isPublic });
}
diff --git a/server/src/index.ts b/server/src/index.ts
index c5958dcc5..9a91c3564 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -26,6 +26,7 @@ import { getUsers } from "./api/analytics/getUsers.js";
import { addSite } from "./api/sites/addSite.js";
import { changeSiteDomain } from "./api/sites/changeSiteDomain.js";
import { changeSitePublic } from "./api/sites/changeSitePublic.js";
+import { changeSiteSalt } from "./api/sites/changeSiteSalt.js";
import { deleteSite } from "./api/sites/deleteSite.js";
import { getSite } from "./api/sites/getSite.js";
import { getSiteHasData } from "./api/sites/getSiteHasData.js";
@@ -39,7 +40,7 @@ import { initializeClickhouse } from "./db/clickhouse/clickhouse.js";
import { allowList, loadAllowedDomains } from "./lib/allowedDomains.js";
import { mapHeaders } from "./lib/auth-utils.js";
import { auth } from "./lib/auth.js";
-import { publicSites } from "./lib/publicSites.js";
+import { siteConfig } from "./lib/siteConfig.js";
import { trackEvent } from "./tracker/trackEvent.js";
import { extractSiteId, isSitePublic } from "./utils.js";
@@ -208,6 +209,7 @@ server.delete("/report/:reportId", deleteReport);
server.post("/add-site", addSite);
server.post("/change-site-domain", changeSiteDomain);
server.post("/change-site-public", changeSitePublic);
+server.post("/change-site-salt", changeSiteSalt);
server.post("/delete-site/:id", deleteSite);
server.get("/get-sites", getSites);
server.get("/get-site/:id", getSite);
@@ -239,8 +241,8 @@ const start = async () => {
await Promise.all([initializeClickhouse()]);
await loadAllowedDomains();
- // Load public sites cache
- await publicSites.loadPublicSites();
+ // Load site configurations cache
+ await siteConfig.loadSiteConfigs();
// Start the server
await server.listen({ port: 3001, host: "0.0.0.0" });
diff --git a/server/src/lib/publicSites.ts b/server/src/lib/publicSites.ts
deleted file mode 100644
index 22171f57c..000000000
--- a/server/src/lib/publicSites.ts
+++ /dev/null
@@ -1,75 +0,0 @@
-import { db } from "../db/postgres/postgres.js";
-import { sites } from "../db/postgres/schema.js";
-
-class PublicSites {
- private publicSitesMap: Map<number, boolean> = new Map();
- private initialized: boolean = false;
-
- async loadPublicSites() {
- try {
- const allSites = await db
- .select({
- siteId: sites.siteId,
- public: sites.public,
- })
- .from(sites);
-
- // Reset the map
- this.publicSitesMap.clear();
-
- // Populate the map with site IDs and their public status
- for (const site of allSites) {
- this.publicSitesMap.set(site.siteId, site.public || false);
- }
-
- this.initialized = true;
- console.log(
- `Loaded ${this.publicSitesMap.size} sites into public sites cache`
- );
- } catch (error) {
- console.error("Error loading public sites:", error);
- this.initialized = false;
- }
- }
-
- /**
- * Check if a site is public without hitting the database
- */
- isSitePublic(siteId: string | number): boolean {
- const numericSiteId = Number(siteId);
- return this.publicSitesMap.get(numericSiteId) || false;
- }
-
- /**
- * Update the public status of a site in the cache
- */
- updateSitePublicStatus(siteId: number, isPublic: boolean): void {
- this.publicSitesMap.set(siteId, isPublic);
- }
-
- /**
- * Add a new site to the cache
- */
- addSite(siteId: number, isPublic: boolean): void {
- this.publicSitesMap.set(siteId, isPublic);
- }
-
- /**
- * Remove a site from the cache
- */
- removeSite(siteId: number): void {
- this.publicSitesMap.delete(siteId);
- }
-
- /**
- * Ensure the cache is initialized
- */
- async ensureInitialized() {
- if (!this.initialized) {
- await this.loadPublicSites();
- }
- }
-}
-
-// Singleton instance
-export const publicSites = new PublicSites();
diff --git a/server/src/lib/siteConfig.ts b/server/src/lib/siteConfig.ts
new file mode 100644
index 000000000..1b6e4d8c1
--- /dev/null
+++ b/server/src/lib/siteConfig.ts
@@ -0,0 +1,112 @@
+import { db } from "../db/postgres/postgres.js";
+import { sites } from "../db/postgres/schema.js";
+
+// Site configuration interface
+interface SiteConfigData {
+ public: boolean;
+ saltUserIds: boolean;
+}
+
+class SiteConfig {
+ private siteConfigMap: Map<number, SiteConfigData> = new Map();
+ private initialized: boolean = false;
+
+ async loadSiteConfigs() {
+ try {
+ const allSites = await db
+ .select({
+ siteId: sites.siteId,
+ public: sites.public,
+ saltUserIds: sites.saltUserIds,
+ })
+ .from(sites);
+
+ // Reset the map
+ this.siteConfigMap.clear();
+
+ // Populate the map with site IDs and their configuration
+ for (const site of allSites) {
+ this.siteConfigMap.set(site.siteId, {
+ public: site.public || false,
+ saltUserIds: site.saltUserIds || false,
+ });
+ }
+
+ this.initialized = true;
+ console.log(
+ `Loaded ${this.siteConfigMap.size} sites into site config cache`
+ );
+ } catch (error) {
+ console.error("Error loading site configurations:", error);
+ this.initialized = false;
+ }
+ }
+
+ /**
+ * Check if a site is public without hitting the database
+ */
+ isSitePublic(siteId: string | number): boolean {
+ const numericSiteId = Number(siteId);
+ const config = this.siteConfigMap.get(numericSiteId);
+ return config?.public || false;
+ }
+
+ /**
+ * Check if a site has user ID salting enabled
+ */
+ shouldSaltUserIds(siteId: string | number): boolean {
+ const numericSiteId = Number(siteId);
+ const config = this.siteConfigMap.get(numericSiteId);
+ return config?.saltUserIds || false;
+ }
+
+ /**
+ * Update the public status of a site in the cache
+ */
+ updateSitePublicStatus(siteId: number, isPublic: boolean): void {
+ const config = this.siteConfigMap.get(siteId) || {
+ public: false,
+ saltUserIds: false,
+ };
+ config.public = isPublic;
+ this.siteConfigMap.set(siteId, config);
+ }
+
+ /**
+ * Update the salt user IDs setting of a site in the cache
+ */
+ updateSiteSaltSetting(siteId: number, saltUserIds: boolean): void {
+ const config = this.siteConfigMap.get(siteId) || {
+ public: false,
+ saltUserIds: false,
+ };
+ config.saltUserIds = saltUserIds;
+ this.siteConfigMap.set(siteId, config);
+ }
+
+ /**
+ * Add a new site to the cache
+ */
+ addSite(siteId: number, config: SiteConfigData): void {
+ this.siteConfigMap.set(siteId, config);
+ }
+
+ /**
+ * Remove a site from the cache
+ */
+ removeSite(siteId: number): void {
+ this.siteConfigMap.delete(siteId);
+ }
+
+ /**
+ * Ensure the cache is initialized
+ */
+ async ensureInitialized() {
+ if (!this.initialized) {
+ await this.loadSiteConfigs();
+ }
+ }
+}
+
+// Singleton instance
+export const siteConfig = new SiteConfig();
diff --git a/server/src/tracker/trackingUtils.ts b/server/src/tracker/trackingUtils.ts
index 31b575558..3b44ede96 100644
--- a/server/src/tracker/trackingUtils.ts
+++ b/server/src/tracker/trackingUtils.ts
@@ -8,6 +8,7 @@ import { db } from "../db/postgres/postgres.js";
import { activeSessions } from "../db/postgres/schema.js";
import { TrackingPayload } from "../types.js";
import { trackingPayloadSchema } from "./trackEvent.js";
+import { siteConfig } from "../lib/siteConfig.js";
// Define extended payload types
export type BaseTrackingPayload = TrackingPayload & {
@@ -98,7 +99,8 @@ export function createBasePayload(
): TotalTrackingPayload {
const userAgent = request.headers["user-agent"] || "";
const ipAddress = getIpAddress(request);
- const userId = getUserId(ipAddress, userAgent);
+ const siteId = validatedBody.site_id;
+ const userId = getUserId(ipAddress, userAgent, siteId);
return {
...validatedBody,
@@ -156,11 +158,35 @@ function getDailySalt(): string {
return newSalt;
}
-function getUserId(ip: string, userAgent: string): string {
- const dailySalt = getDailySalt(); // Get the salt for the current day
+/**
+ * Generate a user ID based on IP and user agent
+ * If the site has salting enabled, also includes a daily rotating salt
+ *
+ * @param ip User's IP address
+ * @param userAgent User's user agent string
+ * @param siteId The site ID to check for salting configuration
+ * @returns A sha256 hash to identify the user
+ */
+function getUserId(
+ ip: string,
+ userAgent: string,
+ siteId?: string | number
+): string {
+ // Only apply salt if the site has salting enabled
+ if (siteId && siteConfig.shouldSaltUserIds(siteId)) {
+ console.info("salted");
+ const dailySalt = getDailySalt(); // Get the salt for the current day
+ return crypto
+ .createHash("sha256")
+ .update(ip + userAgent + dailySalt)
+ .digest("hex");
+ }
+
+ console.info("not salted");
+ // Otherwise, just hash IP and user agent
return crypto
.createHash("sha256")
- .update(ip + userAgent + dailySalt)
+ .update(ip + userAgent)
.digest("hex");
}
diff --git a/server/src/utils.ts b/server/src/utils.ts
index ee801f128..76a39dd48 100644
--- a/server/src/utils.ts
+++ b/server/src/utils.ts
@@ -1,4 +1,4 @@
-import { publicSites } from "./lib/publicSites.js";
+import { siteConfig } from "./lib/siteConfig.js";
export function getDeviceType(
screenWidth: number,
@@ -48,11 +48,11 @@ export function getDeviceType(
// Check if a site is public
export const isSitePublic = async (siteId: string | number) => {
try {
- // Ensure the publicSites cache is initialized
- await publicSites.ensureInitialized();
+ // Ensure the siteConfig cache is initialized
+ await siteConfig.ensureInitialized();
// Use the cached value
- return publicSites.isSitePublic(siteId);
+ return siteConfig.isSitePublic(siteId);
} catch (err) {
console.error("Error checking if site is public:", err);
return false;