|
| 1 | +import crypto from 'node:crypto' |
1 | 2 | import { createLogger } from '@sim/logger' |
| 3 | +import { NextResponse } from 'next/server' |
| 4 | +import { safeCompare } from '@/lib/core/security/encryption' |
2 | 5 | import { getNotificationUrl, getProviderConfig } from '@/lib/webhooks/providers/subscription-utils' |
3 | 6 | import type { |
| 7 | + AuthContext, |
4 | 8 | DeleteSubscriptionContext, |
5 | 9 | FormatInputContext, |
6 | 10 | FormatInputResult, |
@@ -31,7 +35,71 @@ const ALL_RESEND_EVENTS = [ |
31 | 35 | 'domain.deleted', |
32 | 36 | ] |
33 | 37 |
|
| 38 | +/** |
| 39 | + * Verify a Resend webhook signature using the Svix signing scheme. |
| 40 | + * Resend uses Svix under the hood: HMAC-SHA256 of `${svix-id}.${svix-timestamp}.${body}` |
| 41 | + * signed with the base64-decoded `whsec_...` secret. |
| 42 | + */ |
| 43 | +function verifySvixSignature( |
| 44 | + secret: string, |
| 45 | + msgId: string, |
| 46 | + timestamp: string, |
| 47 | + signatures: string, |
| 48 | + rawBody: string |
| 49 | +): boolean { |
| 50 | + try { |
| 51 | + const secretBytes = Buffer.from(secret.replace(/^whsec_/, ''), 'base64') |
| 52 | + const toSign = `${msgId}.${timestamp}.${rawBody}` |
| 53 | + const expectedSignature = crypto |
| 54 | + .createHmac('sha256', secretBytes) |
| 55 | + .update(toSign, 'utf8') |
| 56 | + .digest('base64') |
| 57 | + |
| 58 | + const providedSignatures = signatures.split(' ') |
| 59 | + for (const versionedSig of providedSignatures) { |
| 60 | + const parts = versionedSig.split(',') |
| 61 | + if (parts.length !== 2) continue |
| 62 | + const sig = parts[1] |
| 63 | + if (safeCompare(sig, expectedSignature)) { |
| 64 | + return true |
| 65 | + } |
| 66 | + } |
| 67 | + return false |
| 68 | + } catch (error) { |
| 69 | + logger.error('Error verifying Resend Svix signature:', error) |
| 70 | + return false |
| 71 | + } |
| 72 | +} |
| 73 | + |
34 | 74 | export const resendHandler: WebhookProviderHandler = { |
| 75 | + async verifyAuth({ |
| 76 | + request, |
| 77 | + rawBody, |
| 78 | + requestId, |
| 79 | + providerConfig, |
| 80 | + }: AuthContext): Promise<NextResponse | null> { |
| 81 | + const signingSecret = providerConfig.signingSecret as string | undefined |
| 82 | + if (!signingSecret) { |
| 83 | + return null |
| 84 | + } |
| 85 | + |
| 86 | + const svixId = request.headers.get('svix-id') |
| 87 | + const svixTimestamp = request.headers.get('svix-timestamp') |
| 88 | + const svixSignature = request.headers.get('svix-signature') |
| 89 | + |
| 90 | + if (!svixId || !svixTimestamp || !svixSignature) { |
| 91 | + logger.warn(`[${requestId}] Resend webhook missing Svix signature headers`) |
| 92 | + return new NextResponse('Unauthorized - Missing Resend signature headers', { status: 401 }) |
| 93 | + } |
| 94 | + |
| 95 | + if (!verifySvixSignature(signingSecret, svixId, svixTimestamp, svixSignature, rawBody)) { |
| 96 | + logger.warn(`[${requestId}] Resend Svix signature verification failed`) |
| 97 | + return new NextResponse('Unauthorized - Invalid Resend signature', { status: 401 }) |
| 98 | + } |
| 99 | + |
| 100 | + return null |
| 101 | + }, |
| 102 | + |
35 | 103 | async formatInput({ body }: FormatInputContext): Promise<FormatInputResult> { |
36 | 104 | const payload = body as Record<string, unknown> |
37 | 105 | const data = payload.data as Record<string, unknown> | undefined |
@@ -134,7 +202,12 @@ export const resendHandler: WebhookProviderHandler = { |
134 | 202 | } |
135 | 203 | ) |
136 | 204 |
|
137 | | - return { providerConfigUpdates: { externalId: responseBody.id } } |
| 205 | + return { |
| 206 | + providerConfigUpdates: { |
| 207 | + externalId: responseBody.id, |
| 208 | + signingSecret: responseBody.signing_secret, |
| 209 | + }, |
| 210 | + } |
138 | 211 | } catch (error: unknown) { |
139 | 212 | const err = error as Error |
140 | 213 | logger.error( |
|
0 commit comments