Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix: webhook isolation
Marc Seitzece358be634f
diff --git a/lib/api/views/send-webhook-event.ts b/lib/api/views/send-webhook-event.tsindex 1843037e..a6c21823 100644--- a/lib/api/views/send-webhook-event.ts+++ b/lib/api/views/send-webhook-event.ts@@ -29,12 +29,8 @@ export async function sendLinkViewWebhook({select: { plan: true },});- if (- team?.plan === "free" ||- team?.plan === "pro" ||- team?.plan.includes("trial")- ) {- // team is not on paid plan, so we don't need to send webhooks+ if (team?.plan === "free" || team?.plan === "pro") {+ // team is not on a webhook-eligible plan, so we don't send webhooksreturn;}diff --git a/lib/webhook/send-webhooks.ts b/lib/webhook/send-webhooks.tsindex 58311d30..c86c08a3 100644--- a/lib/webhook/send-webhooks.ts+++ b/lib/webhook/send-webhooks.ts@@ -6,6 +6,17 @@ import { createWebhookSignature } from "./signature";import { prepareWebhookPayload } from "./transform";import { EventDataProps, WebhookPayload, WebhookTrigger } from "./types";+// Strip path, query, and credentials from a URL so logs never expose+// secrets embedded in webhook endpoints (tokens in path/query, basic auth, etc.).+const redactUrl = (url: string): string => {+ try {+ const u = new URL(url);+ return `${u.protocol}//${u.host}`;+ } catch {+ return "[invalid-url]";+ }+};+// Send webhooks to multiple webhooksexport const sendWebhooks = async ({webhooks,@@ -22,11 +33,36 @@ export const sendWebhooks = async ({const payload = prepareWebhookPayload(trigger, data);- return await Promise.all(+ // Use allSettled so that a single QStash failure (e.g. transient network+ // error or rate limit on one endpoint) does not prevent the remaining+ // webhooks in the batch from being delivered.+ const results = await Promise.allSettled(webhooks.map((webhook) =>publishWebhookEventToQStash({ webhook, payload }),),);++ const fulfilled: Awaited<ReturnType<typeof publishWebhookEventToQStash>>[] =+ [];+ for (let i = 0; i < results.length; i++) {+ const result = results[i];+ if (result.status === "fulfilled") {+ fulfilled.push(result.value);+ } else {+ const webhook = webhooks[i];+ const reasonMessage =+ result.reason instanceof Error+ ? result.reason.message+ : typeof result.reason === "string"+ ? result.reason+ : "Unknown error";+ console.error(+ `Failed to deliver webhook ${webhook.pId} to ${redactUrl(webhook.url)}: ${reasonMessage}`,+ );+ }+ }++ return fulfilled;};// Publish webhook event to QStash@@ -46,20 +82,35 @@ const publishWebhookEventToQStash = async ({const signature = await createWebhookSignature(webhook.secret, payload);- const response = await qstash.publishJSON({- url: webhook.url,- body: payload,- headers: {- "X-Papermark-Signature": signature,- "Upstash-Hide-Headers": "true",- },- callback: callbackUrl.href,- failureCallback: callbackUrl.href,- });+ try {+ const response = await qstash.publishJSON({+ url: webhook.url,+ body: payload,+ headers: {+ "X-Papermark-Signature": signature,+ "Upstash-Hide-Headers": "true",+ },+ callback: callbackUrl.href,+ failureCallback: callbackUrl.href,+ });- if (!response.messageId) {- console.error("Failed to publish webhook event to QStash", response);- }+ if (!response.messageId) {+ console.error("Failed to publish webhook event to QStash", response);+ }- return response;+ return response;+ } catch (error) {+ // Surface which webhook failed so the caller's allSettled handler and+ // logs make it easy to identify the broken endpoint.+ const errorMessage =+ error instanceof Error+ ? error.message+ : typeof error === "string"+ ? error+ : "Unknown error";+ console.error(+ `Failed to publish webhook event to QStash for webhook ${webhook.pId} (${redactUrl(webhook.url)}): ${errorMessage}`,+ );+ throw error;+ }};diff --git a/lib/webhook/triggers/link-created.ts b/lib/webhook/triggers/link-created.tsindex 4eaaeee3..67c7355e 100644--- a/lib/webhook/triggers/link-created.ts+++ b/lib/webhook/triggers/link-created.ts@@ -28,12 +28,8 @@ export async function sendLinkCreatedWebhook({select: { plan: true },});- if (- team?.plan === "free" ||- team?.plan === "pro" ||- team?.plan.includes("trial")- ) {- // team is not on paid plan, so we don't need to send webhooks+ if (team?.plan === "free" || team?.plan === "pro") {+ // team is not on a webhook-eligible plan, so we don't send webhooksreturn;}