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.

Prevent deleting root links via bulk (#4182)

Pedro Ladeiraa4e58328b12c
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 489fbe8826..b30cb32ff3 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -6,6 +6,7 @@ import {
updateLink,
} from "@/lib/api/links";
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
+import { isRootDomainLinkKey } from "@/lib/api/links/utils/is-root-domain-link-key";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { verifyFolderAccess } from "@/lib/folder/permissions";
@@ -212,6 +213,14 @@ export const DELETE = withWorkspace(
linkId: params.linkId,
});
+ if (isRootDomainLinkKey(link.key)) {
+ throw new DubApiError({
+ code: "forbidden",
+ message:
+ "You can't delete a custom domain link. You can delete the domain instead.",
+ });
+ }
+
if (link.folderId) {
await verifyFolderAccess({
workspace,
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 424d09c074..cd8dc09bbb 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -11,6 +11,7 @@ import { includeProgramEnrollment } from "@/lib/api/links/include-program-enroll
import { includeTags } from "@/lib/api/links/include-tags";
import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";
import { checkIfLinksHaveFolders } from "@/lib/api/links/utils/check-if-links-have-folders";
+import { isRootDomainLinkKey } from "@/lib/api/links/utils/is-root-domain-link-key";
import { combineTagIds } from "@/lib/api/tags/combine-tag-ids";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -572,6 +573,8 @@ export const DELETE = withWorkspace(
});
}
+ links = links.filter((link) => !isRootDomainLinkKey(link.key));
+
const { count: deletedCount } = await prisma.link.deleteMany({
where: {
id: { in: links.map((link) => link.id) },
diff --git a/apps/web/lib/api/links/utils/is-root-domain-link-key.ts b/apps/web/lib/api/links/utils/is-root-domain-link-key.ts
new file mode 100644
index 0000000000..3fd1c16c34
--- /dev/null
+++ b/apps/web/lib/api/links/utils/is-root-domain-link-key.ts
@@ -0,0 +1,2 @@
+export const isRootDomainLinkKey = (key: string) =>
+ ["_root", "akoJCU0="].includes(key);
diff --git a/apps/web/tests/links/bulk-delete-link.test.ts b/apps/web/tests/links/bulk-delete-link.test.ts
index 0c473ff944..2bb2409afa 100644
--- a/apps/web/tests/links/bulk-delete-link.test.ts
+++ b/apps/web/tests/links/bulk-delete-link.test.ts
@@ -1,5 +1,5 @@
-import { Link } from "@prisma/client";
-import { expect, test } from "vitest";
+import { Domain, Link } from "@prisma/client";
+import { expect, onTestFinished, test } from "vitest";
import { randomId } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK } from "../utils/resource";
@@ -42,3 +42,54 @@ test("DELETE /links/bulk", async (ctx) => {
expect(fetchedLinks.every((l) => l.status === 404)).toBeTruthy();
});
+
+test("DELETE /links/bulk - skips root links", async (ctx) => {
+ const h = new IntegrationHarness(ctx);
+ const { workspace, http } = await h.init();
+ const slug = `${randomId()}.dub-internal-test.com`;
+
+ await http.post<Domain>({
+ path: "/domains",
+ query: { workspaceId: workspace.id },
+ body: { slug },
+ });
+
+ onTestFinished(async () => {
+ await h.deleteDomain(slug);
+ });
+
+ const { data: rootLink } = await http.get<Link>({
+ path: "/links/info",
+ query: { workspaceId: workspace.id, domain: slug },
+ });
+
+ const { data: normalLink } = await http.post<Link>({
+ path: "/links",
+ body: {
+ url: `https://example.com/${randomId()}`,
+ domain: slug,
+ },
+ });
+
+ const { status, data } = await http.delete<{
+ deletedCount: number;
+ }>({
+ path: "/links/bulk",
+ query: {
+ linkIds: [rootLink.id, normalLink.id].join(","),
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(data.deletedCount).toEqual(1);
+
+ const { status: rootStatus } = await http.get({
+ path: `/links/${rootLink.id}`,
+ });
+ const { status: normalStatus } = await http.get({
+ path: `/links/${normalLink.id}`,
+ });
+
+ expect(rootStatus).toBe(200);
+ expect(normalStatus).toBe(404);
+});
diff --git a/apps/web/tests/links/delete-link.test.ts b/apps/web/tests/links/delete-link.test.ts
index 8643b37885..df1664d80c 100644
--- a/apps/web/tests/links/delete-link.test.ts
+++ b/apps/web/tests/links/delete-link.test.ts
@@ -1,5 +1,6 @@
-import { Link } from "@prisma/client";
-import { expect, test } from "vitest";
+import { Domain, Link } from "@prisma/client";
+import { expect, onTestFinished, test } from "vitest";
+import { randomId } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK } from "../utils/resource";
@@ -33,3 +34,44 @@ test("DELETE /links/{linkId}", async (ctx) => {
expect(status2).toBe(404);
});
+
+test("DELETE /links/{linkId} - cannot delete root link", async (ctx) => {
+ const h = new IntegrationHarness(ctx);
+ const { workspace, http } = await h.init();
+ const slug = `${randomId()}.dub-internal-test.com`;
+
+ await http.post<Domain>({
+ path: "/domains",
+ query: { workspaceId: workspace.id },
+ body: { slug },
+ });
+
+ onTestFinished(async () => {
+ await h.deleteDomain(slug);
+ });
+
+ const { data: rootLink } = await http.get<Link>({
+ path: "/links/info",
+ query: { workspaceId: workspace.id, domain: slug },
+ });
+
+ const { status, data } = await http.delete({
+ path: `/links/${rootLink.id}`,
+ });
+
+ expect(status).toBe(403);
+ expect(data).toStrictEqual({
+ error: {
+ code: "forbidden",
+ message:
+ "You can't delete a custom domain link. You can delete the domain instead.",
+ doc_url: "https://dub.co/docs/api-reference/errors#forbidden",
+ },
+ });
+
+ const { status: status2 } = await http.get({
+ path: `/links/${rootLink.id}`,
+ });
+
+ expect(status2).toBe(200);
+});
diff --git a/apps/web/ui/links/links-toolbar.tsx b/apps/web/ui/links/links-toolbar.tsx
index 58ea0fd033..45f5d7d24d 100644
--- a/apps/web/ui/links/links-toolbar.tsx
+++ b/apps/web/ui/links/links-toolbar.tsx
@@ -156,11 +156,13 @@ export const LinksToolbar = memo(
label: "Delete",
icon: Trash,
action: () => setShowDeleteLinkModal(true),
- disabledTooltip: selectedLinks.some(
- ({ programId, clicks }) => programId && clicks > 0,
- )
- ? "You can't delete partner links that have active stats on them."
- : undefined,
+ disabledTooltip: selectedLinks.some(({ key }) => key === "_root")
+ ? "You can't delete a custom domain link. You can delete the domain instead."
+ : selectedLinks.some(
+ ({ programId, clicks }) => programId && clicks > 0,
+ )
+ ? "You can't delete partner links that have active stats on them."
+ : undefined,
keyboardShortcut: "x",
},
],