learning-it by example

#306 Forward deployed engineer interviews 6.1 min

Webhooks Done Right: Signatures, Retries and Replays

Anyone can POST to your webhook URL. Four questions to ask of every message: forged, changed, old or repeated?

1. Before acting on a webhook message, which four things does the video say a receiver should check?
2. In the Python webhook receiver demo (signed messages, made-up secret), what happened to the genuine message sent a second time with the same event id?
3. Why does the receiver compare signatures with hmac.compare_digest and not a plain equals?
4. In the Python webhook sender demo, the flaky line dropped the first two tries after the receiver had done the work. What did the demo print?
5. What happens to a webhook event that the sender cannot deliver after all its tries?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. Run the attacks

    Copy the code folder first. Run python run.py. Match each printed line to one of the four checks: signature, unchanged body, age and event id. You should see 401 for the forged and the changed message, 400 for the old one, and a 200 duplicate for the repeat.

    A hint

    The receiver checks in this order: signature, age, then event id.

  2. Widen the time window

    In webhook.py change TOLERANCE = 300 to TOLERANCE = 900. Predict what happens to the message that is 10 minutes old, then run python run.py. With this change it prints (200, 'processed') and evt_3 joins the processed list. What does a wider window let an attacker do? Then change TOLERANCE back to 300, because the next exercise needs it.

    A hint

    10 minutes is 600 seconds.

  3. Find the edge of the window

    In run.py change clock.now - 600 to clock.now - 301 and run it, then change it to clock.now - 299 and run again. Predict each result first. At 301 seconds the old message prints (400, 'too old'), and at 299 it prints (200, 'processed'). The printed label still says '10 minutes old', which is no longer true. When you have finished, change clock.now - 299 back to clock.now - 600.

    A hint

    The receiver refuses a message more than TOLERANCE seconds away from now.

  4. Make the line worse

    In run.py in the flaky function change calls["n"] <= 2 to calls["n"] <= 4. The sender makes at most 4 tries. Predict what the flaky line and the dead letters will show, then run it. With this change the flaky line prints 'delivered on try 0' (a stale label: it means the sender gave up), dead letters become ['evt_4', 'evt_5'], and evt_4 is still processed exactly once. When you have finished, change 4 back to 2.

    A hint

    The receiver does the work before the reply is lost, so giving up does not undo it.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

No network: the line is a function and the clock is a number. The secret is made up and the signing is simplified (the Standard Webhooks scheme also signs the message id). The seen list is in memory and never expires. A request with a missing header raises an error instead of a 401. Sources are vendor pages, not the specification.

Up next in Forward deployed engineer interviews

More in Forward deployed engineer interviews

All Forward deployed engineer interviews videos

Related videos