The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix webhook signature
Kiran K0c863bac9d14
diff --git a/apps/web/app/api/webhooks/receive/route.ts b/apps/web/app/api/webhooks/receive/route.tsindex e0c7244f79..bf2b1a5fff 100644--- a/apps/web/app/api/webhooks/receive/route.ts+++ b/apps/web/app/api/webhooks/receive/route.ts@@ -1,16 +1,28 @@-// TODO:-// Move this to proper route path-// Verify the Dub-Signature header-import { prisma } from "@/lib/prisma";import { LeadEventData } from "@/lib/webhook/types";import { webhookPayloadSchema } from "@/lib/zod/schemas/webhooks";+import crypto from "crypto";// POST /api/webhooks/receive - receive link-level webhooks from Dubexport const POST = async (req: Request) => {const body = await req.json();const { event, data } = webhookPayloadSchema.parse(body);+ const webhookSignature = req.headers.get("Dub-Signature");++ if (!webhookSignature) {+ return new Response("No signature", { status: 400 });+ }++ const computedSignature = crypto+ .createHmac("sha256", "whsec_f0dfa39067ee15fdf5abe53c08600c22")+ .update(JSON.stringify(body))+ .digest("hex");++ if (webhookSignature !== computedSignature) {+ return new Response("Invalid signature", { status: 400 });+ }+// A new lead was created on Dubif (event === "lead.created") {const lead = data as LeadEventData;diff --git a/apps/web/lib/webhook/signature.ts b/apps/web/lib/webhook/signature.tsindex 40e6505d5d..a22c1dc514 100644--- a/apps/web/lib/webhook/signature.ts+++ b/apps/web/lib/webhook/signature.ts@@ -5,7 +5,7 @@ export const createWebhookSignature = async (secret: string, body: any) => {}const keyData = new TextEncoder().encode(secret);- const messageData = new TextEncoder().encode(body);+ const messageData = new TextEncoder().encode(JSON.stringify(body));const cryptoKey = await crypto.subtle.importKey("raw",