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.

restrict embed write routes to active enrollments

Pedro Ladeira3faab00f119a
diff --git a/apps/web/app/(ee)/api/embed/referrals/links/[linkId]/route.ts b/apps/web/app/(ee)/api/embed/referrals/links/[linkId]/route.ts
index 192c3deff3..a6340df3fc 100644
--- a/apps/web/app/(ee)/api/embed/referrals/links/[linkId]/route.ts
+++ b/apps/web/app/(ee)/api/embed/referrals/links/[linkId]/route.ts
@@ -7,8 +7,8 @@ import { prisma } from "@/lib/prisma";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { linkEventSchema } from "@/lib/zod/schemas/links";
import {
+ ACTIVE_ENROLLMENT_STATUSES,
createPartnerLinkSchema,
- INACTIVE_ENROLLMENT_STATUSES,
} from "@/lib/zod/schemas/partners";
import { ReferralsEmbedLinkSchema } from "@/lib/zod/schemas/referrals-embed";
import { getPrettyUrl } from "@dub/utils";
@@ -18,17 +18,18 @@ import { NextResponse } from "next/server";
// PATCH /api/embed/referrals/links/[linkId] - update a link for a partner
export const PATCH = withReferralsEmbedToken(
async ({ req, params, programEnrollment, program, links, group }) => {
- const { url, key } = createPartnerLinkSchema
- .pick({ url: true, key: true })
- .parse(await parseRequestBody(req));
-
- if (INACTIVE_ENROLLMENT_STATUSES.includes(programEnrollment.status)) {
+ if (!ACTIVE_ENROLLMENT_STATUSES.includes(programEnrollment.status)) {
throw new DubApiError({
code: "forbidden",
- message: `You are ${programEnrollment.status} from this program hence cannot create links.`,
+ message:
+ "You cannot update links in this program because your enrollment is not active.",
});
}
+ const { url, key } = createPartnerLinkSchema
+ .pick({ url: true, key: true })
+ .parse(await parseRequestBody(req));
+
const link = links.find((link) => link.id === params.linkId);
if (!link) {
diff --git a/apps/web/app/(ee)/api/embed/referrals/links/route.ts b/apps/web/app/(ee)/api/embed/referrals/links/route.ts
index 4de3158943..f6156114fd 100644
--- a/apps/web/app/(ee)/api/embed/referrals/links/route.ts
+++ b/apps/web/app/(ee)/api/embed/referrals/links/route.ts
@@ -7,7 +7,10 @@ import { withReferralsEmbedToken } from "@/lib/embed/referrals/auth";
import { prisma } from "@/lib/prisma";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { linkEventSchema } from "@/lib/zod/schemas/links";
-import { createPartnerLinkSchema } from "@/lib/zod/schemas/partners";
+import {
+ ACTIVE_ENROLLMENT_STATUSES,
+ createPartnerLinkSchema,
+} from "@/lib/zod/schemas/partners";
import { ReferralsEmbedLinkSchema } from "@/lib/zod/schemas/referrals-embed";
import { getUTMParamsFromURL } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
@@ -23,17 +26,18 @@ export const GET = withReferralsEmbedToken(async ({ links }) => {
// POST /api/embed/referrals/links – create links for a partner
export const POST = withReferralsEmbedToken(
async ({ req, programEnrollment, program, links, group }) => {
- const { url, key } = createPartnerLinkSchema
- .pick({ url: true, key: true })
- .parse(await parseRequestBody(req));
-
- if (["banned", "deactivated"].includes(programEnrollment.status)) {
+ if (!ACTIVE_ENROLLMENT_STATUSES.includes(programEnrollment.status)) {
throw new DubApiError({
code: "forbidden",
- message: `You are ${programEnrollment.status} from this program hence cannot create links.`,
+ message:
+ "You cannot create links in this program because your enrollment is not active.",
});
}
+ const { url, key } = createPartnerLinkSchema
+ .pick({ url: true, key: true })
+ .parse(await parseRequestBody(req));
+
if (!program.domain || !program.url) {
throw new DubApiError({
code: "bad_request",
diff --git a/apps/web/app/(ee)/api/embed/referrals/tremendous/send-otp/route.ts b/apps/web/app/(ee)/api/embed/referrals/tremendous/send-otp/route.ts
index 740c8f10f4..2364e85afb 100644
--- a/apps/web/app/(ee)/api/embed/referrals/tremendous/send-otp/route.ts
+++ b/apps/web/app/(ee)/api/embed/referrals/tremendous/send-otp/route.ts
@@ -11,6 +11,7 @@ import {
} from "@/lib/tremendous/constants";
import { ratelimit, redis } from "@/lib/upstash";
import { emailSchema } from "@/lib/zod/schemas/auth";
+import { ACTIVE_ENROLLMENT_STATUSES } from "@/lib/zod/schemas/partners";
import { sendEmail } from "@dub/email";
import PartnerTremendousVerifyEmail from "@dub/email/templates/partner-tremendous-verify-email";
import { TREMENDOUS_SUPPORTED_COUNTRIES } from "@dub/utils";
@@ -24,6 +25,14 @@ const sendOtpSchema = z.object({
// POST /api/embed/referrals/tremendous/send-otp
export const POST = withReferralsEmbedToken(
async ({ req, programEnrollment }) => {
+ if (!ACTIVE_ENROLLMENT_STATUSES.includes(programEnrollment.status)) {
+ throw new DubApiError({
+ code: "forbidden",
+ message:
+ "You cannot set up payouts because your enrollment in this program is not active.",
+ });
+ }
+
if (!TREMENDOUS_ENABLED_PROGRAM_IDS.includes(programEnrollment.programId)) {
throw new DubApiError({
code: "forbidden",
diff --git a/apps/web/app/(ee)/api/embed/referrals/tremendous/verify-otp/route.ts b/apps/web/app/(ee)/api/embed/referrals/tremendous/verify-otp/route.ts
index d0f0058218..0591fa3088 100644
--- a/apps/web/app/(ee)/api/embed/referrals/tremendous/verify-otp/route.ts
+++ b/apps/web/app/(ee)/api/embed/referrals/tremendous/verify-otp/route.ts
@@ -9,6 +9,7 @@ import {
} from "@/lib/tremendous/constants";
import { ratelimit, redis } from "@/lib/upstash";
import { emailSchema } from "@/lib/zod/schemas/auth";
+import { ACTIVE_ENROLLMENT_STATUSES } from "@/lib/zod/schemas/partners";
import { TREMENDOUS_SUPPORTED_COUNTRIES } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -22,6 +23,14 @@ const verifyOtpSchema = z.object({
// POST /api/embed/referrals/tremendous/verify-otp
export const POST = withReferralsEmbedToken(
async ({ req, programEnrollment }) => {
+ if (!ACTIVE_ENROLLMENT_STATUSES.includes(programEnrollment.status)) {
+ throw new DubApiError({
+ code: "forbidden",
+ message:
+ "You cannot set up payouts because your enrollment in this program is not active.",
+ });
+ }
+
if (!TREMENDOUS_ENABLED_PROGRAM_IDS.includes(programEnrollment.programId)) {
throw new DubApiError({
code: "forbidden",