|
| 1 | +import { Webhook } from "@trigger.dev/core/v3"; |
| 2 | +import { subtle } from "crypto"; |
| 3 | + |
| 4 | +/** |
| 5 | + * The type of error thrown when a webhook fails to parse or verify |
| 6 | + */ |
| 7 | +export class WebhookError extends Error { |
| 8 | + constructor(message: string) { |
| 9 | + super(message); |
| 10 | + this.name = "WebhookError"; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +/** Header name used for webhook signatures */ |
| 15 | +const SIGNATURE_HEADER_NAME = "x-trigger-signature-hmacsha256"; |
| 16 | + |
| 17 | +/** |
| 18 | + * Options for constructing a webhook event |
| 19 | + */ |
| 20 | +type ConstructEventOptions = { |
| 21 | + /** Raw payload as string or Buffer */ |
| 22 | + payload: string | Buffer; |
| 23 | + /** Signature header as string, Buffer, or string array */ |
| 24 | + header: string | Buffer | Array<string>; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Interface describing the webhook utilities |
| 29 | + */ |
| 30 | +interface Webhooks { |
| 31 | + /** |
| 32 | + * Constructs and validates a webhook event from an incoming request |
| 33 | + * @param request - Either a Request object or ConstructEventOptions containing the payload and signature |
| 34 | + * @param secret - Secret key used to verify the webhook signature |
| 35 | + * @returns Promise resolving to a validated AlertWebhook object |
| 36 | + * @throws {WebhookError} If validation fails or payload can't be parsed |
| 37 | + * |
| 38 | + * @example |
| 39 | + * // Using with Request object |
| 40 | + * const event = await webhooks.constructEvent(request, "webhook_secret"); |
| 41 | + * |
| 42 | + * @example |
| 43 | + * // Using with manual options |
| 44 | + * const event = await webhooks.constructEvent({ |
| 45 | + * payload: rawBody, |
| 46 | + * header: signatureHeader |
| 47 | + * }, "webhook_secret"); |
| 48 | + */ |
| 49 | + constructEvent(request: ConstructEventOptions | Request, secret: string): Promise<Webhook>; |
| 50 | + |
| 51 | + /** Header name used for webhook signatures */ |
| 52 | + SIGNATURE_HEADER_NAME: string; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Webhook utilities for handling incoming webhook requests |
| 57 | + */ |
| 58 | +export const webhooks: Webhooks = { |
| 59 | + constructEvent, |
| 60 | + SIGNATURE_HEADER_NAME, |
| 61 | +}; |
| 62 | + |
| 63 | +async function constructEvent( |
| 64 | + request: ConstructEventOptions | Request, |
| 65 | + secret: string |
| 66 | +): Promise<Webhook> { |
| 67 | + let payload: string; |
| 68 | + let signature: string; |
| 69 | + |
| 70 | + if (request instanceof Request) { |
| 71 | + if (!secret) { |
| 72 | + throw new WebhookError("Secret is required when passing a Request object"); |
| 73 | + } |
| 74 | + |
| 75 | + const signatureHeader = request.headers.get(SIGNATURE_HEADER_NAME); |
| 76 | + if (!signatureHeader) { |
| 77 | + throw new WebhookError("No signature header found"); |
| 78 | + } |
| 79 | + signature = signatureHeader; |
| 80 | + |
| 81 | + payload = await request.text(); |
| 82 | + } else { |
| 83 | + payload = request.payload.toString(); |
| 84 | + |
| 85 | + if (Array.isArray(request.header)) { |
| 86 | + throw new WebhookError("Signature header cannot be an array"); |
| 87 | + } |
| 88 | + signature = request.header.toString(); |
| 89 | + } |
| 90 | + |
| 91 | + // Verify the signature |
| 92 | + const isValid = await verifySignature(payload, signature, secret); |
| 93 | + |
| 94 | + if (!isValid) { |
| 95 | + throw new WebhookError("Invalid signature"); |
| 96 | + } |
| 97 | + |
| 98 | + // Parse and validate the payload |
| 99 | + try { |
| 100 | + const jsonPayload = JSON.parse(payload); |
| 101 | + const parsedPayload = Webhook.parse(jsonPayload); |
| 102 | + return parsedPayload; |
| 103 | + } catch (error) { |
| 104 | + if (error instanceof Error) { |
| 105 | + throw new WebhookError(`Webhook parsing failed: ${error.message}`); |
| 106 | + } |
| 107 | + throw new WebhookError("Webhook parsing failed"); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Verifies the signature of a webhook payload |
| 113 | + * @param payload - Raw payload string to verify |
| 114 | + * @param signature - Expected signature to check against |
| 115 | + * @param secret - Secret key used to generate the signature |
| 116 | + * @returns Promise resolving to boolean indicating if signature is valid |
| 117 | + * @throws {WebhookError} If signature verification process fails |
| 118 | + * |
| 119 | + * @example |
| 120 | + * const isValid = await verifySignature( |
| 121 | + * '{"event": "test"}', |
| 122 | + * "abc123signature", |
| 123 | + * "webhook_secret" |
| 124 | + * ); |
| 125 | + */ |
| 126 | +async function verifySignature( |
| 127 | + payload: string, |
| 128 | + signature: string, |
| 129 | + secret: string |
| 130 | +): Promise<boolean> { |
| 131 | + try { |
| 132 | + // Convert the payload and secret to buffers |
| 133 | + const hashPayload = Buffer.from(payload, "utf-8"); |
| 134 | + const hmacSecret = Buffer.from(secret, "utf-8"); |
| 135 | + |
| 136 | + // Import the secret key |
| 137 | + const key = await subtle.importKey( |
| 138 | + "raw", |
| 139 | + hmacSecret, |
| 140 | + { name: "HMAC", hash: "SHA-256" }, |
| 141 | + false, |
| 142 | + ["sign", "verify"] |
| 143 | + ); |
| 144 | + |
| 145 | + // Calculate the expected signature |
| 146 | + const actualSignature = await subtle.sign("HMAC", key, hashPayload); |
| 147 | + const actualSignatureHex = Buffer.from(actualSignature).toString("hex"); |
| 148 | + |
| 149 | + // Compare signatures using timing-safe comparison |
| 150 | + return timingSafeEqual(signature, actualSignatureHex); |
| 151 | + } catch (error) { |
| 152 | + throw new WebhookError("Signature verification failed"); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// Timing-safe comparison to prevent timing attacks |
| 157 | +function timingSafeEqual(a: string, b: string): boolean { |
| 158 | + if (a.length !== b.length) { |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + let result = 0; |
| 163 | + for (let i = 0; i < a.length; i++) { |
| 164 | + result |= a.charCodeAt(i) ^ b.charCodeAt(i); |
| 165 | + } |
| 166 | + return result === 0; |
| 167 | +} |
0 commit comments