The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
verify the webhook secret
Kiran K448aab4fd5d7
diff --git a/apps/web/app/(ee)/api/hubspot/webhook/route.ts b/apps/web/app/(ee)/api/hubspot/webhook/route.tsindex abce13f6d2..bcf33f9837 100644--- a/apps/web/app/(ee)/api/hubspot/webhook/route.ts+++ b/apps/web/app/(ee)/api/hubspot/webhook/route.ts@@ -1,23 +1,53 @@-import { handleAndReturnErrorResponse } from "@/lib/api/errors";+import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";+import { HUBSPOT_CLIENT_SECRET } from "@/lib/integrations/hubspot/constants";+import crypto from "crypto";import { AxiomRequest, withAxiom } from "next-axiom";import { NextResponse } from "next/server";-// TODO:-// Verify the webhook signature using client secret-// https://developers.hubspot.com/docs/apps/legacy-apps/authentication/validating-requests--// POST /api/hubspot/webhook – listen to webhook events from Hubspot+// POST /api/hubspot/webhook – listen to webhook events from Hubspotexport const POST = withAxiom(async (req: AxiomRequest) => {- console.log("Received webhook from Hubspot")-try {const rawBody = await req.text();- // console.log(rawBody)+ const signature = req.headers.get("X-HubSpot-Signature");++ // Verify webhook signature+ if (!signature) {+ throw new DubApiError({+ code: "bad_request",+ message: "Missing X-HubSpot-Signature header.",+ });+ }++ if (!HUBSPOT_CLIENT_SECRET) {+ throw new DubApiError({+ code: "internal_server_error",+ message: "Missing HUBSPOT_CLIENT_SECRET environment variable.",+ });+ }++ // Create expected hash: client_secret + request_body+ const sourceString = HUBSPOT_CLIENT_SECRET + rawBody;+ const expectedHash = crypto+ .createHash("sha256")+ .update(sourceString)+ .digest("hex");++ console.log("Expected hash:", expectedHash);+ console.log("Signature:", signature);++ // Compare with provided signature+ if (signature !== expectedHash) {+ throw new DubApiError({+ code: "unauthorized",+ message: "Invalid webhook signature.",+ });+ }+const body = JSON.parse(rawBody);- console.log(body);+ console.log("Verified webhook payload:", body);- return NextResponse.json("OK");+ return NextResponse.json({ message: "Webhook received." });} catch (error) {return handleAndReturnErrorResponse(error);}-});\ No newline at end of file+});