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.

fix domain ownership check

Kiran K5e079fabbbcb
diff --git a/apps/web/app/api/analytics/events/export/route.ts b/apps/web/app/api/analytics/events/export/route.ts
index e3f8f38fc0..8cc2a699f7 100644
--- a/apps/web/app/api/analytics/events/export/route.ts
+++ b/apps/web/app/api/analytics/events/export/route.ts
@@ -1,6 +1,5 @@
import { getEvents } from "@/lib/analytics/get-events";
import { convertToCSV, validDateRangeForPlan } from "@/lib/analytics/utils";
-import { throwIfDomainNotOwned } from "@/lib/api/domains/get-domain";
import { getLink } from "@/lib/api/links/get-link";
import { withWorkspace } from "@/lib/auth";
import { eventsQuerySchema } from "@/lib/zod/schemas/analytics";
@@ -52,10 +51,6 @@ export const GET = withWorkspace(
const { event, domain, interval, start, end, columns, key } = parsedParams;
- if (domain) {
- throwIfDomainNotOwned({ workspace, domain });
- }
-
const link =
domain && key ? await getLink({ workspace, domain, key }) : null;
diff --git a/apps/web/app/api/analytics/export/route.ts b/apps/web/app/api/analytics/export/route.ts
index 5e02043683..217003af99 100644
--- a/apps/web/app/api/analytics/export/route.ts
+++ b/apps/web/app/api/analytics/export/route.ts
@@ -1,7 +1,6 @@
import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import { getAnalytics } from "@/lib/analytics/get-analytics";
import { convertToCSV, validDateRangeForPlan } from "@/lib/analytics/utils";
-import { throwIfDomainNotOwned } from "@/lib/api/domains/get-domain";
import { getLink } from "@/lib/api/links/get-link";
import { withWorkspace } from "@/lib/auth";
import { analyticsQuerySchema } from "@/lib/zod/schemas/analytics";
@@ -14,10 +13,6 @@ export const GET = withWorkspace(
const { interval, start, end, linkId, domain, key } = parsedParams;
- if (domain) {
- throwIfDomainNotOwned({ workspace, domain });
- }
-
const link =
domain && key ? await getLink({ workspace, domain, key }) : null;
diff --git a/apps/web/app/api/analytics/route.ts b/apps/web/app/api/analytics/route.ts
index 585432eea7..95fae22ba0 100644
--- a/apps/web/app/api/analytics/route.ts
+++ b/apps/web/app/api/analytics/route.ts
@@ -1,7 +1,6 @@
import { VALID_ANALYTICS_ENDPOINTS } from "@/lib/analytics/constants";
import { getAnalytics } from "@/lib/analytics/get-analytics";
import { validDateRangeForPlan } from "@/lib/analytics/utils";
-import { throwIfDomainNotOwned } from "@/lib/api/domains/get-domain";
import { getLink } from "@/lib/api/links/get-link";
import { withWorkspace } from "@/lib/auth";
import {
@@ -36,10 +35,6 @@ export const GET = withWorkspace(
key,
} = parsedParams;
- if (domain) {
- throwIfDomainNotOwned({ workspace, domain });
- }
-
const shouldCheckLink = linkId || externalId || (domain && key);
const link = shouldCheckLink
? await getLink({ workspace: workspace, linkId, externalId, domain, key })
diff --git a/apps/web/app/api/domains/[domain]/primary/route.ts b/apps/web/app/api/domains/[domain]/primary/route.ts
index faf6bf67d3..20e4eb8ead 100644
--- a/apps/web/app/api/domains/[domain]/primary/route.ts
+++ b/apps/web/app/api/domains/[domain]/primary/route.ts
@@ -1,4 +1,3 @@
-import { throwIfDomainNotOwned } from "@/lib/api/domains/get-domain";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { DomainSchema } from "@/lib/zod/schemas/domains";
@@ -9,8 +8,6 @@ export const POST = withWorkspace(
async ({ headers, workspace, params }) => {
const { domain } = params;
- throwIfDomainNotOwned({ workspace, domain });
-
const [domainRecord] = await Promise.all([
prisma.domain.update({
where: {
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index df9c862ff8..e0b49468a9 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -4,7 +4,6 @@ import {
removeDomainFromVercel,
validateDomain,
} from "@/lib/api/domains";
-import { throwIfDomainNotOwned } from "@/lib/api/domains/get-domain";
import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -22,8 +21,6 @@ import { NextResponse } from "next/server";
export const GET = withWorkspace(async ({ workspace, params }) => {
const { domain } = params;
- throwIfDomainNotOwned({ workspace, domain });
-
const domainRecord = await prisma.domain.findUnique({
where: {
slug: domain,
@@ -46,8 +43,6 @@ export const PATCH = withWorkspace(
async ({ req, workspace, params }) => {
const { domain } = params;
- throwIfDomainNotOwned({ workspace, domain });
-
const payload = updateDomainBodySchema.parse(await parseRequestBody(req));
const { slug: newDomain, placeholder, expiredUrl, archived } = payload;
@@ -138,11 +133,9 @@ export const PATCH = withWorkspace(
// DELETE /api/domains/[domain] - delete a workspace's domain
export const DELETE = withWorkspace(
- async ({ workspace, params }) => {
+ async ({ params }) => {
const { domain } = params;
- throwIfDomainNotOwned({ workspace, domain });
-
await deleteDomainAndLinks(domain);
return NextResponse.json({ slug: domain });
diff --git a/apps/web/app/api/domains/[domain]/verify/route.ts b/apps/web/app/api/domains/[domain]/verify/route.ts
index 282b2ca451..5dbcbe33e9 100644
--- a/apps/web/app/api/domains/[domain]/verify/route.ts
+++ b/apps/web/app/api/domains/[domain]/verify/route.ts
@@ -3,7 +3,6 @@ import {
getDomainResponse,
verifyDomain,
} from "@/lib/api/domains";
-import { throwIfDomainNotOwned } from "@/lib/api/domains/get-domain";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { DomainVerificationStatusProps } from "@/lib/types";
@@ -12,11 +11,9 @@ import { NextResponse } from "next/server";
export const maxDuration = 30;
// GET /api/domains/[domain]/verify - get domain verification status
-export const GET = withWorkspace(async ({ workspace, params }) => {
+export const GET = withWorkspace(async ({ params }) => {
const { domain } = params;
- throwIfDomainNotOwned({ workspace, domain });
-
let status: DomainVerificationStatusProps = "Valid Configuration";
const [domainJson, configJson] = await Promise.all([
diff --git a/apps/web/lib/api/domains/get-domain.ts b/apps/web/lib/api/domains/get-domain.ts
index e2dbf474a3..54dc346efb 100644
--- a/apps/web/lib/api/domains/get-domain.ts
+++ b/apps/web/lib/api/domains/get-domain.ts
@@ -40,26 +40,26 @@ import { DubApiError } from "../errors";
// };
// Verify workspace domain ownership
-export const throwIfDomainNotOwned = ({
- workspace,
- domain,
-}: {
- workspace: WorkspaceProps;
- domain: string;
-}) => {
- if (isDubDomain(domain)) {
- if (workspace.id !== DUB_WORKSPACE_ID) {
- throw new DubApiError({
- code: "forbidden",
- message: "Domain does not belong to workspace.",
- });
- }
- }
+// export const throwIfDomainNotOwned = ({
+// workspace,
+// domain,
+// }: {
+// workspace: WorkspaceProps;
+// domain: string;
+// }) => {
+// if (isDubDomain(domain)) {
+// if (workspace.id !== DUB_WORKSPACE_ID) {
+// throw new DubApiError({
+// code: "forbidden",
+// message: "Domain does not belong to workspace.",
+// });
+// }
+// }
- if (!workspace.domains.find((d) => d.slug === domain)) {
- throw new DubApiError({
- code: "forbidden",
- message: "Domain does not belong to workspace.",
- });
- }
-};
\ No newline at end of file
+// if (!workspace.domains.find((d) => d.slug === domain)) {
+// throw new DubApiError({
+// code: "forbidden",
+// message: "Domain does not belong to workspace.",
+// });
+// }
+// };
\ No newline at end of file
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index 5f56f2ec88..878c065f35 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -6,7 +6,12 @@ import {
import { prisma } from "@/lib/prisma";
import { PlanProps, WorkspaceProps } from "@/lib/types";
import { ratelimit } from "@/lib/upstash";
-import { API_DOMAIN, getSearchParams } from "@dub/utils";
+import {
+ API_DOMAIN,
+ DUB_WORKSPACE_ID,
+ getSearchParams,
+ isDubDomain,
+} from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { isBetaTester } from "../edge-config";
import { hashToken } from "./hash-token";
@@ -47,6 +52,7 @@ export const withWorkspace = (
needNotExceededLinks, // if the action needs the user to not have exceeded their links usage
allowAnonymous, // special case for /api/links (POST /api/links) – allow no session
allowSelf, // special case for removing yourself from a workspace
+ domainChecks, // if the action needs to check if the domain belongs to the workspace
betaFeature, // if the action is a beta feature
}: {
requiredPlan?: Array<PlanProps>;
@@ -55,6 +61,7 @@ export const withWorkspace = (
needNotExceededLinks?: boolean;
allowAnonymous?: boolean;
allowSelf?: boolean;
+ domainChecks?: boolean;
betaFeature?: boolean;
} = {},
) => {
@@ -80,6 +87,7 @@ export const withWorkspace = (
apiKey = authorizationHeader.replace("Bearer ", "");
}
+ const domain = params?.domain || searchParams.domain;
let session: Session | undefined;
let workspaceId: string | undefined;
let workspaceSlug: string | undefined;
@@ -231,6 +239,25 @@ export const withWorkspace = (
}
}
+ // if domain is defined:
+ // - it's a dub domain and domainChecks is required, check if the user is part of the dub workspace
+ // - it's a custom domain, check if the domain belongs to the workspace
+ if (domain) {
+ if (isDubDomain(domain)) {
+ if (domainChecks && workspace.id !== DUB_WORKSPACE_ID) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "Domain does not belong to workspace.",
+ });
+ }
+ } else if (!workspace.domains.find((d) => d.slug === domain)) {
+ throw new DubApiError({
+ code: "forbidden",
+ message: "Domain does not belong to workspace.",
+ });
+ }
+ }
+
// workspace exists but user is not part of it
if (workspace.users.length === 0) {
const pendingInvites = await prisma.projectInvite.findUnique({