A webhook endpoint is a public URL. Without a signature check, anyone who finds it can post fake orders or events. WooCommerce gives you what you need to verify that a delivery really came from your store.
What WooCommerce sends
Each delivery carries the event as JSON and a signature in the X-WC-Webhook-Signature header. It is a base64-encoded HMAC-SHA256 hash of the request body, made with the secret you entered when you created the webhook. Other headers name the topic and the webhook ID.
The check in PHP
$raw = file_get_contents('php://input'); // raw body, before json_decode
$received = $_SERVER['HTTP_X_WC_WEBHOOK_SIGNATURE'] ?? '';
$expected = base64_encode(hash_hmac('sha256', $raw, $secret, true));
if (!hash_equals($expected, $received)) {
http_response_code(401);
exit;
}Four mistakes that break it
- Hashing the decoded array or re-encoded JSON. Hash the exact raw bytes you received.
- Forgetting the true flag in hash_hmac, which returns hex instead of raw bytes before base64.
- Comparing with == or ===. Use hash_equals so the comparison takes the same time whatever the input.
- Letting a framework or a security plugin read and change the body before your code sees it.
When the header never arrives
Some hosts and proxies strip custom headers. If your signature is always empty, confirm that first. A fallback is to put a long random token in the webhook URL and check it as well, but treat it as a second layer, not a replacement for the signature.
Process each event once
A valid signature does not mean the event is new. WooCommerce can deliver again after a failure, so store the delivery or event ID and take a lock before you act. We cover that in our post on processing webhooks once.
Test it
- Send a request with a wrong secret and expect a 401.
- Send the same valid event twice and expect one effect.
- Return a 2xx quickly and do slow work in a queue, so WooCommerce does not see a timeout as a failure.
