Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

getHostnameFromRequest

Steven Tey8577ef31a395
diff --git a/apps/web/app/(ee)/api/track/click/route.ts b/apps/web/app/(ee)/api/track/click/route.ts
index a9eca60b35..92d81f1b38 100644
--- a/apps/web/app/(ee)/api/track/click/route.ts
+++ b/apps/web/app/(ee)/api/track/click/route.ts
@@ -1,5 +1,8 @@
import { allowedHostnamesCache } from "@/lib/analytics/allowed-hostnames-cache";
-import { verifyAnalyticsAllowedHostnames } from "@/lib/analytics/verify-analytics-allowed-hostnames";
+import {
+ getHostnameFromRequest,
+ verifyAnalyticsAllowedHostnames,
+} from "@/lib/analytics/verify-analytics-allowed-hostnames";
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { linkCache } from "@/lib/api/links/cache";
import { recordClickCache } from "@/lib/api/links/record-click-cache";
@@ -109,7 +112,8 @@ export const POST = withAxiom(async (req: AxiomRequest) => {
includeDomains: true,
});
- cachedAllowedHostnames = workspace?.allowedHostnames as string[];
+ cachedAllowedHostnames = (workspace?.allowedHostnames ??
+ []) as string[];
waitUntil(
allowedHostnamesCache.mset({
@@ -127,7 +131,7 @@ export const POST = withAxiom(async (req: AxiomRequest) => {
if (!allowRequest) {
throw new DubApiError({
code: "forbidden",
- message: "Click tracking is not allowed from this referrer.",
+ message: `Request origin '${getHostnameFromRequest(req)}' is not included in the allowed hostnames for this workspace (${cachedAllowedHostnames.join(", ")}). Update your allowed hostnames here: https://app.dub.co/settings/analytics`,
});
}
diff --git a/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
index b1fc655863..8fc47c05b9 100644
--- a/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
+++ b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
@@ -1,3 +1,9 @@
+export const getHostnameFromRequest = (req: Request) => {
+ const source = req.headers.get("referer") || req.headers.get("origin");
+ const sourceUrl = source ? new URL(source) : null;
+ return sourceUrl?.hostname.replace(/^www\./, "");
+};
+
export const verifyAnalyticsAllowedHostnames = ({
allowedHostnames,
req,
@@ -10,9 +16,7 @@ export const verifyAnalyticsAllowedHostnames = ({
return true;
}
- const source = req.headers.get("referer") || req.headers.get("origin");
- const sourceUrl = source ? new URL(source) : null;
- const hostname = sourceUrl?.hostname.replace(/^www\./, "");
+ const hostname = getHostnameFromRequest(req);
if (!hostname) {
console.log("Click not recorded ❌ – No hostname found in request.", {