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.

Enahance the validation & rate limiting

Kiran Kdd0ebd3bbd98
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index aeea7eae1b..adcf26e4cd 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -74,7 +74,9 @@ export const PATCH = withWorkspace(
linkId: params.linkId,
});
- const body = updateLinkBodySchema.parse(await parseRequestBody(req)) || {};
+ const body =
+ (await updateLinkBodySchema.parseAsync(await parseRequestBody(req))) ||
+ {};
await Promise.all([
...(link.folderId
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index e8cd28748b..c63a8c0ca1 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -9,7 +9,7 @@ import { verifyFolderAccess } from "@/lib/folder/permissions";
import { ratelimit } from "@/lib/upstash";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
- createLinkBodySchema,
+ createLinkBodySchemaAsync,
getLinksQuerySchemaExtended,
linkEventSchema,
} from "@/lib/zod/schemas/links";
@@ -81,7 +81,11 @@ export const POST = withWorkspace(
throwIfLinksUsageExceeded(workspace);
}
- const body = createLinkBodySchema.parse(await parseRequestBody(req));
+ const body = await createLinkBodySchemaAsync.parseAsync(
+ await parseRequestBody(req),
+ );
+
+ console.log(body);
if (!session) {
const ip = req.headers.get("x-forwarded-for") || LOCALHOST_IP;
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 9a903ed292..cc19ddfcd9 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -6,7 +6,12 @@ import {
formatDate,
validDomainRegex,
} from "@dub/utils";
-import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
+import {
+ base64ImageSchema,
+ booleanQuerySchema,
+ getPaginationQuerySchema,
+ publicHostedImageSchema,
+} from "./misc";
import { TagSchema } from "./tags";
import {
parseDateSchema,
@@ -426,7 +431,11 @@ export const createLinkBodySchema = z.object({
.describe("The date and time when the tests were or will be completed."),
});
-export const updateLinkBodySchema = createLinkBodySchema.partial();
+export const createLinkBodySchemaAsync = createLinkBodySchema.extend({
+ image: z.union([base64ImageSchema, publicHostedImageSchema]),
+});
+
+export const updateLinkBodySchema = createLinkBodySchemaAsync.partial();
export const bulkCreateLinksBodySchema = z
.array(createLinkBodySchema)
diff --git a/apps/web/lib/zod/schemas/misc.ts b/apps/web/lib/zod/schemas/misc.ts
index 13bd024e73..d939784be1 100644
--- a/apps/web/lib/zod/schemas/misc.ts
+++ b/apps/web/lib/zod/schemas/misc.ts
@@ -103,3 +103,10 @@ export const uploadedImageSchema = z
}),
])
.transform((v) => v || null);
+
+export const publicHostedImageSchema = z
+ .string()
+ .url()
+ .refine((url) => url.startsWith("http://") || url.startsWith("https://"), {
+ message: "Image URL must start with http:// or https://",
+ });