Anyone who knows your webhook URL could send it fake requests. Every request from TijaraQ is signed with your webhook's signing secret. Check the signature, and ignore any request that doesn't match.
"A signing secret is generated automatically once this webhook is created - use it to verify the X-Webhook-Signature header on requests you receive."
Header |
|
Algorithm | HMAC-SHA256 |
Signed content | The raw request body, exactly as received |
Key | Your webhook's Signing secret |
Read the raw body. Don't parse and re-encode the JSON first.
Calculate the HMAC-SHA256 of the body, using your signing secret.
Add sha256= in front, and compare it with the header using a constant-time comparison.
If they don't match, reply with 401 and ignore the request.
const crypto = require('crypto');
function isValid(rawBody, header, secret) {
const expected = 'sha256=' + crypto.createHmac('sha256', secret).update(rawBody).digest('hex');
return header && crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(header));
}
$expected = 'sha256=' . hash_hmac('sha256', $rawBody, $secret);
$valid = hash_equals($expected, $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '');
import hmac, hashlib
expected = 'sha256=' + hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
valid = hmac.compare_digest(expected, header)
Keep the secret private. Store it in an environment variable, not in your code.
Ignore duplicates using the X-Webhook-Id header, because a retried delivery has the same ID.
Need a new secret? Delete the webhook and create it again.