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.

Validate tagIds in processLink

Tim Wilson922868c0cd79
diff --git a/apps/web/app/api/cron/links/event/route.ts b/apps/web/app/api/cron/links/event/route.ts
index 335a8f3e47..2b04926d4c 100644
--- a/apps/web/app/api/cron/links/event/route.ts
+++ b/apps/web/app/api/cron/links/event/route.ts
@@ -1,5 +1,4 @@
import { getAnalytics } from "@/lib/analytics";
-import { removeInvalidLinkTags } from "@/lib/api/links";
import { limiter, qstash, receiver } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
@@ -35,9 +34,6 @@ export async function POST(req: Request) {
where: {
id: linkId,
},
- include: {
- tags: true,
- },
});
if (!link) {
@@ -77,7 +73,6 @@ export async function POST(req: Request) {
[link.key.toLowerCase()]: await formatRedisLink(link),
}),
recordLink({ link }),
- link.tags.length > 0 && removeInvalidLinkTags({ linkId: link.id }),
]);
// increment links usage and send alert if needed
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index ed7d46bcc1..1c04261e41 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -272,6 +272,8 @@ export async function processLink({
geo,
} = payload;
+ const tagIds = combineTagIds(payload);
+
// url checks
if (!url) {
return {
@@ -417,6 +419,21 @@ export async function processLink({
}
}
+ // tag validity checks
+ if (tagIds.length > 0) {
+ const tags = await prisma.tag.findMany({
+ where: { projectId: project?.id, id: { in: tagIds } },
+ });
+
+ if (tags.length !== tagIds.length) {
+ return {
+ link: payload,
+ error: "One or more tagIds are invalid.",
+ status: 422,
+ };
+ }
+ }
+
// remove polyfill attributes from payload
delete payload["shortLink"];
delete payload["qrCode"];
@@ -829,15 +846,3 @@ export async function transferLink({
},
});
}
-
-export async function removeInvalidLinkTags({ linkId }: { linkId: string }) {
- console.log(`Removing invalid LinkTags for linkId: ${linkId}`);
- return await prisma.linkTag.deleteMany({
- where: {
- linkId,
- tag: {
- id: undefined,
- },
- },
- });
-}