Test discount or redeem endpoints for race conditions with a grouped burst of requests

Pick an endpoint that increments or decrements a limited resource once per use: a one-time coupon, a referral credit, a "redeem points" action, or a vote. Send the same authenticated request 20-30 times at once instead of one at a time, keeping every header and body byte identical except what the server must see as a fresh connection.

With curl, fire them together rather than sequentially:

code
for i in $(seq 1 25); do
  curl -s -o /dev/null -w "%{http_code}\n" \
    -X POST https://target.example/api/coupon/redeem \
    -H "Authorization: Bearer <your-token>" \
    -H "Content-Type: application/json" \
    --data '{"code":"<your-test-coupon>"}' &
done; wait

This works because many redeem/apply handlers read the current balance, compute the new value, and write it back in three separate steps without a row lock or an atomic UPDATE ... WHERE balance >= cost. Requests that land inside that read-write window all see the same starting balance.

Proof of impact: the coupon applies more than once, or the balance goes negative, when it should only ever apply once. A single 200 among expected 409/400 responses is not enough by itself; check the account's actual balance or order total afterward. Only do this against your own test account and with the program's race-condition testing explicitly in scope.

Follow #concurrency, #coupon-abuse, #race-condition or #toctou for new tips like this one, on your notifications page and, if you choose, in a daily or weekly email.

Comments

No comments yet.