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.

Migrate 5 more rate limits to assertRateLimit

Cursor Agentfe7e9660a596
diff --git a/apps/web/app/api/analytics/export/route.ts b/apps/web/app/api/analytics/export/route.ts
index a144ab8dce..e5fcabe42d 100644
--- a/apps/web/app/api/analytics/export/route.ts
+++ b/apps/web/app/api/analytics/export/route.ts
@@ -14,7 +14,8 @@ import { prefixWorkspaceId } from "@/lib/api/workspaces/workspace-id";
import { withWorkspace } from "@/lib/auth";
import { verifyFolderAccess } from "@/lib/folder/permissions";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
-import { ratelimit } from "@/lib/upstash";
+import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";
+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";
import { parseAnalyticsQuery } from "@/lib/zod/schemas/analytics";
export const maxDuration = 300;
@@ -22,17 +23,10 @@ export const maxDuration = 300;
// GET /api/analytics/export – get export data for analytics
export const GET = withWorkspace(
async ({ searchParams, workspace, session }) => {
- const { success } = await ratelimit(1, "30 s").limit(
- `analyticsExport:${workspace.id}`,
- );
-
- if (!success) {
- throw new DubApiError({
- code: "rate_limit_exceeded",
- message:
- "Analytics export is limited to once every 30 seconds. Please try again shortly.",
- });
- }
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.analyticsExport,
+ identifier: workspace.id,
+ });
throwIfClicksUsageExceeded(workspace);
diff --git a/apps/web/app/api/domains/[domain]/transfer/route.ts b/apps/web/app/api/domains/[domain]/transfer/route.ts
index 967b8b3ca2..4ff384c6ea 100644
--- a/apps/web/app/api/domains/[domain]/transfer/route.ts
+++ b/apps/web/app/api/domains/[domain]/transfer/route.ts
@@ -4,7 +4,8 @@ import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { qstash } from "@/lib/cron";
import { prisma } from "@/lib/prisma";
-import { ratelimit } from "@/lib/upstash";
+import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";
+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";
import { transferDomainBodySchema } from "@/lib/zod/schemas/domains";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -35,16 +36,10 @@ export const POST = withWorkspace(
}
// Allow up to 5 domain transfer per workspace per hour
- const { success } = await ratelimit(5, "1 h").limit(
- `domain-transfer:${workspace.id}`,
- );
-
- if (!success) {
- throw new DubApiError({
- code: "rate_limit_exceeded",
- message: "Too many requests. Please try again later.",
- });
- }
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.domainTransfer,
+ identifier: workspace.id,
+ });
const newWorkspace = await prisma.project.findUnique({
where: { id: newWorkspaceId },
diff --git a/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts b/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
index 44aa8e721f..4c26595f01 100644
--- a/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
+++ b/apps/web/lib/actions/partners/retry-failed-paypal-payouts.ts
@@ -3,7 +3,8 @@
import { throwIfNoPermission } from "@/lib/auth/partner-users/throw-if-no-permission";
import { createPayPalBatchPayout } from "@/lib/paypal/create-batch-payout";
import { prisma } from "@/lib/prisma";
-import { ratelimit } from "@/lib/upstash";
+import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";
+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";
import { nanoid } from "@dub/utils";
import * as z from "zod/v4";
import { authPartnerActionClient } from "../safe-action";
@@ -34,15 +35,10 @@ export const retryFailedPaypalPayoutsAction = authPartnerActionClient
throw new Error("Connect your PayPal account to enable payouts.");
}
- const { success } = await ratelimit(1, "12 h").limit(
- `retry-failed-paypal-payouts:${payoutId}`,
- );
-
- if (!success) {
- throw new Error(
- "You've reached the maximum number of retry attempts for the past 24 hours. Please wait and try again later.",
- );
- }
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.retryFailedPaypalPayout,
+ identifier: payoutId,
+ });
// Use a transaction to atomically check and update the payout status
// This prevents race conditions where multiple retry requests happen concurrently
diff --git a/apps/web/lib/actions/partners/start-identity-verification.ts b/apps/web/lib/actions/partners/start-identity-verification.ts
index 45b8489af9..d77164ff5e 100644
--- a/apps/web/lib/actions/partners/start-identity-verification.ts
+++ b/apps/web/lib/actions/partners/start-identity-verification.ts
@@ -1,9 +1,9 @@
"use server";
-import { shouldApplyRateLimit } from "@/lib/api/environment";
import { throwIfNoPermission } from "@/lib/auth/partner-users/throw-if-no-permission";
import { prisma } from "@/lib/prisma";
-import { ratelimit } from "@/lib/upstash/ratelimit";
+import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";
+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";
import { createVeriffSession } from "@/lib/veriff/create-veriff-session";
import {
mergeVeriffMetadata,
@@ -59,18 +59,10 @@ export const startIdentityVerificationAction = authPartnerActionClient.action(
};
}
- // Rate limit check
- if (shouldApplyRateLimit) {
- const { success } = await ratelimit(1, "1 h").limit(
- `identityVerification:${partner.id}`,
- );
-
- if (!success) {
- throw new Error(
- "Too many verification attempts. Please try again later.",
- );
- }
- }
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.identityVerificationStart,
+ identifier: partner.id,
+ });
// Create a new session
const { verification } = await createVeriffSession({
diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.ts
index e5c6c19c4e..2c32fd43e2 100644
--- a/apps/web/lib/actions/partners/update-partner-profile.ts
+++ b/apps/web/lib/actions/partners/update-partner-profile.ts
@@ -14,7 +14,8 @@ import {
} from "@/lib/partners/sync-partner-identity";
import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
-import { ratelimit } from "@/lib/upstash";
+import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";
+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";
import { partnerProfileChangeHistoryLogSchema } from "@/lib/zod/schemas/partner-profile";
import {
MAX_PARTNER_DESCRIPTION_LENGTH,
@@ -112,17 +113,10 @@ export const updatePartnerProfileAction = authPartnerActionClient
}
if (username && username !== partner.username) {
- const { success } = await ratelimit(5, "1 h").limit(
- `partner-profile:username-update:${partner.id}`,
- );
-
- if (!success) {
- throw new DubApiError({
- code: "rate_limit_exceeded",
- message:
- "You've updated your username too many times. Please try again later.",
- });
- }
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.partnerUsernameUpdate,
+ identifier: partner.id,
+ });
if (!validSlugRegex.test(username) || RESERVED_SLUGS.includes(username)) {
throw new Error("Invalid username");
diff --git a/apps/web/lib/upstash/ratelimit-policies.ts b/apps/web/lib/upstash/ratelimit-policies.ts
index a20069f8d7..6a37300db5 100644
--- a/apps/web/lib/upstash/ratelimit-policies.ts
+++ b/apps/web/lib/upstash/ratelimit-policies.ts
@@ -134,4 +134,34 @@ export const RATELIMIT_POLICIES = {
window: "1 h",
keyPrefix: "rl:partner:social:verification",
},
+
+ partnerUsernameUpdate: {
+ attempts: 5,
+ window: "1 h",
+ keyPrefix: "rl:partner:profile:username-update",
+ },
+
+ domainTransfer: {
+ attempts: 5,
+ window: "1 h",
+ keyPrefix: "rl:domains:transfer",
+ },
+
+ analyticsExport: {
+ attempts: 1,
+ window: "30 s",
+ keyPrefix: "rl:analytics:export",
+ },
+
+ retryFailedPaypalPayout: {
+ attempts: 1,
+ window: "12 h",
+ keyPrefix: "rl:partner:paypal:retry-failed-payout",
+ },
+
+ identityVerificationStart: {
+ attempts: 1,
+ window: "1 h",
+ keyPrefix: "rl:partner:identity:verification:start",
+ },
} as const satisfies Record<string, RatelimitPolicy>;