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.

Add folder access check to webhook link filtering

Kiran K76a593a52a53
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
index 7ad13bd915..ef52d17676 100644
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@ -2,6 +2,7 @@ import { DubApiError } from "@/lib/api/errors";
import { linkCache } from "@/lib/api/links/cache";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { getFolders } from "@/lib/folder/get-folders";
import { webhookCache } from "@/lib/webhook/cache";
import { transformWebhook } from "@/lib/webhook/transform";
import { toggleWebhooksForWorkspace } from "@/lib/webhook/update-webhook";
@@ -49,7 +50,7 @@ export const GET = withWorkspace(
// PATCH /api/webhooks/[webhookId] - update a specific webhook
export const PATCH = withWorkspace(
- async ({ workspace, params, req }) => {
+ async ({ workspace, params, req, session }) => {
const { webhookId } = params;
const { name, url, triggers, linkIds } = updateWebhookSchema.parse(
@@ -92,10 +93,21 @@ export const PATCH = withWorkspace(
}
if (linkIds && linkIds.length > 0) {
+ const folders = await getFolders({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ });
+
const links = await prisma.link.findMany({
where: {
- id: { in: linkIds },
+ id: {
+ in: linkIds,
+ },
projectId: workspace.id,
+ OR: [
+ { folderId: null },
+ { folderId: { in: folders.map((folder) => folder.id) } },
+ ],
},
select: {
id: true,
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.ts
index a4d03b0426..dffb2aee0e 100644
--- a/apps/web/app/api/webhooks/route.ts
+++ b/apps/web/app/api/webhooks/route.ts
@@ -2,6 +2,7 @@ import { DubApiError } from "@/lib/api/errors";
import { linkCache } from "@/lib/api/links/cache";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { getFolders } from "@/lib/folder/get-folders";
import { webhookCache } from "@/lib/webhook/cache";
import { createWebhook } from "@/lib/webhook/create-webhook";
import { transformWebhook } from "@/lib/webhook/transform";
@@ -78,10 +79,21 @@ export const POST = withWorkspace(
}
if (linkIds && linkIds.length > 0) {
+ const folders = await getFolders({
+ workspaceId: workspace.id,
+ userId: session.user.id,
+ });
+
const links = await prisma.link.findMany({
where: {
- id: { in: linkIds },
+ id: {
+ in: linkIds,
+ },
projectId: workspace.id,
+ OR: [
+ { folderId: null },
+ { folderId: { in: folders.map((folder) => folder.id) } },
+ ],
},
select: {
id: true,
diff --git a/apps/web/lib/folder/get-folders.ts b/apps/web/lib/folder/get-folders.ts
index 40e9d94a9a..14f8bd6451 100644
--- a/apps/web/lib/folder/get-folders.ts
+++ b/apps/web/lib/folder/get-folders.ts
@@ -15,12 +15,18 @@ export const getFolders = async ({
where: {
projectId: workspaceId,
OR: [
- { accessLevel: { not: null } },
+ {
+ accessLevel: {
+ not: null,
+ },
+ },
{
users: {
some: {
userId,
- role: { not: null },
+ role: {
+ not: null,
+ },
},
},
},