CIDADAXSEC
← All topics
Expert

Race conditions

Race conditions exploit a race between parallel requests to slip past limits, rate limits, or single-step checkpoints.

Material

A race condition occurs when two processes or requests collide on the same data without adequate locking. A window of milliseconds between the check and the write is enough for two requests to both pass a rule that should only apply once.

How it works

The most common shape is check-then-act: the server reads a balance or state, decides the action is allowed, then writes the result. If two requests read the same old value before either writes, both see the same condition and both proceed.

http
POST /redeem HTTP/1.1
Host: lab.cidadaxsec.local
Content-Type: application/json
Cookie: session=3f9a1c7e

{"coupon":"SINGLE-USE"}

HTTP/1.1 200 OK
{"couponId":"SINGLE-USE","discount":"50%","remaining":0}
bash
# kirim banyak request paralel agar semuanya melihat state yang sama
seq 1 30 | xargs -P 30 -I{} curl -sS -o /dev/null -w "%{http_code}\n" \
  -X POST "https://lab.cidadaxsec.local/redeem" \
  -b "session=3f9a1c7e" \
  -H "Content-Type: application/json" \
  -d '{"coupon":"SINGLE-USE"}' | sort | uniq -c

Attack vectors

  • Limit overrun: one voucher is redeemed many times even though it should only apply once.
  • Rate limiting: many parallel requests slip past the rate cap because the counter is incremented too late.
  • Multi-endpoint: the attacker watches for a header missing from the response and fires the next endpoint before state is locked.
  • Single-endpoint: two requests to the same URL with the same body run at the same time.
  • Partial construction: the same object is created twice from one source, for instance two accounts from one form.

Simply raising the request count often fails because network latency queues the requests neatly. The window is what matters: send requests as close together as possible and use HTTP/2 multiplexing or many connections so they arrive together.

Preventing

  • Use database transactions with row locking or Serializable isolation for check-then-act operations.
  • Enforce a unique constraint at the database level for one-time resources such as coupon codes.
  • Apply an idempotency key per action so duplicate requests return the same result instead of a second effect.
  • Lock the state explicitly before checking, and release the lock in a finally block that always runs.
  • Apply rate limiting centrally in a shared layer for all instances, not with per-process memory counters.
python
from contextlib import contextmanager

@contextmanager
def advisory_lock(conn, key: str):
    conn.execute("SELECT pg_advisory_lock(hashtext(%s))", (key,))
    try:
        yield
    finally:
        conn.execute("SELECT pg_advisory_unlock(hashtext(%s))", (key,))

Labs

  • Limit overrun race conditions
  • Bypassing rate limits via race conditions
  • Multi-endpoint race conditions
  • Single-endpoint race conditions
  • Exploiting time-sensitive vulnerabilities
  • Partial construction race conditions
Race conditions are non-deterministic, so a failed attempt does not prove there is no window. Repeat several times and watch for a response showing more side effects than requests sent.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.