Definitive Engineering Takeaway: Step-by-step PHP 8.3 guide to validating raw webhook request bodies with timing-safe HMAC-SHA256 signatures and rejecting replay attacks on custom /wp-json/ endpoints.
- Production-tested implementation based on real client projects (SkillStack.ie, FloppyFabrics.ie, Aoife Brogan Ballet).
- Zero-bloat execution: strict attention to DOM depth, Core Web Vitals (sub-1.2s mobile LCP), and clean PHP hooks.
- Verified code payloads formatted for rapid copy-paste and direct deployment.
Publicly reachable WordPress REST routes (/wp-json/vendor/v1/webhook) are routinely probed by automated scanners. Relying on a static query-string token (?secret=xyz) exposes credentials in server access logs, proxy caches, and browser history. Production integrations must verify cryptographic HMAC-SHA256 signatures over the exact raw request body.
1. Why You Must Hash the Raw Payload - Never Re-Encoded JSON
A common WordPress bug is calling $request->get_json_params() and re-encoding it with wp_json_encode() before computing the digest. Even minor differences in whitespace, Unicode escaping, or key order produce a completely different SHA-256 hash. Always read $request->get_body() verbatim.
function eosite_verify_webhook_permission(WP_REST_Request $request) {
$raw_body = $request->get_body();
$signature = (string) $request->get_header('x-wc-webhook-signature');
$timestamp = (int) $request->get_header('x-webhook-timestamp');
if ($raw_body === '' || $signature === '') {
return new WP_Error('missing_signature', 'Webhook signature required.', ['status' => 401]);
}
// Optional replay window check (reject requests older than 300 seconds)
if ($timestamp > 0 && abs(time() - $timestamp) > 300) {
return new WP_Error('stale_webhook', 'Timestamp outside replay window.', ['status' => 401]);
}
$secret = defined('EOSITE_WEBHOOK_SECRET') ? EOSITE_WEBHOOK_SECRET : '';
$expected = base64_encode(hash_hmac('sha256', $raw_body, $secret, true));
if ($secret === '' || !hash_equals($expected, trim($signature))) {
return new WP_Error('invalid_signature', 'Signature verification failed.', ['status' => 403]);
}
return true;
}
2. Timing-Safe Comparison & Secret Storage
- Always use
hash_equals(): Standard===comparisons leak timing side-channels across byte mismatches. - Keep secrets out of
wp_options: Define signing secrets inwp-config.phpor environment variables so database backups and autoloaded options never expose live webhook keys. - Enforce Replay Deduplication: Store each processed delivery ID (e.g.
X-WC-Webhook-Delivery-ID) in a short-lived transient or Redis key for 15 minutes to drop duplicate retries cleanly.
| Provider | Signature Header | Encoding Format |
|---|---|---|
| WooCommerce Webhooks | X-WC-Webhook-Signature | Base64 of raw binary HMAC-SHA256 |
| GitHub Webhooks | X-Hub-Signature-256 | sha256=<hex> |
| Stripe Webhooks | Stripe-Signature | t=<ts>,v1=<hex> over timestamp.payload |
Test Your Signatures Before Deploying
Paste any sample payload and signing secret directly into our browser-based WooCommerce HMAC-SHA256 Webhook Inspector (zero server logging). For full custom endpoint hardening or third-party ERP/LMS bridges, review Custom WordPress API & Webhook Development or our SkillStack.ie engineering case study.
Need a Quick Fix ($49+) or Dedicated WordPress Engineer?
Start with one small task (SMTP, GA4, Local SEO schema, mobile CSS) or tap directly into 100+ Irish builds & 1,000+ AU QA standards at $25-$30/hr.