Signature Verification
Always verify webhook signatures to ensure requests come from legitimate sources. Most services sign payloads with HMAC-SHA256.
// Verify webhook signature
const signature = request.headers['x-signature'];
const payload = JSON.stringify(request.body);
const expected = crypto
.createHmac('sha256', webhookSecret)
.update(payload)
.digest('hex');
if (signature !== expected) {
throw new Error('Invalid signature');
}Key Points:
- Store webhook secrets in environment variables
- Use constant-time comparison to prevent timing attacks
- Log failed verification attempts for security monitoring