InBrott
target://race-coupon-ledger
hardOPEN

Double-Spend Ledger

race-conditiontoctouconcurrency

// Scenario

A billing service processes promotional coupon redemptions against a simulated store:

  • Python: redeem_coupon(store, user_id, coupon_code) (called concurrently across multiple threads!)
  • JavaScript: async redeemCoupon(store, userId, couponCode) (called concurrently via Promise.all!)

The store exposes get_balance(user_id) / set_balance(user_id, val) and is_used(user_id, code) / mark_used(user_id, code) (async in JS, blocking with simulated I/O yields in Python).

// The Vulnerability

Because the check (is_used) and state mutation (mark_used + set_balance) are not synchronized, concurrent requests for the same (user_id, coupon_code) pass the check simultaneously before either marks the coupon used — resulting in a classic TOCTOU double-spend!

// Your Task

Make redeem_coupon / redeemCoupon atomic with respect to concurrent invocations:

  • Use proper synchronization (a threading.Lock in Python, or an async per-key/store mutex queue in JavaScript) so only one redemption can check-and-commit at a time.
  • Credit +50 only once per (user_id, coupon_code) and return True for the single winning redemption and False for duplicates or invalid coupons (WELCOME50 is valid).

Sample Sanity Checks (1)

  • Single redemption credits +50
    Redeeming WELCOME50 once adds 50 to user balance
Hints (0/3)
initializing editor...

[idle] Press RUN to execute visible sample checks, or SUBMIT to run hidden exploit & functionality suites.