Overview
Webhook signature verification ensures that incoming webhook requests are genuinely from Fin and have not been tampered with. This process uses HMAC (Hash-based Message Authentication Code) to validate each webhook payload.How It Works
- Extract the signature: Retrieve the signature value from the webhook request header
- Recalculate the signature: Use your secret key and the same hash algorithm to compute the HMAC signature for the received payload
- Compare signatures: Verify that your computed signature matches the one provided in the header
Verification Steps
Follow these steps to verify webhook signatures in your application:- Read the raw request body - Capture the HTTP body as raw bytes without any modifications or parsing
- Extract signature headers - Read the
x-fin-signatureandx-fin-signature-algorithmheaders from the request - Compute the HMAC - Using your webhook secret key and the algorithm specified in
x-fin-signature-algorithm, calculate: - Compare signatures - Accept the webhook only if your computed signature exactly matches the
x-fin-signatureheader value
Code Examples
Here are implementation examples in different programming languages:Best Practices
Store secrets securely
Store secrets securely
Never hardcode your webhook secret in your source code. Use environment variables or a secure secrets management system.
Use constant-time comparison
Use constant-time comparison
Always use constant-time comparison functions (like
crypto.timingSafeEqual in Node.js or hmac.compare_digest in Python) to prevent timing attacks.Preserve raw body
Preserve raw body
Ensure your web framework provides access to the raw request body before any parsing or modifications occur.
Handle errors gracefully
Handle errors gracefully
Return appropriate HTTP status codes (401 Unauthorized) for failed verifications without revealing specific error details.
Troubleshooting
If signature verification is failing:- Check the raw body: Ensure you’re using the exact raw bytes received, not a re-serialized version
- Verify the secret: Confirm you’re using the correct webhook secret from your Fin dashboard
- Check character encoding: Make sure you’re using UTF-8 encoding consistently
- Inspect headers: Verify that
x-fin-signatureandx-fin-signature-algorithmheaders are present - Test the algorithm: Confirm you’re using the algorithm specified in the
x-fin-signature-algorithmheader