Web / API Series

📨 A Top 1% Hands-On for Building a Webhook Receiver Yourself and Experiencing Signature Verification and Handling a Retried Delivery

Introduction

This article is part of the Top 1% Series Complete Article Guide, and the sixth article in the Web/API Series. You can follow along with one of the Ubuntu Server environments set up in the Hands-On Prep Manual.

Prerequisite Knowledge

Getting the Big Picture

This hands-on covers four steps.

graph LR
    Step1["Step 1<br/>Write a script that sends<br/>a signed webhook"]
    Step2["Step 2<br/>Build a receiver that<br/>verifies the signature"]
    Step3["Step 3<br/>Confirm a tampered<br/>request gets rejected"]
    Step4["Step 4<br/>Confirm handling<br/>of a duplicate delivery"]
    Step1 --> Step2 --> Step3 --> Step4

Hands-On Steps

Step 1: Write a Script That Sends a Signed Webhook

Using a secret string shared ahead of time between the sender and receiver (the shared secret), write a script that computes the payload’s HMAC-SHA256 signature and sends it. Save it as sender.py.

import hmac
import hashlib
import json
import urllib.request

SHARED_SECRET = b"my-shared-secret"

def send_webhook(event_id, payload_dict):
    payload = json.dumps(payload_dict).encode()
    signature = hmac.new(SHARED_SECRET, payload, hashlib.sha256).hexdigest()
    req = urllib.request.Request(
        "http://localhost:8000/webhook",
        data=payload,
        headers={
            "Content-Type": "application/json",
            "X-Event-Id": event_id,
            "X-Signature": signature,
        },
    )
    with urllib.request.urlopen(req) as resp:
        print(resp.status, resp.read())

send_webhook("evt_001", {"type": "payment.succeeded", "amount": 1000})

Step 2: Build a Receiver That Verifies the Signature

The receiver side computes the signature itself, from the received payload, using the same shared secret, and checks whether it matches the signature that was sent. Save it as receiver.py.

import hmac
import hashlib
from http.server import BaseHTTPRequestHandler, HTTPServer

SHARED_SECRET = b"my-shared-secret"
processed_event_ids = set()

class Handler(BaseHTTPRequestHandler):
    def do_POST(self):
        length = int(self.headers.get('Content-Length', 0))
        payload = self.rfile.read(length)
        received_signature = self.headers.get('X-Signature', '')
        event_id = self.headers.get('X-Event-Id', '')

        expected_signature = hmac.new(SHARED_SECRET, payload, hashlib.sha256).hexdigest()

        if not hmac.compare_digest(expected_signature, received_signature):
            self.send_response(401)
            self.end_headers()
            self.wfile.write(b'{"error": "invalid signature"}')
            return

        if event_id in processed_event_ids:
            print(f"Event {event_id} already processed, skipping")
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b'{"status": "already processed"}')
            return

        processed_event_ids.add(event_id)
        print(f"Processing event {event_id}: {payload}")
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b'{"status": "processed"}')

HTTPServer(('0.0.0.0', 8000), Handler).serve_forever()
python3 receiver.py &
python3 sender.py

Output:

Processing event evt_001: b'{"type": "payment.succeeded", "amount": 1000}'
200 b'{"status": "processed"}'

You confirmed a webhook with a correct signature got processed normally.

Why Use the Dedicated `hmac.compare_digest` Comparison Function?

When checking whether two signatures match, avoid a plain string comparison (==). A normal string comparison stops immediately the moment it hits a mismatch, which creates a risk: the time it takes to find a mismatch leaks, bit by bit, exactly how much of the signature matched so far — letting an attacker gradually infer it. This is called a timing attack. hmac.compare_digest is implemented to always take a constant amount of time regardless of the string’s length, closing off this timing attack. It looks like an ordinary == comparison on the surface, but its internal implementation is entirely different — that distinction matters.

Step 3: Confirm a Tampered Request Gets Rejected

Modify sender.py slightly to compute the signature first, then send a payload whose content has been tampered with afterward.

import hmac
import hashlib
import json
import urllib.request

SHARED_SECRET = b"my-shared-secret"
payload_dict = {"type": "payment.succeeded", "amount": 1000}
payload = json.dumps(payload_dict).encode()
signature = hmac.new(SHARED_SECRET, payload, hashlib.sha256).hexdigest()

# Tamper with just the amount, after the signature was computed
tampered_payload = json.dumps({"type": "payment.succeeded", "amount": 999999}).encode()

req = urllib.request.Request(
    "http://localhost:8000/webhook",
    data=tampered_payload,
    headers={"X-Event-Id": "evt_002", "X-Signature": signature},
)
try:
    urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
    print(e.code, e.read())

Output:

401 b'{"error": "invalid signature"}'

Because the payload’s content (the amount) got changed, the signature the receiver computed itself no longer matched the signature that was sent, so it was correctly rejected with a 401 error. A third party who doesn’t know the shared secret can never compute a valid signature for their tampered payload, so they can’t slip past this check.

Step 4: Confirm Handling of a Duplicate (Retried) Delivery

A real-world webhook sender (Stripe, say) may retry sending the same event if it never receives a response from the receiver. Reproduce this behavior by sending Step 1’s request again, with the same event_id.

python3 sender.py
python3 sender.py

Output of the second run:

200 b'{"status": "already processed"}'

Checking the receiver’s log, the first run shows Processing event evt_001, while the second shows Event evt_001 already processed, skipping — confirming the actual processing (just a print here, but in real-world practice, something significant like crediting an amount in a database) never ran twice.

What a Pro Sees Here (Top 1% Understanding)

The Baseline Assumption for Handling an Incoming Webhook Is: Treat It as “Untrusted Input”

A webhook receiver endpoint might look like an internal API on the surface, but it’s actually an endpoint published to the internet, which anyone can send a request to. Skip the signature-verification step, and a third party who doesn’t know the shared secret can send a forged “payment completed” notification — shipping a product even though no payment ever actually happened — a concrete, real-world harm. Just as HTTP request smuggling’s defense was built on the idea of rejecting contradictory input, handling an incoming webhook follows the same standard real-world practice: never unconditionally trust input that merely claims a given sender.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. A webhook that should be valid gets rejected with a 401 error: Check that the shared secret’s value exactly matches between the sender and receiver. Also check whether the payload’s exact string representation used for the signature calculation (whitespace, newlines, and similar) matches between the two sides.
  2. The same event gets processed multiple times: Check whether the event-ID deduplication mechanism is implemented correctly. In real-world practice, this event-ID record needs to live in a persistent database, not an in-memory set.
  3. An event that should have been retried never arrives at all: Check the sender’s own retry policy (how many times, at what interval).

Summary

Takeaways to Apply Today

  1. When implementing a webhook receiver, build the habit of implementing both signature verification and deduplication, as two genuinely separate defenses.
  2. Watch out for the assumption “it’s safe because it uses HTTPS” — encryption and verifying the sender’s legitimacy are separate problems.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).