Race conditions let one coupon, gift card or credit be used twice

Last updated November 13, 2026
Steps
- List every “only once” or “only up to X” rule in the app: one-time coupons, gift card and store-credit balances, free-trial or AI credit counters, referral bonuses, inventory for limited drops, votes and ratings, and withdrawal or payout limits.
- For each, find the code. The risky shape is read-then-write: SELECT (or ORM find) to check the limit, some application logic, then a separate UPDATE or save().
- Replace it with one conditional write the database enforces—for example UPDATE coupons SET used_at = now() WHERE code = $1 AND used_at IS NULL RETURNING id—and only apply the discount if a row came back.
- For balances, decrement in the same statement with a guard (… SET balance = balance - $amt WHERE id = $id AND balance >= $amt), or wrap the read and write in a transaction that locks the row (SELECT … FOR UPDATE in PostgreSQL).
- Add database constraints as a backstop: a UNIQUE index on (coupon_id, user_id) or on redemption records, and CHECK (balance >= 0).
- Don’t use session variables, in-memory flags or a client-side “disabled” button to enforce the limit—they don’t stop parallel requests.
- For payment and credit-granting endpoints, accept an idempotency key so retries and double-clicks are processed once (Stripe’s API supports idempotent requests).
- Retest in staging by firing 20–30 identical requests in parallel at the redeem endpoint. Exactly one should succeed.
Why it matters
MITRE CWE-362 describes code that relies on exclusive access to a shared resource while a timing window lets another thread change it. PortSwigger’s Web Security Academy shows how a one-time discount code can be claimed many times inside a fraction-of-a-second race window, and lists gift cards, ratings, balance withdrawals and rate limits as common targets. Its prevention advice: make state changes on sensitive endpoints atomic using the datastore’s concurrency features, use uniqueness constraints as defense in depth, and don’t use one storage layer (such as sessions) to secure another.
Sources
MITRE CWE-362: Concurrent Execution using Shared Resource with Improper Synchronization — https://cwe.mitre.org/data/definitions/362.html
PortSwigger Web Security Academy: Race conditions — https://portswigger.net/web-security/race-conditions
PostgreSQL documentation: Explicit locking — https://www.postgresql.org/docs/current/explicit-locking.html
Stripe API reference: Idempotent requests — https://docs.stripe.com/api/idempotent_requests
Checked at the source on November 7, 2026.
Related patterns
- Checkout prices, totals and discounts taken from the browser
- No rate limiting on login, signup or AI endpoints
- Stripe webhooks not verified
- IDOR in vibe-coded apps: changing an ID in the URL to see someone else's data
- Mass assignment: users can set fields like role or is_admin
- Password reset flows that leak accounts or trust the Host header
- Webhooks processed twice: duplicate payments, emails and orders
Found a problem you'd rather not fix yourself?
Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.
Get it fixed by StorenTechVibe Security Weekly
One email a week with new failure patterns and how to fix them.
By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.