Webhook signature verification helps you check that messages you get really come from the right sender and not from someone pretending to be them.
Webhook signature verification in Rest API
1. Receive webhook request with headers and body. 2. Extract the signature from the request headers. 3. Use your secret key to create a hash of the request body. 4. Compare your hash with the signature from the header. 5. If they match, accept the webhook; otherwise, reject it.
The secret key is shared only between you and the webhook sender.
Hashing algorithms like HMAC with SHA256 are commonly used.
signature = request.headers['X-Signature'] computed_hash = hmac.new(secret_key, request.body, hashlib.sha256).hexdigest() if hmac.compare_digest(signature, computed_hash): process_webhook() else: reject_request()
const signature = req.headers['x-signature']; const computedHash = crypto.createHmac('sha256', secretKey) .update(req.body) .digest('hex'); if (crypto.timingSafeEqual(Buffer.from(signature, 'utf8'), Buffer.from(computedHash, 'utf8'))) { processWebhook(); } else { rejectRequest(); }
This program simulates receiving a webhook with a signature and verifies it using HMAC SHA256. It prints a message depending on whether the signature matches.
import hmac import hashlib # Simulated webhook request data request_body = b'{"order_id":12345,"status":"paid"}' secret_key = b'mysecretkey' # Simulate signature sent by webhook sender signature = hmac.new(secret_key, request_body, hashlib.sha256).hexdigest() # Function to verify webhook signature def verify_webhook(body, signature, secret): computed_hash = hmac.new(secret, body, hashlib.sha256).hexdigest() return hmac.compare_digest(signature, computed_hash) # Check the webhook if verify_webhook(request_body, signature, secret_key): print("Webhook verified: process the data") else: print("Webhook verification failed: reject the data")
Always use a timing-safe comparison function like hmac.compare_digest to avoid timing attacks.
Keep your secret key private and never share it publicly.
Check the webhook documentation for the exact header name and hashing method used.
Webhook signature verification confirms the webhook is from a trusted source.
It uses a secret key and hashing to compare signatures safely.
Always reject webhooks if the signature does not match.
Practice
Solution
Step 1: Understand webhook security
Webhook signature verification is used to check if the webhook request is really from the expected sender.Step 2: Identify the purpose of verification
This process confirms the webhook is trusted by comparing the signature with a secret key.Final Answer:
To confirm the webhook is from a trusted source -> Option AQuick Check:
Verification = Trusted source confirmation [OK]
- Confusing verification with encryption
- Thinking it speeds up webhook delivery
- Assuming it logs request time
Solution
Step 1: Understand HMAC function parameters
The correct order is secret key first, then message (payload), and the hashing algorithm.Step 2: Identify correct syntax and algorithm
hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest() uses hmac.new with secret encoded, payload encoded, and hashlib.sha256, which is correct.Final Answer:
hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest() -> Option AQuick Check:
Secret first, payload second, SHA256 used [OK]
- Swapping secret and payload order
- Using wrong hash function like md5
- Incorrect function call syntax
import hmac
import hashlib
secret = "mysecret"
payload = "testdata"
signature = hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest()
computed = hmac.new(secret.encode(), payload.encode(), hashlib.sha256).hexdigest()
if computed == signature:
print("Signature verified")
else:
print("Signature mismatch")Solution
Step 1: Analyze the signature comparison
The code compares the computed signature with the received signature and prints "Signature verified" if they match.Step 2: Determine output when signatures match
Since the signatures match, the output will be the success message "Signature verified".Final Answer:
"Signature verified" -> Option BQuick Check:
Matching signatures print verification message [OK]
- Confusing mismatch message as output
- Expecting an error instead of message
- Assuming no output (None)
import hmac
import hashlib
secret = "mysecret"
payload = "data"
signature = "abc123"
computed = hmac.new(secret, payload.encode(), hashlib.sha256).hexdigest()
if computed == signature:
print("Verified")
else:
print("Not verified")Solution
Step 1: Check data types for hmac.new
hmac.new requires the secret key as bytes, but secret is a string and not encoded.Step 2: Identify correct fix
Encoding secret with secret.encode() fixes the error; payload encoding is correct.Final Answer:
secret should be encoded before use in hmac.new -> Option CQuick Check:
Secret must be bytes, so encode it [OK]
- Not encoding secret key
- Changing payload encoding incorrectly
- Using wrong hash function
- Wrong comparison operator
Solution
Step 1: Retrieve the secret key
You need the secret key first to compute the HMAC signature.Step 2: Compute HMAC of the payload using the secret
Use the secret key and payload to compute the signature.Step 3: Compare computed signature with header signature
Check if the computed signature matches the one sent in the webhook header.Step 4: Accept webhook if signatures match
Only accept the webhook if the signatures are the same to ensure trust.Final Answer:
1. Retrieve the secret key 2. Compute HMAC of the payload using the secret 3. Compare computed signature with header signature 4. Accept webhook if signatures match -> Option DQuick Check:
Secret -> Compute -> Compare -> Accept [OK]
- Accepting webhook before verification
- Comparing before computing signature
- Rejecting on matching signatures
Ready to prove it?
Pass the test and this pattern moves unknown
