Skip to Content
Docs

Verify Incoming Alert Webhooks

A generic webhook destination receives a signed JSON POST at its configured HTTPS URL. Build a receiver that verifies the raw request, checks freshness, and deduplicates accepted deliveries. Slack destinations use Slack's payload and do not use this signing contract. An arbitrary third-party notification endpoint is not automatically compatible.

Retrieve the Destination Key

After creating the destination, call POST /destinations/{id}/signing-key with no body. Each reveal requires authentication, tenant membership, and contextual:alert-destinations:update. Ordinary list, get, create, update, and apply responses do not expose the URL credential or signing key.

The sensitive response includes:

Field Meaning
destinationId Destination whose key was revealed.
signingKey Canonical base64 encoding of 32 key bytes.
encoding / algorithm base64 / HMAC-SHA256.
signatureVersion / keyDerivationVersion v1 / 2.
signatureHeader x-contextual-signature.
timestampHeader x-contextual-signature-timestamp.
deliveryIdHeader x-contextual-delivery-id.

The response uses Cache-Control: no-store. Store the key as a receiver secret. Do not log it or embed it in client-side code. Decode it from base64 to obtain the HMAC key bytes.

Verify Raw Bytes

The signature input is the UTF-8 string v1:<timestamp>: immediately followed by the exact raw body bytes. The timestamp is Unix time in whole seconds. The signature header is v1= followed by the lowercase hexadecimal SHA-256 HMAC.

Capture the body before JSON middleware transforms it. Re-serializing parsed JSON changes the signature input. The Node.js example checks the signature and time window. Before processing a notification, validate the signed envelope and deduplicate the delivery.

JavaScript
import { createHmac, timingSafeEqual } from 'node:crypto';

export function verifySignature(rawBody, headers, signingKey, nowSeconds) {
  if (!Buffer.isBuffer(rawBody)) return false;
  const key = Buffer.from(signingKey, 'base64');
  if (key.length !== 32 || key.toString('base64') !== signingKey) return false;
  const timestamp = headers['x-contextual-signature-timestamp'];
  const signature = headers['x-contextual-signature'];
  if (typeof timestamp !== 'string' || !/^(0|[1-9][0-9]*)$/.test(timestamp)) return false;
  if (typeof signature !== 'string' || !/^v1=[0-9a-f]{64}$/.test(signature)) return false;
  const seconds = Number(timestamp);
  if (!Number.isSafeInteger(seconds) || !Number.isSafeInteger(nowSeconds)) return false;
  if (Math.abs(nowSeconds - seconds) > 300) return false;
  const expected = createHmac('sha256', key)
    .update(`v1:${timestamp}:`).update(rawBody).digest();
  const received = Buffer.from(signature.slice(3), 'hex');
  return received.length === expected.length && timingSafeEqual(received, expected);
}

Pass your server's current Unix time as nowSeconds, for example Math.floor(Date.now() / 1000). This example permits five minutes of clock difference in either direction. Reject older or excessively future timestamps and maintain synchronized clocks. Reject missing, duplicate, or malformed headers at your HTTP boundary, and impose an appropriate request-body size limit.

Validate the Signed Envelope

Only after successful signature verification, parse the JSON and check its version, kind, destination, delivery identity, and required structure. Version 1 contains:

Field Meaning
version Envelope version, 1.
kind condition or event.
deliveryId Signed identifier used for deduplication.
sentAt, transitionAt Send time and condition/event transition time.
subscriptionId, destinationId Route identifiers.
alert Alert state, identity, labels, and available observation details.
suppressedSinceLastNotification Optional suppression count.

Require x-contextual-delivery-id to equal the signed envelope's deliveryId. Check that destinationId belongs to the receiver/key configuration you selected. Never trust an unsigned header alone as the identity of a delivery.

Handle Retries and Replay

Atomically record accepted delivery IDs in a durable store shared by every receiver replica. Combine deduplication with durable enqueueing or processing so a crash cannot mark an unprocessed notification as complete. A duplicate should receive a successful acknowledgment without performing the action twice.

Keep deduplication records for your delivery and replay retention requirements, at least through the accepted timestamp window. A five-minute tolerance can admit a timestamp up to five minutes ahead, so calculate expiry from the accepted signed timestamp, not only from arrival time. Fail safely if deduplication storage is unavailable. A valid signature alone does not prevent replay.

Key Lifecycle

Repeated reveals and destination URL or metadata updates retain the same signing key. URL replacement is not signing-key rotation. Deleting and recreating the same destination ID is also not a reliable way to rotate its key.

Coordinate receiver reconfiguration with your platform administrator when signing keys change. Retrieve the destination key through the supported endpoint and update your receiver's secret storage.