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.

add verifyAnalyticsAllowedHostnames for track/**/client endopints

Steven Tey859ad7fb2227
diff --git a/apps/web/app/(ee)/api/track/lead/client/route.ts b/apps/web/app/(ee)/api/track/lead/client/route.ts
index 162efee2fc..03ad1093c6 100644
--- a/apps/web/app/(ee)/api/track/lead/client/route.ts
+++ b/apps/web/app/(ee)/api/track/lead/client/route.ts
@@ -1,5 +1,10 @@
+import {
+ getHostnameFromRequest,
+ verifyAnalyticsAllowedHostnames,
+} from "@/lib/analytics/verify-analytics-allowed-hostnames";
import { trackLead } from "@/lib/api/conversions/track-lead";
import { COMMON_CORS_HEADERS } from "@/lib/api/cors";
+import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withPublishableKey } from "@/lib/auth/publishable-key";
import { trackLeadRequestSchema } from "@/lib/zod/schemas/leads";
@@ -10,6 +15,18 @@ export const POST = withPublishableKey(
async ({ req, workspace }) => {
const body = await parseRequestBody(req);
+ const allowRequest = verifyAnalyticsAllowedHostnames({
+ allowedHostnames: (workspace?.allowedHostnames ?? []) as string[],
+ req,
+ });
+
+ if (!allowRequest) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: `Request origin '${getHostnameFromRequest(req)}' is not included in the allowed hostnames for this workspace. Update your allowed hostnames here: https://app.dub.co/settings/analytics`,
+ });
+ }
+
const {
clickId,
eventName,
diff --git a/apps/web/app/(ee)/api/track/sale/client/route.ts b/apps/web/app/(ee)/api/track/sale/client/route.ts
index 8e3ab90fa4..3821996406 100644
--- a/apps/web/app/(ee)/api/track/sale/client/route.ts
+++ b/apps/web/app/(ee)/api/track/sale/client/route.ts
@@ -1,3 +1,7 @@
+import {
+ getHostnameFromRequest,
+ verifyAnalyticsAllowedHostnames,
+} from "@/lib/analytics/verify-analytics-allowed-hostnames";
import { trackSale } from "@/lib/api/conversions/track-sale";
import { COMMON_CORS_HEADERS } from "@/lib/api/cors";
import { DubApiError } from "@/lib/api/errors";
@@ -11,6 +15,18 @@ export const POST = withPublishableKey(
async ({ req, workspace }) => {
const body = await parseRequestBody(req);
+ const allowRequest = verifyAnalyticsAllowedHostnames({
+ allowedHostnames: (workspace?.allowedHostnames ?? []) as string[],
+ req,
+ });
+
+ if (!allowRequest) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: `Request origin '${getHostnameFromRequest(req)}' is not included in the allowed hostnames for this workspace. Update your allowed hostnames here: https://app.dub.co/settings/analytics`,
+ });
+ }
+
let {
customerExternalId,
paymentProcessor,
diff --git a/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
index b91d184b03..2911782bb0 100644
--- a/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
+++ b/apps/web/lib/analytics/verify-analytics-allowed-hostnames.ts
@@ -25,7 +25,7 @@ export const verifyAnalyticsAllowedHostnames = ({
const hostname = getHostnameFromRequest(req);
if (!hostname) {
- console.log("Click not recorded ❌ – No hostname found in request.", {
+ console.log("Event not recorded ❌ – No hostname found in request.", {
allowedHostnames,
});
return false;
@@ -49,7 +49,7 @@ export const verifyAnalyticsAllowedHostnames = ({
}
console.log(
- `Click not recorded ❌ – Hostname ${hostname} does not match any allowed patterns.`,
+ `Event not recorded ❌ – Hostname ${hostname} does not match any allowed patterns.`,
{
allowedHostnames,
},