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.

fix some links issues, improved verifyFolderAccess

Steven Teye6ca6793212c
diff --git a/apps/web/app/api/analytics/export/route.ts b/apps/web/app/api/analytics/export/route.ts
index 2c64b97ef4..63fb89cec7 100644
--- a/apps/web/app/api/analytics/export/route.ts
+++ b/apps/web/app/api/analytics/export/route.ts
@@ -36,29 +36,15 @@ export const GET = withWorkspace(
});
}
- await Promise.all([
- ...(link && link.folderId
- ? [
- verifyFolderAccess({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId: link.folderId,
- requiredPermission: "folders.read",
- }),
- ]
- : []),
-
- ...(folderId
- ? [
- verifyFolderAccess({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId,
- requiredPermission: "folders.read",
- }),
- ]
- : []),
- ]);
+ const folderIdToVerify = link?.folderId || folderId;
+ if (folderIdToVerify) {
+ await verifyFolderAccess({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ folderId: folderIdToVerify,
+ requiredPermission: "folders.read",
+ });
+ }
validDateRangeForPlan({
plan: workspace.plan,
diff --git a/apps/web/app/api/analytics/route.ts b/apps/web/app/api/analytics/route.ts
index 972a8a0c4e..0cd1f8db2b 100644
--- a/apps/web/app/api/analytics/route.ts
+++ b/apps/web/app/api/analytics/route.ts
@@ -61,29 +61,15 @@ export const GET = withWorkspace(
});
}
- await Promise.all([
- ...(link && link.folderId
- ? [
- verifyFolderAccess({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId: link.folderId,
- requiredPermission: "folders.read",
- }),
- ]
- : []),
-
- ...(folderId
- ? [
- verifyFolderAccess({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId,
- requiredPermission: "folders.read",
- }),
- ]
- : []),
- ]);
+ const folderIdToVerify = link?.folderId || folderId;
+ if (folderIdToVerify) {
+ await verifyFolderAccess({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ folderId: folderIdToVerify,
+ requiredPermission: "folders.read",
+ });
+ }
validDateRangeForPlan({
plan: workspace.plan,
diff --git a/apps/web/app/api/events/export/route.ts b/apps/web/app/api/events/export/route.ts
index fa6db4f63c..823a58684e 100644
--- a/apps/web/app/api/events/export/route.ts
+++ b/apps/web/app/api/events/export/route.ts
@@ -60,29 +60,15 @@ export const GET = withWorkspace(
? await getLinkOrThrow({ workspaceId: workspace.id, domain, key })
: null;
- await Promise.all([
- ...(link && link.folderId
- ? [
- verifyFolderAccess({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId: link.folderId,
- requiredPermission: "folders.read",
- }),
- ]
- : []),
-
- ...(folderId
- ? [
- verifyFolderAccess({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId,
- requiredPermission: "folders.read",
- }),
- ]
- : []),
- ]);
+ const folderIdToVerify = link?.folderId || folderId;
+ if (folderIdToVerify) {
+ await verifyFolderAccess({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ folderId: folderIdToVerify,
+ requiredPermission: "folders.read",
+ });
+ }
validDateRangeForPlan({
plan: workspace.plan,
diff --git a/apps/web/app/api/events/route.ts b/apps/web/app/api/events/route.ts
index e1e4f19550..88dd59f113 100644
--- a/apps/web/app/api/events/route.ts
+++ b/apps/web/app/api/events/route.ts
@@ -43,29 +43,15 @@ export const GET = withWorkspace(
});
}
- await Promise.all([
- ...(link && link.folderId
- ? [
- verifyFolderAccess({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId: link.folderId,
- requiredPermission: "folders.read",
- }),
- ]
- : []),
-
- ...(folderId
- ? [
- verifyFolderAccess({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId,
- requiredPermission: "folders.read",
- }),
- ]
- : []),
- ]);
+ const folderIdToVerify = link?.folderId || folderId;
+ if (folderIdToVerify) {
+ await verifyFolderAccess({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ folderId: folderIdToVerify,
+ requiredPermission: "folders.read",
+ });
+ }
validDateRangeForPlan({
plan: workspace.plan,
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 11a3244091..ce89d395af 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -274,29 +274,14 @@ export const PATCH = withWorkspace(
}
}
- await Promise.all([
- // ...(link.folderId
- // ? [
- // verifyFolderAccess({
- // workspaceId: workspace.id,
- // userId: session.user.id,
- // folderId: link.folderId,
- // requiredPermission: "folders.links.write",
- // }),
- // ]
- // : []),
-
- ...(data.folderId
- ? [
- verifyFolderAccess({
- workspaceId: workspace.id,
- userId: session.user.id,
- folderId: data.folderId,
- requiredPermission: "folders.links.write",
- }),
- ]
- : []),
- ]);
+ if (data.folderId) {
+ await verifyFolderAccess({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ folderId: data.folderId,
+ requiredPermission: "folders.links.write",
+ });
+ }
const processedLinks = await Promise.all(
links.map(async (link) =>
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index 572f234fb3..030f4a4f9d 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -320,18 +320,24 @@ export async function processLink<T extends Record<string, any>>({
code: "unprocessable_entity",
};
}
-
+ } else {
// only perform tag validity checks if:
// - not bulk creation (we do that check separately in the route itself)
// - tagIds are present
- } else {
- // Tag validity checks
if (tagIds && tagIds.length > 0) {
+ if (!workspace) {
+ return {
+ link: payload,
+ error:
+ "Workspace not found. You can't add tags to a link without a workspace.",
+ code: "not_found",
+ };
+ }
const tags = await prisma.tag.findMany({
select: {
id: true,
},
- where: { projectId: workspace?.id, id: { in: tagIds } },
+ where: { projectId: workspace.id, id: { in: tagIds } },
});
if (tags.length !== tagIds.length) {
@@ -348,12 +354,20 @@ export async function processLink<T extends Record<string, any>>({
};
}
} else if (tagNames && tagNames.length > 0) {
+ if (!workspace) {
+ return {
+ link: payload,
+ error:
+ "Workspace not found. You can't add tags to a link without a workspace.",
+ code: "not_found",
+ };
+ }
const tags = await prisma.tag.findMany({
select: {
name: true,
},
where: {
- projectId: workspace?.id,
+ projectId: workspace.id,
name: { in: tagNames },
},
});
@@ -374,6 +388,35 @@ export async function processLink<T extends Record<string, any>>({
}
}
+ // only perform folder validity checks if:
+ // - not bulk creation (we do that check separately in the route itself)
+ // - folderId is present
+ if (folderId) {
+ if (!workspace || !userId) {
+ return {
+ link: payload,
+ error:
+ "Workspace or user ID not found. You can't add a folder to a link without a workspace or user ID.",
+ code: "not_found",
+ };
+ }
+
+ try {
+ await verifyFolderAccess({
+ workspaceId: workspace.id,
+ userId,
+ folderId,
+ requiredPermission: "folders.links.write",
+ });
+ } catch (error) {
+ return {
+ link: payload,
+ error: error.message,
+ code: error.code,
+ };
+ }
+ }
+
// Program validity checks
if (programId && !skipProgramChecks) {
const program = await prisma.program.findUnique({
@@ -444,24 +487,6 @@ export async function processLink<T extends Record<string, any>>({
}
}
- // Folder checks
- if (folderId && workspace && userId) {
- try {
- await verifyFolderAccess({
- workspaceId: workspace.id,
- userId,
- folderId,
- requiredPermission: "folders.links.write",
- });
- } catch (error) {
- return {
- link: payload,
- error: error.message,
- code: error.code,
- };
- }
- }
-
// custom social media image checks (see if R2 is configured)
if (proxy && !process.env.STORAGE_SECRET_ACCESS_KEY) {
return {
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index 032ae80612..8dbff7f797 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -101,7 +101,10 @@ export const withWorkspace = (
*/
if (!idOrSlug && !isRestrictedToken) {
// special case for anonymous link creation
- if (req.headers.has("dub-anonymous-link-creation")) {
+ if (
+ req.headers.has("dub-anonymous-link-creation") &&
+ req.nextUrl.pathname === "/links"
+ ) {
// @ts-expect-error
return await handler({
req,