Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Insecure Design and Misconfiguration

Business Logic Flaws and Abuse Cases

Learn how business logic flaws such as price tampering, coupon abuse, skipped workflow steps and race conditions arise, and how to defend with server-side rules, state machines and idempotency.

A business logic flaw lets someone use an application's legitimate features in a way the designers did not intend. There is no malformed input, no injected code and no broken library: every request looks valid. Scanners usually miss these bugs because only a human who understands the business rules can tell that something is wrong.

Typical symptoms: free products, negative balances, skipped payment steps, unlimited coupon use, bypassed limits.

Where logic flaws hide

Pattern Example
Trusting client-supplied values The cart sends price=1.00 in a hidden field and the server charges it
Missing limits and bounds A transfer amount of -100 moves money in reverse
Skippable workflow steps Calling /checkout/confirm without ever calling /checkout/pay
Replay of one-time actions A single-use coupon or gift card used again
Race conditions Two simultaneous withdrawals both see the old balance
Rounding and unit errors Fractions of a cent, currency mismatches
Feature interactions Stacking a discount code with a clearance sale into a negative total
Abuse of "free" features Using a password-reset email to spam a victim

Rule 1: the server owns the facts

Prices, discounts, roles, totals and permissions come from the server's own data. The client may request ("add product 17, quantity 2"); the server computes everything else.

Python Trusting a client-sent price

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The vulnerable version charges two cents for two books and "charges" minus sixty dollars for a negative quantity, which would credit the customer.

Rule 2: enforce workflows as state machines

When a process has steps (cart, address, payment, confirmation), each endpoint must verify the order is in the right state, not just that the user is logged in.

stateDiagram-v2
  [*] --> Cart
  Cart --> Address: add address
  Address --> Paid: payment confirmed
  Paid --> Shipped: warehouse ships
  Paid --> Refunded: refund
  Shipped --> [*]
  Refunded --> [*]

Only the arrows are allowed. There is no arrow from Cart straight to Shipped.

Python A tiny order state machine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The attacker's shortcut ("jump straight to shipped") is refused because the transition table does not allow it.

Rule 3: one-time means one time (and atomic)

Single-use coupons, gift cards, invitations and password reset links must be consumed atomically. A check-then-update sequence has a time gap: two requests arriving together can both pass the check. That is a race condition (also called TOCTOU, time of check to time of use).

Fixes:

  • Make the database enforce it: a unique constraint, or an UPDATE ... WHERE used = 0 and check the affected-row count.
  • Use transactions with appropriate locking or isolation.
  • Use idempotency keys so a repeated request (a double click, a retry) has the effect of one.
Python Atomic redemption with SQL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the atomic version the database performs the check and the change as one step, and rowcount tells you whether you won. Two simultaneous callers cannot both see rowcount == 1.

Here is the same idea for money, using a transaction and a constraint:

Python Balances that cannot go negative

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notes: amounts are integers in the smallest currency unit (cents) to avoid floating-point rounding, a CHECK constraint keeps the invariant even if the application code has a bug, and the transaction rolls back the first update when the second step or a constraint fails.

Rule 4: set limits on everything

Quantities, amounts, attempts, file sizes, items per page, discount stacking and account creation all need sensible maximums. Combine them with rate limiting and monitoring that notices unusual patterns, such as a user redeeming the same type of code fifty times.

Finding logic flaws

  1. Draw the workflow and list every state and endpoint.
  2. For each endpoint ask: what if called out of order, twice, in parallel, with negative or huge values, or as another user?
  3. Write these as automated tests with two accounts.
  4. Review features with someone who thinks adversarially ("how would I get this for free?").

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A checkout form sends price=0.01 in a hidden field and the server charges it. What is the fix?

    The client can send any value. The server must own prices, discounts and totals.
  2. 2. What does a state machine protect against?

    Only listed transitions are allowed, so attackers cannot jump from cart to shipped.
  3. 3. How do you safely redeem a single-use coupon?

    An atomic conditional update lets the database guarantee only one request succeeds.
  4. 4. Why store money as integer cents?

    Floating point cannot represent many decimal fractions exactly.

Summary

  • Business logic flaws abuse legitimate features and rarely show up in automated scans.
  • Let the server own prices, totals and roles; validate quantities and ranges.
  • Model workflows as state machines and verify the current state on every step.
  • Make one-time actions atomic with conditional updates, constraints, transactions and idempotency keys.
  • Test out-of-order, repeated, parallel, negative and cross-user requests.

Next: Security Misconfiguration and Hardening (A05)