Skip to content

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

Enhance CORS handling and add origin normalization

Bill Yange0a8634a352a
diff --git a/server/src/index.ts b/server/src/index.ts
index 78ce28801..c0e9e77dc 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -49,7 +49,7 @@ import { mapHeaders } from "./lib/auth-utils.js";
import { auth } from "./lib/auth.js";
import { siteConfig } from "./lib/siteConfig.js";
import { trackEvent } from "./tracker/trackEvent.js";
-import { extractSiteId, isSitePublic } from "./utils.js";
+import { extractSiteId, isSitePublic, normalizeOrigin } from "./utils.js";
// Import Stripe handlers
import { createCheckoutSession } from "./api/stripe/createCheckoutSession.js";
@@ -74,7 +74,7 @@ const server = Fastify({
server.register(cors, {
origin: (origin, callback) => {
- if (!origin || allowList.includes(origin)) {
+ if (!origin || allowList.includes(normalizeOrigin(origin))) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"), false);
diff --git a/server/src/lib/allowedDomains.ts b/server/src/lib/allowedDomains.ts
index 75e2178cf..6e765bf48 100644
--- a/server/src/lib/allowedDomains.ts
+++ b/server/src/lib/allowedDomains.ts
@@ -29,7 +29,7 @@ export const loadAllowedDomains = async () => {
allowList = [
"http://localhost:3002",
process.env.BASE_URL || "",
- ...domains.map(({ domain }) => `https://${domain}`),
+ ...domains.map(({ domain }) => domain),
];
initAuth(allowList);
} catch (error) {
diff --git a/server/src/utils.ts b/server/src/utils.ts
index 76a39dd48..953197cb2 100644
--- a/server/src/utils.ts
+++ b/server/src/utils.ts
@@ -74,3 +74,12 @@ export const extractSiteId = (path: string) => {
}
return null;
};
+
+export const normalizeOrigin = (url: string) => {
+ try {
+ const hostname = new URL(url).hostname;
+ return hostname.replace(/^www\./, "");
+ } catch {
+ return url;
+ }
+};