From e72cb0951d35a77319fc14d2c14a44aff2cfb700 Mon Sep 17 00:00:00 2001 From: alphaeusmote <41258468-alphaeusmote@users.noreply.replit.com> Date: Sat, 12 Apr 2025 00:21:42 +0000 Subject: [PATCH] Refactor webhook authorization to use customer-based permissions. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9111ef36-26c8-4085-84ca-a35dc1fec1b5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7083d608-d6d3-4a6a-9a27-6286c5109627/576e7a1f-0f68-4e6c-8fa8-b07538fd6481.jpg --- server/routes.ts | 52 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/server/routes.ts b/server/routes.ts index f97634a..3796ec4 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -1542,9 +1542,16 @@ export async function registerRoutes(app: Express): Promise { return res.status(404).json({ message: "Webhook not found" }); } - // Check authorization - if (req.user?.role !== "admin" && webhook.organizationId !== req.user?.organizationId) { - return res.status(403).json({ message: "Not authorized to test this webhook" }); + // Check authorization for non-admin users + if (req.user?.role !== "admin") { + // Get user's customers + const userCustomers = await storage.getUserCustomers(req.user?.id as string); + const userCustomerIds = userCustomers.map(c => c.id); + + // Check if the user has access to the webhook's customer + if (!userCustomerIds.includes(webhook.customerId)) { + return res.status(403).json({ message: "Not authorized to test this webhook" }); + } } const testPayload = { @@ -1584,9 +1591,16 @@ export async function registerRoutes(app: Express): Promise { return res.status(404).json({ message: "Webhook not found" }); } - // Permission check: admin can view all logs, others only for their organization's webhooks - if (req.user?.role !== "admin" && webhook.organizationId !== req.user?.organizationId) { - return res.status(403).json({ message: "Not authorized to access logs for this webhook" }); + // Permission check: admin can view all logs, others only for their customer's webhooks + if (req.user?.role !== "admin") { + // Get user's customers + const userCustomers = await storage.getUserCustomers(req.user?.id as string); + const userCustomerIds = userCustomers.map(c => c.id); + + // Check if the user has access to the webhook's customer + if (!userCustomerIds.includes(webhook.customerId)) { + return res.status(403).json({ message: "Not authorized to access logs for this webhook" }); + } } try { @@ -1620,9 +1634,16 @@ export async function registerRoutes(app: Express): Promise { return res.status(404).json({ message: "Associated webhook not found" }); } - // Permission check - if (req.user?.role !== "admin" && webhook.organizationId !== req.user?.organizationId) { - return res.status(403).json({ message: "Not authorized to access this log" }); + // Permission check for non-admin users + if (req.user?.role !== "admin") { + // Get user's customers + const userCustomers = await storage.getUserCustomers(req.user?.id as string); + const userCustomerIds = userCustomers.map(c => c.id); + + // Check if the user has access to the webhook's customer + if (!userCustomerIds.includes(webhook.customerId)) { + return res.status(403).json({ message: "Not authorized to access this log" }); + } } res.json(log); @@ -1648,9 +1669,16 @@ export async function registerRoutes(app: Express): Promise { return res.status(404).json({ message: "Associated webhook not found" }); } - // Permission check - if (req.user?.role !== "admin" && webhook.organizationId !== req.user?.organizationId) { - return res.status(403).json({ message: "Not authorized to retry this webhook" }); + // Permission check for non-admin users + if (req.user?.role !== "admin") { + // Get user's customers + const userCustomers = await storage.getUserCustomers(req.user?.id as string); + const userCustomerIds = userCustomers.map(c => c.id); + + // Check if the user has access to the webhook's customer + if (!userCustomerIds.includes(webhook.customerId)) { + return res.status(403).json({ message: "Not authorized to retry this webhook" }); + } } // Only retry failed deliveries