#4576·dub

Domain Transfer Leaves `linksUsage` & `totalLinks` Permanently Desynchronized

Author: codeCraft-RitikCreated Sep 28, 2026Updated Sep 29, 2026

️ Metadata

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:

typescript
// 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 publishWorkspaceLinksUsageEvent or atomically decrements Project.totalLinks (see bulkDeleteLinks and deleteLink).

However, during domain transfer:

  1. Source Workspace (currentWorkspaceId): linksUsage and totalLinks are never decremented. If 1,000 links are transferred away, the source workspace remains permanently charged for those 1,000 links. This triggers premature linksUsage >= linksLimit locks and automated warning emails.
  2. Destination Workspace (newWorkspaceId): linksUsage and totalLinks are never incremented. The destination workspace receives 1,000 links with 0 added to its usage counters.
  3. 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

  1. Workspace A (with 200 links on customdomain.com) initiates a domain transfer to Workspace B.
  2. The transfer cron finishes migrating all 200 links.
  3. Query prisma.project.findUnique for Workspace A and Workspace B:
    • Notice Workspace A's linksUsage and totalLinks are completely unchanged.
    • Notice Workspace B's linksUsage has increased by 0, despite now holding 200 active links.

️ 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:

diff
--- 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(),
+        }),
       ]);