Skip to content

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

Remove loadAllowedDomains function and related references to streamline site management

Bill Yang47cccf6a81e6
diff --git a/server/src/api/sites/addSite.ts b/server/src/api/sites/addSite.ts
index 5681f58cc..3f1e93035 100644
--- a/server/src/api/sites/addSite.ts
+++ b/server/src/api/sites/addSite.ts
@@ -1,10 +1,8 @@
-import { FastifyReply, FastifyRequest } from "fastify";
import { randomBytes } from "crypto";
+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 { getSessionFromReq } from "../../lib/auth-utils.js";
-import { siteConfig } from "../../lib/siteConfig.js";
export async function addSite(
request: FastifyRequest<{
@@ -67,7 +65,7 @@ export async function addSite(
}
// Generate a random 12-character hex ID
- const id = randomBytes(6).toString('hex');
+ const id = randomBytes(6).toString("hex");
// Create the new site
const newSite = await db
@@ -84,11 +82,6 @@ export async function addSite(
})
.returning();
- // Update allowed domains
- await loadAllowedDomains();
-
- // No need to update cache as we're querying directly now
-
return reply.status(201).send(newSite[0]);
} catch (error) {
console.error("Error adding site:", error);
diff --git a/server/src/api/sites/changeSiteDomain.ts b/server/src/api/sites/changeSiteDomain.ts
index 06689ab28..39f26073a 100644
--- a/server/src/api/sites/changeSiteDomain.ts
+++ b/server/src/api/sites/changeSiteDomain.ts
@@ -2,7 +2,6 @@ import { eq } from "drizzle-orm";
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 { getUserHasAdminAccessToSite } from "../../lib/auth-utils.js";
import { siteConfig } from "../../lib/siteConfig.js";
@@ -38,7 +37,6 @@ export async function changeSiteDomain(
return reply.status(404).send({ error: "Site not found" });
}
- await loadAllowedDomains();
siteConfig.updateConfig(siteId, { domain: newDomain });
return reply.status(200).send({ message: "Domain updated successfully" });
diff --git a/server/src/api/sites/deleteSite.ts b/server/src/api/sites/deleteSite.ts
index e3c8b161c..eea108d1a 100644
--- a/server/src/api/sites/deleteSite.ts
+++ b/server/src/api/sites/deleteSite.ts
@@ -1,9 +1,5 @@
-import { eq } from "drizzle-orm";
import { FastifyReply, FastifyRequest } from "fastify";
import { clickhouse } from "../../db/clickhouse/clickhouse.js";
-import { db } from "../../db/postgres/postgres.js";
-import { sites } from "../../db/postgres/schema.js";
-import { loadAllowedDomains } from "../../lib/allowedDomains.js";
import { getUserHasAdminAccessToSite } from "../../lib/auth-utils.js";
import { siteConfig } from "../../lib/siteConfig.js";
@@ -15,13 +11,10 @@ export async function deleteSite(request: FastifyRequest<{ Params: { id: string
return reply.status(403).send({ error: "Forbidden" });
}
- await db.delete(sites).where(eq(sites.siteId, Number(id)));
await clickhouse.command({
query: `DELETE FROM events WHERE site_id = ${id}`,
});
- await loadAllowedDomains();
- // Remove the site from the siteConfig cache
siteConfig.removeSite(Number(id));
return reply.status(200).send({ success: true });
diff --git a/server/src/index.ts b/server/src/index.ts
index 1f36df264..448c67008 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -67,7 +67,6 @@ import { getUserOrganizations } from "./api/user/getUserOrganizations.js";
import { listOrganizationMembers } from "./api/user/listOrganizationMembers.js";
import { initializeClickhouse } from "./db/clickhouse/clickhouse.js";
import { initPostgres } from "./db/postgres/initPostgres.js";
-import { loadAllowedDomains } from "./lib/allowedDomains.js";
import { getSessionFromReq, mapHeaders } from "./lib/auth-utils.js";
import { auth } from "./lib/auth.js";
import { IS_CLOUD } from "./lib/const.js";
@@ -149,12 +148,6 @@ const server = Fastify({
server.register(cors, {
origin: (_origin, callback) => {
callback(null, true);
-
- // if (!origin || allowList.includes(normalizeOrigin(origin))) {
- // callback(null, true);
- // } else {
- // callback(new Error("Not allowed by CORS"), false);
- // }
},
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"],
allowedHeaders: ["Content-Type", "Authorization", "X-Requested-With"],
@@ -415,7 +408,7 @@ server.get("/api/health", { logLevel: "silent" }, (_, reply) => reply.send("OK")
const start = async () => {
try {
console.info("Starting server...");
- await Promise.all([initializeClickhouse(), loadAllowedDomains(), initPostgres()]);
+ await Promise.all([initializeClickhouse(), initPostgres()]);
telemetryService.startTelemetryCron();
diff --git a/server/src/lib/allowedDomains.ts b/server/src/lib/allowedDomains.ts
deleted file mode 100644
index 57172574d..000000000
--- a/server/src/lib/allowedDomains.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-import { db, sql } from "../db/postgres/postgres.js";
-import { sites } from "../db/postgres/schema.js";
-import { normalizeOrigin } from "../utils.js";
-import { initAuth } from "./auth.js";
-import dotenv from "dotenv";
-
-dotenv.config();
-
-export let allowList: string[] = [];
-
-export const loadAllowedDomains = async () => {
- try {
- // Check if the sites table exists
- const tableExists = await sql`
- SELECT EXISTS (
- SELECT FROM information_schema.tables
- WHERE table_schema = 'public'
- AND table_name = 'sites'
- );
- `;
-
- // Only query the sites table if it exists
- let domains: { domain: string }[] = [];
- if (tableExists[0].exists) {
- // Use Drizzle to get domains
- const sitesData = await db.select({ domain: sites.domain }).from(sites);
- domains = sitesData;
- }
-
- allowList = [
- "localhost",
- normalizeOrigin(process.env.BASE_URL || ""),
- ...domains.map(({ domain }) => normalizeOrigin(domain)),
- ];
- initAuth(allowList);
- } catch (error) {
- console.error("Error loading allowed domains:", error);
- // Set default values in case of error
- allowList = ["localhost", normalizeOrigin(process.env.BASE_URL || "")];
- initAuth(allowList);
- }
-};