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.

Enforce tenant ownership on SCIM, SAML, and webhook endpoints (#3834)

Kiran K0af40b4d80b3
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index d0e4505581..fcdef66506 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -300,8 +300,9 @@ export const PATCH = withWorkspace(
})),
);
- let { tagNames, expiresAt } = data;
+ let { tagNames, expiresAt, webhookIds } = data;
const tagIds = combineTagIds(data);
+
// tag checks
if (tagIds && tagIds.length > 0) {
const tags = await prisma.tag.findMany({
@@ -336,6 +337,34 @@ export const PATCH = withWorkspace(
}
}
+ // Webhook checks
+ if (webhookIds && webhookIds.length > 0) {
+ const webhooks = await prisma.webhook.findMany({
+ where: {
+ id: {
+ in: webhookIds,
+ },
+ projectId: workspace.id,
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ const invalidWebhookIds = webhookIds.filter(
+ (id) => !webhooks.some((webhook) => webhook.id === id),
+ );
+
+ if (invalidWebhookIds.length > 0) {
+ throw new DubApiError({
+ code: "unprocessable_entity",
+ message: `Invalid webhookIds detected: ${invalidWebhookIds.join(", ")}`,
+ });
+ }
+
+ data.webhookIds = webhooks.map((webhook) => webhook.id);
+ }
+
if (data.folderId) {
await verifyFolderAccess({
workspace,
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts
index eacc834aa8..b78b4fd424 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/saml/route.ts
@@ -104,6 +104,27 @@ export const DELETE = withWorkspace(
const { apiController } = await jackson();
+ const connections = await apiController.getConnections({
+ tenant: workspace.id,
+ product: "Dub",
+ });
+
+ const connection = connections.find(
+ (connection) => connection.clientID === clientID,
+ );
+
+ if (!connection) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "SAML connection not found",
+ });
+ }
+
+ await apiController.deleteConnections({
+ clientID: connection.clientID,
+ clientSecret: connection.clientSecret,
+ });
+
await apiController.deleteConnections({
clientID,
clientSecret,
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts
index edf1275550..4f3e8ac9b1 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/scim/route.ts
@@ -47,6 +47,28 @@ export const POST = withWorkspace(
const { directorySyncController } = await jackson();
+ if (currentDirectoryId) {
+ const directory =
+ await directorySyncController.directories.get(currentDirectoryId);
+
+ if (!directory || directory.error || !directory.data) {
+ throw new DubApiError({
+ code: "not_found",
+ message: directory?.error?.message ?? "Directory not found.",
+ });
+ }
+
+ if (
+ directory.data.tenant !== workspace.id ||
+ directory.data.product !== "Dub"
+ ) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Directory not found.",
+ });
+ }
+ }
+
const [data, _] = await Promise.all([
directorySyncController.directories.create({
name: "Dub SCIM Directory",
@@ -54,6 +76,7 @@ export const POST = withWorkspace(
product: "Dub",
type: provider,
}),
+
currentDirectoryId &&
directorySyncController.directories.delete(currentDirectoryId),
]);
@@ -68,11 +91,31 @@ export const POST = withWorkspace(
// DELETE /api/workspaces/[idOrSlug]/scim – delete a SCIM directory
export const DELETE = withWorkspace(
- async ({ searchParams }) => {
+ async ({ workspace, searchParams }) => {
const { directoryId } = deleteDirectorySchema.parse(searchParams);
const { directorySyncController } = await jackson();
+ const directory =
+ await directorySyncController.directories.get(directoryId);
+
+ if (!directory || directory.error || !directory.data) {
+ throw new DubApiError({
+ code: "not_found",
+ message: directory?.error?.message ?? "Directory not found.",
+ });
+ }
+
+ if (
+ directory.data.tenant !== workspace.id ||
+ directory.data.product !== "Dub"
+ ) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "Directory not found.",
+ });
+ }
+
const { error, data } =
await directorySyncController.directories.delete(directoryId);