,v1=`. The hex is * HMAC-SHA256 over the literal text ".", keyed with the secret * of that endpoint. PHP hands that header over as HTTP_X_SENDPIER_SIGNATURE. * * For 24 hours after you renew the secret the header carries TWO signatures: * `t=,v1=,v1=`. * Collect every `v1` and accept the request if any one of them matches. This * is what lets you switch to the new secret at your own pace. */ function sendpier_signature_is_valid(string $secret, string $header, string $body, int $tolerance = TOLERANCE_SECONDS): bool { $timestamp = null; $candidates = []; foreach (explode(',', $header) as $part) { if (str_contains($part, '=')) { [$name, $value] = explode('=', trim($part), 2); if ($name === 't') { $timestamp = $value; } elseif ($name === 'v1' && $value !== '') { $candidates[] = $value; } } } if ($timestamp === null || $timestamp === '' || $candidates === []) { return false; } // Without the age check an intercepted request stays replayable for as long // as the secret lives: the signature itself never expires. if (abs(time() - (int) $timestamp) > $tolerance) { return false; } $expected = hash_hmac('sha256', $timestamp.'.'.$body, $secret); // Constant time, and for every value: a plain === leaks, through its own // running time, how many leading characters of a guess were right. Do not // stop at the first match, so the running time does not tell which of the // two signatures was the right one. $valid = false; foreach ($candidates as $candidate) { if (hash_equals($expected, $candidate)) { $valid = true; } } return $valid; } $secret = getenv('SENDPIER_WEBHOOK_SECRET'); if ($secret === false || $secret === '') { http_response_code(500); error_log('SENDPIER_WEBHOOK_SECRET is not set'); exit; } if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { http_response_code(405); exit; } // The raw bytes we sent. Re-serialising an object shifts a space or a key // order, and then the signature no longer matches. $body = (string) file_get_contents('php://input'); $header = (string) ($_SERVER['HTTP_X_SENDPIER_SIGNATURE'] ?? ''); if (! sendpier_signature_is_valid($secret, $header, $body)) { http_response_code(400); error_log('refused a webhook delivery with an invalid signature'); exit; } $event = json_decode($body, true); if (! is_array($event) || ! isset($event['id'], $event['event'])) { http_response_code(400); exit; } /* | Deduplicate on `id` (the same number as the X-SendPier-Delivery header) and | order on `occurred_at`. We queue every event once, but a delivery that | answers too late gets it again, and a retry from yesterday can arrive after a | fresh event. Process an event you already know silently and answer 200. | | if ($store->hasEvent($event['id'])) { http_response_code(200); exit; } | $store->queue($event); */ http_response_code(200); // Everything below runs after the answer is on its way. In a real application // this is where you hand the event to your queue worker instead. error_log(sprintf( 'webhook %s: %s for %s', (string) $event['id'], (string) $event['event'], (string) ($event['data']['to'] ?? $event['data']['domain'] ?? 'unknown') ));