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.

verifyAnalyticsAllowedHostnames

Steven Teybed444aa56eb
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.ts
index b94034d4f9..b99740c579 100644
--- a/apps/web/app/api/track/click/route.ts
+++ b/apps/web/app/api/track/click/route.ts
@@ -1,3 +1,4 @@
+import { verifyAnalyticsAllowedHostnames } from "@/lib/analytics/verify-analytics-allowed-hostnames";
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { conn } from "@/lib/planetscale/connection";
@@ -64,25 +65,7 @@ export const POST = withAxiom(
}
const allowedHostnames = link.allowedHostnames;
-
- if (allowedHostnames && allowedHostnames.length > 0) {
- const source = req.headers.get("referer") || req.headers.get("origin");
- const sourceUrl = source ? new URL(source) : null;
- const hostname = sourceUrl?.hostname.replace(/^www\./, "");
-
- if (!hostname || !allowedHostnames.includes(hostname)) {
- console.error("Hostname not allowed.", {
- hostname,
- allowedHostnames,
- });
- throw new DubApiError({
- code: "forbidden",
- message: `Hostname ${hostname} not included in allowed hostnames (${allowedHostnames.join(
- ", ",
- )}).`,
- });
- }
- }
+ verifyAnalyticsAllowedHostnames({ allowedHostnames, req });
const cacheKey = `recordClick:${link.id}:${ip}`;
let clickId = await redis.get<string>(cacheKey);
diff --git a/apps/web/app/api/track/visit/route.ts b/apps/web/app/api/track/visit/route.ts
index 14fef92473..fdf218d9a1 100644
--- a/apps/web/app/api/track/visit/route.ts
+++ b/apps/web/app/api/track/visit/route.ts
@@ -1,3 +1,4 @@
+import { verifyAnalyticsAllowedHostnames } from "@/lib/analytics/verify-analytics-allowed-hostnames";
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { linkCache } from "@/lib/api/links/cache";
import { includeTags } from "@/lib/api/links/include-tags";
@@ -53,9 +54,11 @@ export const POST = withAxiom(
});
}
- const linkKey = urlObj.pathname.slice(1);
+ let linkKey = urlObj.pathname.slice(1);
+ if (linkKey === "") {
+ linkKey = "_root";
+ }
- // might wanna cache this
const { rows } = await conn.execute<{
id: string;
url: string;
@@ -102,25 +105,7 @@ export const POST = withAxiom(
}
const allowedHostnames = link.allowedHostnames;
-
- if (allowedHostnames && allowedHostnames.length > 0) {
- const source = req.headers.get("referer") || req.headers.get("origin");
- const sourceUrl = source ? new URL(source) : null;
- const hostname = sourceUrl?.hostname.replace(/^www\./, "");
-
- if (!hostname || !allowedHostnames.includes(hostname)) {
- console.error("Hostname not allowed.", {
- hostname,
- allowedHostnames,
- });
- throw new DubApiError({
- code: "forbidden",
- message: `Hostname ${hostname} not included in allowed hostnames (${allowedHostnames.join(
- ", ",
- )}).`,
- });
- }
- }
+ verifyAnalyticsAllowedHostnames({ allowedHostnames, req });
const cacheKey = `recordClick:${link.id}:${ip}`;
let clickId = await redis.get<string>(cacheKey);
diff --git a/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
new file mode 100644
index 0000000000..369eb7a923
--- /dev/null
+++ b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
@@ -0,0 +1,28 @@
+import { DubApiError } from "../api/errors";
+
+export const verifyAnalyticsAllowedHostnames = ({
+ allowedHostnames,
+ req,
+}: {
+ allowedHostnames: string[];
+ req: Request;
+}) => {
+ if (allowedHostnames && allowedHostnames.length > 0) {
+ const source = req.headers.get("referer") || req.headers.get("origin");
+ const sourceUrl = source ? new URL(source) : null;
+ const hostname = sourceUrl?.hostname.replace(/^www\./, "");
+
+ if (!hostname || !allowedHostnames.includes(hostname)) {
+ console.error("Hostname not allowed.", {
+ hostname,
+ allowedHostnames,
+ });
+ throw new DubApiError({
+ code: "forbidden",
+ message: `Hostname ${hostname} not included in allowed hostnames (${allowedHostnames.join(
+ ", ",
+ )}).`,
+ });
+ }
+ }
+};