Domain Transfer Leaves `linksUsage` & `totalLinks` Permanently Desynchronized
Author: codeCraft-RitikCreated Sep 28, 2026Updated Sep 29, 2026
️ Metadata
- Affected Files:
- Severity:
High (P1 - Data Corruption & Quota Bypass) - Category: Billing / Usage Quota Enforcement
Overview & Problem Statement
When a custom domain is transferred from one workspace to another (POST /api/domains/[domain]/transfer), the domain is updated to newWorkspaceId, and a background QStash job (/api/cron/domains/transfer) batches and reassigns all existing links to the target workspace:
// apps/web/app/(ee)/api/cron/domains/transfer/route.ts:54-76
await prisma.link.updateMany({
where: {
domain,
projectId: currentWorkspaceId,
id: { in: linkIds },
},
data: {
projectId: newWorkspaceId,
// reset all stats and folder
clicks: 0,
...
},
});Root Cause Analysis
Dub relies on cached counters linksUsage and totalLinks on the Project model to enforce plan tier limits (e.g. workspace.linksUsage >= workspace.linksLimit checked in apps/web/lib/api/links/usage-checks.ts).
Everywhere else in Dub:
- Link creation calls
publishWorkspaceLinksUsageEvent({ workspaceId, linksCount: +N }). - Link deletion calls
publishWorkspaceLinksUsageEventor atomically decrementsProject.totalLinks(seebulkDeleteLinksanddeleteLink).
However, during domain transfer:
- Source Workspace (
currentWorkspaceId):linksUsageandtotalLinksare never decremented. If 1,000 links are transferred away, the source workspace remains permanently charged for those 1,000 links. This triggers prematurelinksUsage >= linksLimitlocks and automated warning emails. - Destination Workspace (
newWorkspaceId):linksUsageandtotalLinksare never incremented. The destination workspace receives 1,000 links with0added to its usage counters. - Abuse Potential: A user can create thousands of links on an Enterprise or Pro workspace, transfer the domain to a Free tier workspace, and bypass all link creation quota limits on the Free workspace.
Steps to Reproduce
- Workspace A (with 200 links on
customdomain.com) initiates a domain transfer to Workspace B. - The transfer cron finishes migrating all 200 links.
- Query
prisma.project.findUniquefor Workspace A and Workspace B:- Notice Workspace A's
linksUsageandtotalLinksare completely unchanged. - Notice Workspace B's
linksUsagehas increased by 0, despite now holding 200 active links.
- Notice Workspace A's
️ Proposed Solution & Patch
In apps/web/app/(ee)/api/cron/domains/transfer/route.ts, when a batch of links is transferred, emit events to update linksUsage and totalLinks for both workspaces:
--- a/apps/web/app/(ee)/api/cron/domains/transfer/route.ts
+++ b/apps/web/app/(ee)/api/cron/domains/transfer/route.ts
@@ -6,6 +6,7 @@ import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { prisma } from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
+import { publishWorkspaceLinksUsageEvent } from "@/lib/upstash/redis-streams/workspace-links-usage";
import { APP_DOMAIN_WITH_NGROK, log, sleep } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -102,6 +103,20 @@ export async function POST(req: Request) {
folderId: null,
})),
),
+
+ // Reconcile linksUsage and totalLinks on both workspaces
+ publishWorkspaceLinksUsageEvent({
+ workspaceId: currentWorkspaceId,
+ linksCount: -linkIds.length,
+ timestamp: new Date().toISOString(),
+ }),
+ publishWorkspaceLinksUsageEvent({
+ workspaceId: newWorkspaceId,
+ linksCount: linkIds.length,
+ timestamp: new Date().toISOString(),
+ }),
]);Source: dubinc/dub