Skip to content

Web Race Conditions & Business Logic Attacks

How to test and prevent web application race conditions (TOCTOU): coupon double-spending, concurrent transfers, database locks, and idempotency.

By BugSnaps Security Research · 2 min read · Updated

Add BugSnaps as a preferred source on Google(opens in a new tab)

Want more of our security guides? Select BugSnaps and confirm your choice on Google.

Race conditions happen when an application handles concurrent HTTP requests that read and modify shared state without atomic locking or transaction isolation. By sending a burst of parallel requests within the narrow Time-of-Check to Time-of-Use (TOCTOU) window between a validation query and a state update, an attacker can force a single-use action to succeed multiple times.

How parallel requests trigger double-spends and limit bypasses

Consider a promotional credit endpoint that checks `if (!coupon.redeemed)`, adds credit to the user's wallet, and then runs `UPDATE coupons SET redeemed = true`. If those three steps execute as separate non-locked queries, sending ten concurrent HTTP/2 requests in a single TCP packet allows multiple worker threads to read `redeemed = false` before the first thread commits its update:

  • Coupon, referral, and gift-card double redemption: claiming a single-use promotional code or free-trial credit multiple times across parallel requests.
  • Balance withdrawal and transfer over-drafting: initiating concurrent transfers that each pass the `balance >= amount` check before the balance column is decremented.
  • Quota and seat-limit bypass: creating more team invitations, API keys, or scan jobs than a plan allows by racing the count check.
  • Multi-step workflow state confusion: changing cart items in one request at the exact moment a second request finalizes checkout.

Preventing race conditions with atomic queries, locks, and idempotency

Application-level `if` statements in Node.js, Python, or Go memory do not serialize concurrent database writes across multiple server instances. To close TOCTOU windows reliably, push the state check and state transition into a single atomic database operation:

  1. Use conditional atomic updates: execute `UPDATE coupons SET redeemed = true WHERE id = $1 AND redeemed = false` and check that the affected row count equals 1 before crediting the account.
  2. Use row-level locking or serializable transactions: lock the balance or inventory row with `SELECT ... FOR UPDATE` inside the same database transaction that records the debit.
  3. Enforce unique constraints and idempotency keys: add a database `UNIQUE (user_id, coupon_id)` constraint and require an `Idempotency-Key` header on state-changing payment and transfer endpoints.

Replace separate read-then-write checks with atomic conditional SQL updates, row locks, and database uniqueness constraints.

Run a vulnerability scan on your website.

Verify your domain and use MyPentest to check for supported web and API weaknesses. Review findings and coverage; a clean scan does not rule out every CVE.