Back to Full Documentation
Zero-Trust Security

Webhook Signatures Reference

Verify the authenticity of custom HTTPS alerts sent by PingPup using HMAC-SHA256 cryptographic signatures.

HTTP HeaderFormat / ExampleDescription
X-PingPup-Signaturet=1730390400,v1=5e8f4...UNIX timestamp and hex-encoded HMAC-SHA256 signature
X-PingPup-Timestamp1730390400UNIX epoch seconds when the webhook was generated
Anti-Replay Window

The signature is computed over {timestamp}.{rawBody} using your channel signing secret (whsec_...). Always enforce a 300-second (5 minute) anti-replay tolerance window.

Node.js / TypeScript Verification
import crypto from 'node:crypto';

/**
 * Verify a PingPup incoming webhook signature in Node.js / TypeScript.
 *
 * @param secret - Your channel signing secret (starts with whsec_...)
 * @param rawBody - Exact UTF-8 string payload received in HTTP request body
 * @param signatureHeader - Value of 'X-PingPup-Signature' header (format: t=<unix>,v1=<hex>)
 * @param toleranceSec - Maximum clock drift tolerance in seconds (default: 300s)
 */
export function verifyPingPupWebhook(
  secret: string,
  rawBody: string,
  signatureHeader?: string | null,
  toleranceSec = 300
): { valid: boolean; reason?: string } {
  if (!secret) return { valid: false, reason: 'missing_secret' };
  if (!signatureHeader) return { valid: false, reason: 'missing_signature_header' };

  // 1. Parse t=<unix> and v1=<hex>
  const match = signatureHeader.match(/t=(\d+),v1=([a-f0-9]+)/i);
  if (!match) return { valid: false, reason: 'invalid_signature_format' };

  const timestamp = parseInt(match[1], 10);
  const signatureHex = match[2].toLowerCase();

  // 2. Enforce 300s anti-replay window
  const now = Math.floor(Date.now() / 1000);
  if (Math.abs(now - timestamp) > toleranceSec) {
    return { valid: false, reason: 'timestamp_out_of_window' };
  }

  // 3. Compute HMAC-SHA256 over `${timestamp}.${rawBody}`
  const hmac = crypto.createHmac('sha256', secret);
  hmac.update(`${timestamp}.${rawBody}`, 'utf8');
  const expectedHex = hmac.digest('hex');

  // 4. Constant-time comparison to prevent timing attacks
  const expectedBuf = Buffer.from(expectedHex, 'utf8');
  const actualBuf = Buffer.from(signatureHex, 'utf8');

  if (expectedBuf.length !== actualBuf.length || !crypto.timingSafeEqual(expectedBuf, actualBuf)) {
    return { valid: false, reason: 'signature_mismatch' };
  }

  return { valid: true };
}
Python Verification
import hmac
import hashlib
import time
import re

def verify_pingpup_webhook(secret: str, raw_body: str, signature_header: str, tolerance_sec: int = 300) -> bool:
    """
    Verify incoming PingPup webhook signature in Python.
    Header format: X-PingPup-Signature: t=<unix_seconds>,v1=<hex_hmac_sha256>
    """
    if not secret or not signature_header:
        return False

    # 1. Parse timestamp and signature
    match = re.search(r't=(\d+),v1=([a-f0-9]+)', signature_header, re.IGNORECASE)
    if not match:
        return False

    timestamp = int(match.group(1))
    actual_sig = match.group(2).lower()

    # 2. Check 300-second anti-replay window
    now = int(time.time())
    if abs(now - timestamp) > tolerance_sec:
        return False

    # 3. Compute HMAC-SHA256 over f"{timestamp}.{raw_body}"
    payload_to_sign = f"{timestamp}.{raw_body}".encode('utf-8')
    expected_sig = hmac.new(
        secret.encode('utf-8'),
        payload_to_sign,
        hashlib.sha256
    ).hexdigest().lower()

    # 4. Constant-time comparison
    return hmac.compare_digest(expected_sig, actual_sig)