The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fix r2 delete bug
Steven Tey6a3726ca367c
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.tsindex 60a917d0a0..0822bce742 100644--- a/apps/web/app/api/links/bulk/route.ts+++ b/apps/web/app/api/links/bulk/route.ts@@ -7,6 +7,7 @@ import {} from "@/lib/api/links";import { bulkDeleteLinks } from "@/lib/api/links/bulk-delete-links";import { bulkUpdateLinks } from "@/lib/api/links/bulk-update-links";+import { getOwnedLinkImageKey } from "@/lib/api/links/get-owned-link-image-key";import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";import { includeTags } from "@/lib/api/links/include-tags";import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";@@ -28,7 +29,6 @@ import {bulkCreateLinksBodySchema,bulkUpdateLinksBodySchema,} from "@/lib/zod/schemas/links";-import { R2_URL } from "@dub/utils";import { waitUntil } from "@vercel/functions";import { NextResponse } from "next/server";@@ -568,15 +568,12 @@ export const PATCH = withWorkspace((async () => {if (data.proxy && data.image) {await Promise.allSettled(- links.map(async (link) => {- // delete old proxy image urls if exist and match the link ID- if (- link.image &&- link.image.startsWith(`${R2_URL}/images/${link.id}`) &&- link.image !== data.image- ) {- storage.delete({ key: link.image.replace(`${R2_URL}/`, "") });+ links.map((link) => {+ const key = getOwnedLinkImageKey(link.image, link.id);+ if (key && link.image !== data.image) {+ return storage.delete({ key });}+ return null;}),);}diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.tsindex 9230d59958..150afe7391 100644--- a/apps/web/lib/api/links/bulk-delete-links.ts+++ b/apps/web/lib/api/links/bulk-delete-links.ts@@ -1,9 +1,10 @@+import { getOwnedLinkImageKey } from "@/lib/api/links/get-owned-link-image-key";import { queuePartnerSearchSyncForLinks } from "@/lib/api/partners/queue-partner-search-sync";import { deleteDiscountCodes } from "@/lib/discounts/delete-discount-code";import { prisma } from "@/lib/prisma";import { storage } from "@/lib/storage";import { recordLink } from "@/lib/tinybird";-import { chunk, R2_URL } from "@dub/utils";+import { chunk } from "@dub/utils";import { waitUntil } from "@vercel/functions";import { linkCache } from "./cache";import { ExpandedLink } from "./utils";@@ -54,13 +55,10 @@ export async function bulkDeleteLinks(recordLink(links, { deleted: true }),// For links that have an image, delete the image from R2- ...links- .filter((link) =>- link.image?.startsWith(`${R2_URL}/images/${link.id}`),- )- .map((link) =>- storage.delete({ key: link.image!.replace(`${R2_URL}/`, "") }),- ),+ ...links.flatMap((link) => {+ const key = getOwnedLinkImageKey(link.image, link.id);+ return key ? [storage.delete({ key })] : [];+ }),// Queue an index update if any of the deleted links are associated with a partner// The program enrollments outlives them, so the document is re-serialized without them.diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.tsindex dad9719fd9..a0f9c685e5 100644--- a/apps/web/lib/api/links/delete-link.ts+++ b/apps/web/lib/api/links/delete-link.ts@@ -1,9 +1,9 @@+import { getOwnedLinkImageKey } from "@/lib/api/links/get-owned-link-image-key";import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";import { deleteDiscountCodes } from "@/lib/discounts/delete-discount-code";import { prisma } from "@/lib/prisma";import { storage } from "@/lib/storage";import { recordLink } from "@/lib/tinybird";-import { R2_URL } from "@dub/utils";import { waitUntil } from "@vercel/functions";import { linkCache } from "./cache";import { includeProgramEnrollment } from "./include-program-enrollment";@@ -36,12 +36,12 @@ export async function deleteLink(linkId: string) {},});+ const imageKey = getOwnedLinkImageKey(link.image, link.id);+waitUntil(Promise.allSettled([- // if there's a valid image and it has the same link ID, delete it- link.image &&- link.image.startsWith(`${R2_URL}/images/${link.id}`) &&- storage.delete({ key: link.image.replace(`${R2_URL}/`, "") }),+ // Delete the preview image only when it is this link's own object.+ imageKey && storage.delete({ key: imageKey }),// Remove the link from RedislinkCache.delete(link),diff --git a/apps/web/lib/api/links/get-owned-link-image-key.ts b/apps/web/lib/api/links/get-owned-link-image-key.tsnew file mode 100644index 0000000000..156428f481--- /dev/null+++ b/apps/web/lib/api/links/get-owned-link-image-key.ts@@ -0,0 +1,52 @@+import {+ isSafeStorageKey,+ urlPathHasRelativeSegments,+} from "@/lib/storage/assert-safe-storage-key";+import { R2_URL } from "@dub/utils";++const LINK_IMAGE_NONCE = /^[A-Za-z0-9_-]{1,32}$/;++/**+ * Object key for a link preview image stored by us.+ * Returns null unless the URL is exactly `images/{linkId}` or+ * `images/{linkId}_{nonce}` on the asset host. The key is rebuilt from the+ * link id — it is never sliced out of the client-supplied URL.+ */+export function getOwnedLinkImageKey(+ image: string | null | undefined,+ linkId: string,+): string | null {+ if (!image || !linkId) return null;+ if (urlPathHasRelativeSegments(image)) return null;+ if (!isSafeStorageKey(`images/${linkId}`)) return null;++ let imageUrl: URL;+ let assetUrl: URL;+ try {+ imageUrl = new URL(image);+ assetUrl = new URL(R2_URL);+ } catch {+ return null;+ }++ if (imageUrl.origin !== assetUrl.origin) return null;+ if (imageUrl.username || imageUrl.password) return null;++ const basePath = assetUrl.pathname.replace(/\/$/, "");+ const prefix = `${basePath}/images/${linkId}`;+ const { pathname } = imageUrl;++ let key: string | null = null;++ if (pathname === prefix) {+ key = `images/${linkId}`;+ } else if (pathname.startsWith(`${prefix}_`)) {+ const nonce = pathname.slice(prefix.length + 1);+ if (LINK_IMAGE_NONCE.test(nonce)) {+ key = `images/${linkId}_${nonce}`;+ }+ }++ if (!key || !isSafeStorageKey(key)) return null;+ return key;+}diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.tsindex 365726b5c5..d4c18264de 100644--- a/apps/web/lib/api/links/update-link.ts+++ b/apps/web/lib/api/links/update-link.ts@@ -1,3 +1,4 @@+import { getOwnedLinkImageKey } from "@/lib/api/links/get-owned-link-image-key";import { queuePartnerSearchSyncForLinks } from "@/lib/api/partners/queue-partner-search-sync";import { getPartnerEnrollmentInfo } from "@/lib/planetscale/get-partner-enrollment-info";import { prisma } from "@/lib/prisma";@@ -175,6 +176,11 @@ export async function updateLink({},});+ const previousImageKey =+ oldLink.image && oldLink.image !== image+ ? getOwnedLinkImageKey(oldLink.image, id)+ : null;+waitUntil((async () => {const { partner, discount } = await getPartnerEnrollmentInfo({@@ -228,11 +234,8 @@ export async function updateLink({},}),- // If there's a valid old image and it starts with the same link ID but is different from the new image, delete it- oldLink.image &&- oldLink.image.startsWith(`${R2_URL}/images/${id}`) &&- oldLink.image !== image &&- storage.delete({ key: oldLink.image.replace(`${R2_URL}/`, "") }),+ // Delete the previous preview image when it belongs to this link and is being replaced.+ previousImageKey && storage.delete({ key: previousImageKey }),// Schedule AB test completionchangedTestCompletedAt &&diff --git a/apps/web/lib/messages/utils.ts b/apps/web/lib/messages/utils.tsindex 3d93c63427..edfb71054c 100644--- a/apps/web/lib/messages/utils.ts+++ b/apps/web/lib/messages/utils.ts@@ -35,12 +35,19 @@ export function sortMessageAttachments<);}-// Normalize a user-supplied file name+// Normalize a user-supplied file name into one storage-key segment.export function sanitizeFileName(name: string): string {- return name+ const sanitized = name.trim().replace(/\s+/g, " ")- .replace(/[\x00-\x1F\x7F/\\?#%]/g, (ch) => encodeURIComponent(ch));+ .replace(/[/\\]/g, "-")+ .replace(/[\x00-\x1F\x7F?#%]/g, (ch) => encodeURIComponent(ch));++ if (sanitized === "" || sanitized === "." || sanitized === "..") {+ return "_";+ }++ return sanitized;}export function isPreviewableImageType(mimeType: string): boolean {diff --git a/apps/web/lib/storage.ts b/apps/web/lib/storage.tsindex 2cdea37e9e..ff3e72c07b 100644--- a/apps/web/lib/storage.ts+++ b/apps/web/lib/storage.ts@@ -1,4 +1,5 @@import { isPrivateIp } from "@/lib/api/is-private-ip";+import { assertSafeStorageKey } from "@/lib/storage/assert-safe-storage-key";import { OG_AVATAR_URL, R2_URL, fetchWithTimeout } from "@dub/utils";import { AwsClient } from "aws4fetch";import { promises as dns } from "dns";@@ -60,15 +61,14 @@ class StorageClient {headers["Content-Type"] = opts.contentType;}+ const requestUrl = this.objectRequestUrl(key, bucket);+try {- const response = await this.client.fetch(- `${process.env.STORAGE_ENDPOINT}/${this._getBucketName(bucket)}/${key}`,- {- method: "PUT",- headers,- body: uploadBody,- },- );+ const response = await this.client.fetch(requestUrl, {+ method: "PUT",+ headers,+ body: uploadBody,+ });if (!response.ok) {throw new Error(response.statusText);@@ -90,13 +90,12 @@ class StorageClient {key: string;bucket?: BucketType;}) {+ const requestUrl = this.objectRequestUrl(key, bucket);+try {- const response = await this.client.fetch(- `${process.env.STORAGE_ENDPOINT}/${this._getBucketName(bucket)}/${key}`,- {- method: "DELETE",- },- );+ const response = await this.client.fetch(requestUrl, {+ method: "DELETE",+ });if (!response.ok) {throw new Error(response.statusText);@@ -110,7 +109,7 @@ class StorageClient {// Fetch object metadata (Content-Type, size, eTag) without downloading the bodyasync head({ key, bucket = "public" }: { key: string; bucket?: BucketType }) {const response = await this.client.fetch(- `${process.env.STORAGE_ENDPOINT}/${this._getBucketName(bucket)}/${key}`,+ this.objectRequestUrl(key, bucket),{method: "HEAD",},@@ -148,7 +147,7 @@ class StorageClient {length?: number;}) {const response = await this.client.fetch(- `${process.env.STORAGE_ENDPOINT}/${this._getBucketName(bucket)}/${key}`,+ this.objectRequestUrl(key, bucket),{method: "GET",headers: {@@ -183,9 +182,7 @@ class StorageClient {expiresIn: number;headers?: Record<string, string>;}) {- const url = new URL(- `${process.env.STORAGE_ENDPOINT}/${this._getBucketName(bucket)}/${key}`,- );+ const url = new URL(this.objectRequestUrl(key, bucket));url.searchParams.set("X-Amz-Expires", String(expiresIn));@@ -364,6 +361,12 @@ class StorageClient {return blob;}+ private objectRequestUrl(key: string, bucket: BucketType) {+ assertSafeStorageKey(key);++ return `${process.env.STORAGE_ENDPOINT}/${this._getBucketName(bucket)}/${key}`;+ }+private _getBucketName(bucket: BucketType) {if (bucket === "public") {const bucketName = process.env.STORAGE_PUBLIC_BUCKET;diff --git a/apps/web/lib/storage/assert-safe-storage-key.ts b/apps/web/lib/storage/assert-safe-storage-key.tsnew file mode 100644index 0000000000..bae1a931a5--- /dev/null+++ b/apps/web/lib/storage/assert-safe-storage-key.ts@@ -0,0 +1,116 @@+const MAX_STORAGE_KEY_LENGTH = 1024;++/**+ * Rejects object keys that the S3 signer would resolve to a different path.+ * `..`, encoded dots, and a leading slash all pass a prefix check and are+ * then normalised by `Request` before the signature is computed.+ */+export function assertSafeStorageKey(key: string): void {+ if (!isSafeStorageKey(key)) {+ throw new Error("Invalid storage key.");+ }+}++export function isSafeStorageKey(key: string): boolean {+ if (typeof key !== "string") return false;+ if (key.length === 0 || key.length > MAX_STORAGE_KEY_LENGTH) return false;+ if (key.startsWith("/") || key.endsWith("/")) return false;+ if (key.includes("\\") || key.includes("//")) return false;+ if (/[\u0000-\u001F\u007F?#]/.test(key)) return false;++ const segments = key.split("/");+ if (segments.some((segment) => !isSafePathSegment(segment))) return false;++ try {+ return resolvedPathname(key) === encodedPathname(key);+ } catch {+ return false;+ }+}++/** True when an http(s) URL path contains a relative segment. Non-URLs are ignored. */+export function urlPathHasRelativeSegments(value: string): boolean {+ const trimmed = value.trim();+ if (!/^https?:\/\//i.test(trimmed)) return false;++ const originMatch = trimmed.match(/^https?:\/\/[^/?#]*/i);+ if (!originMatch) return true;++ const rawPath = trimmed.slice(originMatch[0].length).split(/[?#]/)[0] ?? "";+ if (rawPath.includes("\\") || rawPath.includes("\0")) return true;++ return rawPath+ .split("/")+ .filter((segment) => segment.length > 0)+ .some((segment) => !isSafePathSegment(segment));+}++function isSafePathSegment(segment: string): boolean {+ if (segment.length === 0) return false;+ if (isDotSegment(segment)) return false;+ return !decodesToTraversal(segment);+}++function isDotSegment(segment: string): boolean {+ return [segment, segment.normalize("NFKC")].some((form) => {+ const dots = form.replace(/%2e/gi, ".");+ return dots === "." || dots === "..";+ });+}++// Repeated decoding catches `%2e%2e`, `%252e%252e`, and `%2f`-smuggled `..`.+// A decoded `%25` is a literal `%`, so decoding stops once no `%HH` remains.+function decodesToTraversal(segment: string): boolean {+ try {+ decodeURIComponent(segment);+ } catch {+ return true;+ }++ let current = segment;++ for (let depth = 0; depth < 3; depth++) {+ if (containsTraversal(current)) return true;+ if (!/%[0-9a-f]{2}/i.test(current)) return false;++ let decoded: string;+ try {+ decoded = decodeURIComponent(current);+ } catch {+ return true;+ }++ if (decoded === current) return false;+ current = decoded;+ }++ return containsTraversal(current);+}++function containsTraversal(value: string): boolean {+ if (value.includes("\0") || isDotSegment(value)) return true;+ if (!value.includes("/") && !value.includes("\\")) return false;++ return value+ .split(/[/\\]/)+ .some((part) => part.length === 0 || isDotSegment(part));+}++function resolvedPathname(key: string): string {+ return new URL(`https://storage.invalid/${key}`).pathname;+}++function encodedPathname(key: string): string {+ const url = new URL("https://storage.invalid");+ url.pathname =+ "/" ++ key+ .split("/")+ .map((segment) => {+ const part = new URL("https://storage.invalid");+ part.pathname = `/${segment}`;+ return part.pathname.slice(1);+ })+ .join("/");+ return url.pathname;+}diff --git a/apps/web/lib/zod/schemas/images.ts b/apps/web/lib/zod/schemas/images.tsindex 2651ad4917..2386d83f5c 100644--- a/apps/web/lib/zod/schemas/images.ts+++ b/apps/web/lib/zod/schemas/images.ts@@ -1,3 +1,4 @@+import { urlPathHasRelativeSegments } from "@/lib/storage/assert-safe-storage-key";import { GOOGLE_FAVICON_URL, R2_URL } from "@dub/utils";import { fileTypeFromBuffer } from "file-type";import * as z from "zod/v4";@@ -58,6 +59,9 @@ export const storedR2ImageUrlSchema = z.trim().refine((url) => url.startsWith(R2_URL), {message: `URL must start with ${R2_URL}`,+ })+ .refine((url) => !urlPathHasRelativeSegments(url), {+ message: "Image URL must not contain relative path segments.",});// Google favicon URL schema - supports URLs starting with GOOGLE_FAVICON_URL@@ -93,7 +97,10 @@ export const publicHostedImageSchema = zerror:"Image must be a publicly accessible http:// or https:// URL (localhost and IP addresses aren't allowed).",})- .trim();+ .trim()+ .refine((url) => !urlPathHasRelativeSegments(url), {+ message: "Image URL must not contain relative path segments.",+ });/** Coerce unusable preview strings (e.g. data:favicons) to null before create/update link validation. */export function preprocessLinkPreviewImage(diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex f79ac5bc0e..784fd3ae3c 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -1,4 +1,5 @@import { ErrorCode } from "@/lib/api/error-codes";+import { urlPathHasRelativeSegments } from "@/lib/storage/assert-safe-storage-key";import { DUB_FOUNDING_DATE, formatDate, validDomainRegex } from "@dub/utils";import * as z from "zod/v4";import {@@ -433,6 +434,9 @@ export const createLinkBodySchema = z.object({image: z.string().nullish()+ .refine((value) => value == null || !urlPathHasRelativeSegments(value), {+ message: "Image URL must not contain relative path segments.",+ }).describe("The custom link preview image (og:image). Will be used for Custom Link Previews if `proxy` is true. Learn more: https://d.to/og",),diff --git a/apps/web/tests/misc/storage-key.test.ts b/apps/web/tests/misc/storage-key.test.tsnew file mode 100644index 0000000000..af95f9e951--- /dev/null+++ b/apps/web/tests/misc/storage-key.test.ts@@ -0,0 +1,130 @@+import { getOwnedLinkImageKey } from "@/lib/api/links/get-owned-link-image-key";+import { sanitizeFileName } from "@/lib/messages/utils";+import { storage } from "@/lib/storage";+import {+ assertSafeStorageKey,+ isSafeStorageKey,+ urlPathHasRelativeSegments,+} from "@/lib/storage/assert-safe-storage-key";+import { publicHostedImageSchema } from "@/lib/zod/schemas/images";+import {+ createLinkBodySchema,+ createLinkBodySchemaAsync,+} from "@/lib/zod/schemas/links";+import { R2_URL } from "@dub/utils";+import { describe, expect, it } from "vitest";++const linkId = "link_1M4W0XN11M227PH3DNYRH6H3Q";++describe("isSafeStorageKey", () => {+ it.each([+ "images/link_1M4W0XN11M227PH3DNYRH6H3Q",+ "images/link_1M4W0XN11M227PH3DNYRH6H3Q_abc1234",+ "messages/prog_123/file.pdf",+ "messages/prog_123/100%25.pdf",+ "messages/prog_123/foo%2Fbar.pdf",+ "messages/prog_123/my file.png",+ "resumes/ZbOEyTdkAe",+ ])("accepts %s", (key) => {+ expect(isSafeStorageKey(key)).toBe(true);+ });++ it.each([+ "images/link_1/../../resumes/ZbOEyTdkAe",+ "images/%2e%2e/secret",+ "images/%2E%2E/%2e%2e/secret",+ "images/%252e%252e/secret",+ "images/..%2f..%2fsecret",+ "/images/link_1",+ "images//link_1",+ "images/./link_1",+ "images/link_1/",+ "images/foo\\..\\..\\secret",+ "../other-bucket/secret",+ "",+ ])("rejects %s", (key) => {+ expect(isSafeStorageKey(key)).toBe(false);+ expect(() => assertSafeStorageKey(key)).toThrow("Invalid storage key.");+ });+});++describe("getOwnedLinkImageKey", () => {+ it("returns the key for this link's uploaded image", () => {+ expect(getOwnedLinkImageKey(`${R2_URL}/images/${linkId}`, linkId)).toBe(+ `images/${linkId}`,+ );+ expect(+ getOwnedLinkImageKey(`${R2_URL}/images/${linkId}_abc1234`, linkId),+ ).toBe(`images/${linkId}_abc1234`);+ });++ it("rejects a relative path that still starts with the link prefix", () => {+ expect(+ getOwnedLinkImageKey(+ `${R2_URL}/images/${linkId}/../../resumes/ZbOEyTdkAe`,+ linkId,+ ),+ ).toBeNull();+ });++ it.each([+ `${R2_URL}/images/${linkId}/%2e%2e/%2e%2e/resumes/ZbOEyTdkAe`,+ `${R2_URL}/images/${linkId}\\..\\..\\resumes\\ZbOEyTdkAe`,+ `${R2_URL}/resumes/ZbOEyTdkAe`,+ `${R2_URL}/images/link_someoneelse`,+ "https://example.com/og.png",+ `${R2_URL}/images/${linkId}/extra`,+ ])("rejects %s", (image) => {+ expect(getOwnedLinkImageKey(image, linkId)).toBeNull();+ });+});++describe("link image schema", () => {+ const traversal = `${R2_URL}/images/${linkId}/../../resumes/ZbOEyTdkAe`;++ it("rejects relative segments in hosted image URLs", () => {+ expect(urlPathHasRelativeSegments(traversal)).toBe(true);+ expect(urlPathHasRelativeSegments("https://example.com/og.png")).toBe(+ false,+ );+ expect(publicHostedImageSchema.safeParse(traversal).success).toBe(false);+ expect(+ publicHostedImageSchema.safeParse("https://example.com/og.png").success,+ ).toBe(true);+ });++ it("rejects the traversal image on create and update schemas", async () => {+ expect(+ createLinkBodySchema.safeParse({+ url: "https://example.com",+ image: traversal,+ }).success,+ ).toBe(false);++ await expect(+ createLinkBodySchemaAsync.parseAsync({+ url: "https://example.com",+ image: traversal,+ }),+ ).rejects.toThrow(/relative path segments/);+ });+});++describe("storage client", () => {+ it("refuses to delete a key the signer would resolve elsewhere", async () => {+ await expect(+ storage.delete({ key: "images/link_1/../../resumes/ZbOEyTdkAe" }),+ ).rejects.toThrow("Invalid storage key.");+ });+});++describe("sanitizeFileName", () => {+ it("keeps a slashy or relative file name inside one safe segment", () => {+ expect(+ isSafeStorageKey(`messages/prog/${sanitizeFileName("../../secret.pdf")}`),+ ).toBe(true);+ expect(sanitizeFileName("../../secret.pdf")).toBe("..-..-secret.pdf");+ expect(sanitizeFileName("..")).toBe("_");+ expect(sanitizeFileName("100%.pdf")).toBe("100%25.pdf");+ });+});