Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Support X-Body-Signature for Intercom uninstall and health check webhook (#4054)

Kiran K50d17cb41258
diff --git a/apps/web/app/(ee)/api/intercom/webhook/verify-webhook-signature.ts b/apps/web/app/(ee)/api/intercom/webhook/verify-webhook-signature.ts
index a11f60f66a..eea0353851 100644
--- a/apps/web/app/(ee)/api/intercom/webhook/verify-webhook-signature.ts
+++ b/apps/web/app/(ee)/api/intercom/webhook/verify-webhook-signature.ts
@@ -7,33 +7,54 @@ export async function verifyIntercomWebhookSignature(
req: Request,
): Promise<string> {
const rawBody = await req.text();
- const signature = req.headers.get("X-Hub-Signature");
+ const hubSignature = req.headers.get("X-Hub-Signature");
+ const bodySignature = req.headers.get("X-Body-Signature");
- if (!signature) {
+ if (!INTERCOM_CLIENT_SECRET) {
throw new DubApiError({
- code: "bad_request",
- message: "Missing X-Hub-Signature header.",
+ code: "internal_server_error",
+ message: "Missing INTERCOM_CLIENT_SECRET environment variable.",
});
}
- if (!INTERCOM_CLIENT_SECRET) {
+ // Uninstall and health-check use X-Body-Signature
+ if (!hubSignature && !bodySignature) {
throw new DubApiError({
- code: "internal_server_error",
- message: "Missing INTERCOM_CLIENT_SECRET environment variable.",
+ code: "bad_request",
+ message: "Missing X-Hub-Signature or X-Body-Signature header.",
});
}
- const expectedSignature =
- "sha1=" +
- crypto
- .createHmac("sha1", INTERCOM_CLIENT_SECRET)
+ let expectedSignature: string;
+ let providedSignature: string;
+
+ if (hubSignature) {
+ expectedSignature =
+ "sha1=" +
+ crypto
+ .createHmac("sha1", INTERCOM_CLIENT_SECRET)
+ .update(rawBody)
+ .digest("hex");
+
+ providedSignature = hubSignature;
+ } else if (bodySignature) {
+ expectedSignature = crypto
+ .createHmac("sha256", INTERCOM_CLIENT_SECRET)
.update(rawBody)
.digest("hex");
- const providedSignature = Buffer.from(signature, "utf8");
- const expectedSignatureBuffer = Buffer.from(expectedSignature, "utf8");
+ providedSignature = bodySignature;
+ } else {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "Missing X-Hub-Signature or X-Body-Signature header.",
+ });
+ }
+
+ const providedBuffer = Buffer.from(providedSignature, "utf8");
+ const expectedBuffer = Buffer.from(expectedSignature, "utf8");
- if (providedSignature.length !== expectedSignatureBuffer.length) {
+ if (providedBuffer.length !== expectedBuffer.length) {
throw new DubApiError({
code: "unauthorized",
message: "Invalid webhook signature.",
@@ -41,8 +62,8 @@ export async function verifyIntercomWebhookSignature(
}
const isSignatureValid = crypto.timingSafeEqual(
- Uint8Array.from(providedSignature),
- Uint8Array.from(expectedSignatureBuffer),
+ Uint8Array.from(providedBuffer),
+ Uint8Array.from(expectedBuffer),
);
if (!isSignatureValid) {