Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Revert note attachments webhook auth to AuthMiddleware flow

Anibal Svarcasc807547dafb4
diff --git a/supabase/functions/delete_note_attachments/index.ts b/supabase/functions/delete_note_attachments/index.ts
index c6923019..2fc10430 100644
--- a/supabase/functions/delete_note_attachments/index.ts
+++ b/supabase/functions/delete_note_attachments/index.ts
@@ -1,9 +1,8 @@
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
+import { AuthMiddleware } from "../_shared/authentication.ts";
import { supabaseAdmin } from "../_shared/supabaseAdmin.ts";
import { ATTACHMENTS_BUCKET } from "../../../src/components/atomic-crm/providers/commons/attachments.ts";
-const DEFAULT_WEBHOOK_SECRET = "atomic-crm-note-attachments-webhook-secret";
-
type NoteAttachment = {
path?: string | null;
src?: string | null;
@@ -53,25 +52,9 @@ const deleteNoteAttachments = async (req: Request) => {
});
};
-Deno.serve(async (req: Request) => {
- if (!isWebhookRequestAuthenticated(req)) {
- return jsonResponse({ error: "Unauthorized" }, 401);
- }
-
- return deleteNoteAttachments(req);
-});
-
-const isWebhookRequestAuthenticated = (req: Request) => {
- const providedSecret = req.headers.get("x-webhook-secret");
- if (!providedSecret) {
- return false;
- }
-
- const expectedSecret =
- Deno.env.get("ATTACHMENTS_WEBHOOK_SECRET") ?? DEFAULT_WEBHOOK_SECRET;
-
- return providedSecret === expectedSecret;
-};
+Deno.serve(async (req: Request) =>
+ AuthMiddleware(req, async (req: Request) => deleteNoteAttachments(req)),
+);
const getPathsToDelete = (payload: WebhookPayload): string[] => {
const oldPaths = extractAttachmentPaths(payload.old_record?.attachments);
diff --git a/supabase/migrations/20260220154000_note_attachments_webhooks.sql b/supabase/migrations/20260220154000_note_attachments_webhooks.sql
index aafd9b9c..843ed878 100644
--- a/supabase/migrations/20260220154000_note_attachments_webhooks.sql
+++ b/supabase/migrations/20260220154000_note_attachments_webhooks.sql
@@ -27,7 +27,6 @@ BEGIN
)
);
issuer := nullif(issuer, '');
-
IF issuer IS NOT NULL THEN
issuer := rtrim(issuer, '/');
IF right(issuer, 8) = '/auth/v1' THEN
@@ -76,12 +75,22 @@ BEGIN
AS $function$
DECLARE
payload jsonb;
- webhook_secret text;
+ request_headers jsonb;
+ auth_header text;
BEGIN
- webhook_secret := coalesce(
- nullif(current_setting('app.settings.attachments_webhook_secret', true), ''),
- 'atomic-crm-note-attachments-webhook-secret'
+ request_headers := coalesce(
+ nullif(current_setting('request.headers', true), '')::jsonb,
+ '{}'::jsonb
);
+ auth_header := request_headers ->> 'authorization';
+
+ IF auth_header IS NULL OR auth_header = '' THEN
+ IF TG_OP = 'DELETE' THEN
+ RETURN OLD;
+ END IF;
+
+ RETURN NEW;
+ END IF;
payload := jsonb_build_object(
'old_record', OLD,
@@ -96,8 +105,8 @@ BEGIN
headers := jsonb_build_object(
'Content-Type',
'application/json',
- 'X-Webhook-Secret',
- webhook_secret
+ 'Authorization',
+ auth_header
),
timeout_milliseconds := 10000
);