The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
prevent unauthorized deletes
Steven Teyf7cd23121c08
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.tsindex 2f28dcf9b2..99a6868baa 100644--- a/apps/web/app/api/links/bulk/route.ts+++ b/apps/web/app/api/links/bulk/route.ts@@ -5,7 +5,7 @@ import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";-import { isStored, storage } from "@/lib/storage";+import { storage } from "@/lib/storage";import { NewLinkProps, ProcessedLinkProps } from "@/lib/types";import {bulkCreateLinksBodySchema,@@ -251,8 +251,11 @@ export const PATCH = withWorkspace(async ({ req, workspace, headers }) => {waitUntil(Promise.allSettled(links.map(async (link) => {- // delete old proxy image urls if exist- if (link.image && isStored(link.image)) {+ // delete old proxy image urls if exist and match the link ID+ if (+ link.image &&+ link.image.startsWith(`${R2_URL}/images/${link.id}`)+ ) {storage.delete(link.image.replace(`${R2_URL}/`, ""));}}),diff --git a/apps/web/lib/api/domains/delete-domain-links.ts b/apps/web/lib/api/domains/delete-domain-links.tsindex 9a8c5363bd..70fed26417 100644--- a/apps/web/lib/api/domains/delete-domain-links.ts+++ b/apps/web/lib/api/domains/delete-domain-links.ts@@ -2,7 +2,7 @@ import { prisma } from "@/lib/prisma";import { redis } from "@/lib/upstash";import { R2_URL } from "@dub/utils";import { waitUntil } from "@vercel/functions";-import { isStored, storage } from "../../storage";+import { storage } from "../../storage";import { recordLink } from "../../tinybird";import { removeDomainFromVercel } from "./remove-domain-vercel";@@ -65,7 +65,10 @@ export async function deleteDomainAndLinks(domain: string) {})),]),...allLinks.map((link) => {- if (link.image && isStored(link.image)) {+ if (+ link.image &&+ link.image.startsWith(`${R2_URL}/images/${link.id}`)+ ) {storage.delete(link.image.replace(`${R2_URL}/`, ""));}}),diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.tsindex 02db4ae287..fdef2803ee 100644--- a/apps/web/lib/api/links/create-link.ts+++ b/apps/web/lib/api/links/create-link.ts@@ -101,16 +101,9 @@ export async function createLink(link: ProcessedLinkProps) {workspace_id: response.projectId,created_at: response.createdAt,}),- /* Upload image to R2 and update the link with the uploaded image URL when:- - proxy is enabled- - image is set and:- - image is not stored in R2- or- - image URL does not start with the R2 URL + image ID (could be duplicate link flow)- */- ...(proxy &&- image &&- (!isStored(image) || !image.startsWith(`${R2_URL}/images/${response.id}`))+ // Upload image to R2 and update the link with the uploaded image URL when+ // proxy is enabled and image is set and not stored in R2+ ...(proxy && image && !isStored(image)? [// upload image to R2storage.upload(`images/${response.id}`, image, {diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.tsindex 885c2cc9bc..3b8390bd8a 100644--- a/apps/web/lib/api/links/delete-link.ts+++ b/apps/web/lib/api/links/delete-link.ts@@ -1,5 +1,5 @@import { prisma } from "@/lib/prisma";-import { isStored, storage } from "@/lib/storage";+import { storage } from "@/lib/storage";import { recordLink } from "@/lib/tinybird";import { redis } from "@/lib/upstash";import { R2_URL } from "@dub/utils";@@ -16,9 +16,9 @@ export async function deleteLink(linkId: string) {});waitUntil(Promise.allSettled([- // if the image is stored in Cloudflare R2, delete it+ // if there's a valid image and it has the same link ID, delete itlink.image &&- isStored(link.image) &&+ link.image.startsWith(`${R2_URL}/images/${link.id}`) &&storage.delete(link.image.replace(`${R2_URL}/`, "")),redis.hdel(link.domain.toLowerCase(), link.key.toLowerCase()),recordLink({diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.tsindex 9f720e8c5d..61925c6213 100644--- a/apps/web/lib/api/links/update-link.ts+++ b/apps/web/lib/api/links/update-link.ts@@ -141,9 +141,9 @@ export async function updateLink({width: 1200,height: 630,}),- // if old link image is stored in R2, delete it+ // if there's a valid old image and it has the same link ID, delete itoldLink.image &&- isStored(oldLink.image) &&+ oldLink.image.startsWith(`${R2_URL}/images/${id}`) &&storage.delete(oldLink.image.replace(`${R2_URL}/`, "")),]),);diff --git a/apps/web/lib/api/workspaces.ts b/apps/web/lib/api/workspaces.tsindex ece699304d..7964dccb88 100644--- a/apps/web/lib/api/workspaces.ts+++ b/apps/web/lib/api/workspaces.ts@@ -93,8 +93,8 @@ export async function deleteWorkspace(})),),// remove all images from R2- ...defaultDomainLinks.map(({ image }) =>- image && isStored(image)+ ...defaultDomainLinks.map(({ id, image }) =>+ image && image.startsWith(`${R2_URL}/images/${id}`)? storage.delete(image.replace(`${R2_URL}/`, "")): Promise.resolve(),),