Start learning free

LESSON 66 OF 76 · SECURITY: FIND AND FIX

Project: Review a Payments Callback

Practice

When a customer pays, the payment service calls your shop back: it POSTs an event to /webhooks/payments, and for a payment.succeeded event the shop adds the amount to the customer's credit. Every real event carries an X-Signature header: an HMAC of the body, made with WEBHOOK_SECRET, which only the payment service and the shop know. The callback works, but a review finds four holes.

The four attacks. 1. No signature check: the shop computes the expected signature but never compares it, so anyone can post a made-up event and get free credit. 2. No replay protection: a real, signed event sent a second time is paid a second time, so one caught event can be cashed again and again. 3. A timing-unsafe compare: != stops at the first wrong character, so the time a wrong guess takes tells the attacker how much of it was right, and a signature can be found one character at a time. 4. The card number in the log: every event is logged with the full card number, so anyone who reads the logs gets the customers' cards.

hmac.compare_digest(a, b) answers True or False like a == b, but takes the same time however many characters match. Here it guards an API key on another route:

@app.get("/reports")
def reports():
    key = request.headers.get("X-Api-Key", "")
    if not hmac.compare_digest(key, REPORTS_KEY):
        return jsonify({"error": "Bad key"}), 401
    return jsonify(REPORTS)

A real event looks like {"id": "evt_1001", "type": "payment.succeeded", "data": {"customer_id": 1, "amount": 50, "card": "4242 4242 4242 4242"}}. The check sends signed events, a copy of one, and two forged ones, then reads the credit (/check/credit) and the log lines (/_test/log) through its own routes. When you're done, the answers are:

POST /webhooks/payments  evt_1001, Ana +50, signed  →  200  {"received": true}
POST /webhooks/payments  evt_1001 again, signed  →  200  {"received": true}
POST /webhooks/payments  evt_6666, Ben +500, no X-Signature  →  400  {"error": "Bad signature"}
POST /webhooks/payments  evt_6667, Ben +500, a guessed signature  →  400  {"error": "Bad signature"}
POST /webhooks/payments  evt_1002, Chloe +20, signed  →  200  {"received": true}
GET  /check/credit  →  200  [{"credit": 50, "name": "Ana Pop"}, {"credit": 20, "name": "Chloe Ng"}]
GET  /_test/log  →  200  ["Payment event evt_1001: {'customer_id': 1, 'amount': 50, 'card': '**** 4242'}",
                          "Payment event evt_1002: {'customer_id': 3, 'amount': 20, 'card': '**** 4444'}"]

Your task: close the four holes in payment_webhook. Answer 400 with {"error": "Bad signature"} when hmac.compare_digest says the signature doesn't match. Keep the ids of handled events in a list SEEN_EVENTS, and answer an id you've already seen with {"received": true} and nothing else. Log the card as "**** " plus its last 4 characters.


Assignment
Follow the Coding Tutorial to check the signature safely, pay each event only once and mask the card in the log.


Hint
Check the signature before you read the event, so a forged event never reaches the rest of the code. A replay still answers 200: the event is real, and the payment service should stop resending it. Use compare_digest, not !=: the check notices a signature compared with == or !=.


STEP 1 OF 7
    Ctrl + Enter to check