Lesson 19 of 20 · 18m
Business logic: the rules your tests never check
Semantics are the attack surface: client-computed totals, float money, negative quantities, and skipped flow steps all survive syntactic validation.
Business-logic flaws are the bugs no scanner finds because nothing is syntactically wrong — the code does exactly what it was told, and what it was told is wrong. The model people carry is "validation means types and formats." Attackers validate semantics: can quantity be negative, does the server recompute the price, can step three run without step two, does 0.1 + 0.2 balance the ledger.
The client computes nothing
price, total, discount, quantity in a request are suggestions. The server recomputes from its own catalog: cart totals, coupon value, shipping, tax. Anything the client sums is a field an attacker rewrites — the classic checkout bypass isn't malformed input but a different price the server believed, and bug-bounty archives are full of $1 checkouts that shipped. Flows are the same shape: a wizard whose step 2 writes state and step 3 charges is a state machine, and POST /confirm without /verify is the first thing tried — Kettle's state-machine work and a decade of bounties are built on skipped steps. Ordering, preconditions, and "this hasn't already happened" are server-side checks; the disabled button in the UI is not one.
Numbers are a security boundary
Float money fails audit before it fails security: 0.1 + 0.2 !== 0.3, and a ledger that multiplies 19.99 * 3 in IEEE-754 drifts cents. The rule is integer minor units — cents, yen, 1999 — or a decimal type (decimal.Decimal, BigDecimal), never float/double/Number. Stripe's API is minor units end-to-end and documents the trap: JPY and a dozen currencies are zero-decimal — amount: 500 means ¥500, not ¥5.00. Overflow is the sibling: quantity parsed into signed 32-bit, or Number past MAX_SAFE_INTEGER (2⁵³−1), wraps or silently mis-represents, and parseInt("999999999999999999")-class input turns "buy a billion, refund the overflow" into real ledger entries.
NaN is the quiet bypass in this class: every NaN >= 0 comparison is false, so if (amount >= 0) written as the positive check rejects it — but if (!isNaN(amount)) and truthiness-shaped checks don't, and NaN then poisons every downstream total. Locale is the other edge: "10,5" is a decimal in half of Europe and a string in JavaScript, and parseFloat stops at the comma — validate the parsed number, not the string.
Negative space
The check that never ran is the exploit: -1 quantity × $50 = -$50 total = a refund path or a credit issued, and a 100%-off coupon applied twice yields a negative amount — arithmetic doesn't check intent. Bound every numeric input: quantity ≥ 1, discount ≤ subtotal, totals ≥ 0, values within catalog range.
Voucher economics are logic too, and they're where abuse compounds. Coupon stacking rules, expiry, per-account caps — and the refund-arbitrage loop where an item bought with a discount refunds at full price. Enumeration is IDOR's cousin (sequential gift-card numbers), and rate limits on redemption endpoints are economic controls, not correctness controls: a six-digit promo code has a million candidates, so the endpoint's job is making a million guesses cost more than the prize. Replays matter here too — re-sending an identical redemption request is a logic problem even when it isn't a race, which is why those paths carry idempotency keys (the race lesson covers the concurrent variant).
Practice
Recompute every total server-side, store money as integer minor units or Decimal, bound every numeric field, enforce flow order in the state machine rather than the UI, and make "can't happen" a rejection — negative or out-of-catalog values are 400s, not clamps to sanitize away. The review question for money paths is literal: where does this number come from, and what stops it being a lie — and the answer should always be a server-side check you can point at.
Keep reading
Related articles from the SudoSecurity blog.
Building a Production REST API With Node.js and Express
Build a REST API in Node.js and Express the way production systems need: input validation, JWT auth, refresh token rotation, status codes, rate limiting, idempotency keys, and centralized error handling.
RSA in JavaScript Part 2: The JWT Is Where You'll Actually Meet It
Most JavaScript developers never call an RSA function directly. They verify an RS256 token, and two classic attacks turn that into an authentication bypass — both demonstrated here, both fixed by one line.
RSA Signatures: The Version in Most Tutorials Is Forgeable
Signing is encryption with the keys swapped, and that symmetry is exactly what makes the naive implementation breakable. Here are two forgeries executed against textbook RSA signatures, and what PSS does that hashing alone cannot.
Ask Java for AES and You Get ECB. Nobody Warns You.
Cipher.getInstance("AES") compiles, runs, encrypts, decrypts, and hands you the weakest mode in the box. Here is the OpenJDK source that makes that decision, and the line where ECB gets chosen for you.