CWE-840OWASP A04:2021CWE-362CWE-285

CWE-840: Business Logic Errors

An application fails to enforce its own business rules, so a user abuses a legitimate workflow to reach an outcome the rules were meant to prevent.

Also known as: Business logic flaws, Workflow abuse, Logic errors

Risk

Varies from low to critical by rule. Because each request is individually valid, these flaws evade scanners and are often found only when a user has already gained free goods, skipped a step, or exceeded a limit.

Definition

What it is

CWE-840 covers flaws where the application does not enforce its own rules about how a feature or workflow may be used. The requests are well formed and authenticated, but the sequence, quantity, timing, or combination breaks an intended constraint the code never checked. Examples include skipping a required step, applying a discount twice, using a negative quantity, or exceeding a limit. The weakness is missing enforcement of the business rules themselves, not a technical fault in the request.

Use-cases

How it shows up in real code

Negative quantity refund

A cart accepts a negative quantity, so adding a line with a negative count lowers the total or produces a credit the user was never owed.

Skipping a required step

A checkout or approval workflow can be reached at the final step directly, so a user jumps past payment or review that the flow assumed came first.

Stacking a one-time discount

A promo meant to apply once is applied repeatedly by resubmitting it, because the rule limiting it to a single use is never enforced.

Exceeding an intended limit

A free tier allows a fixed number of actions, but the count is not enforced server-side, so a user continues past the cap without paying.

AI risk

Why AI-generated code has increased the exposure

  • Assistants implement the happy path the prompt describes and omit the guardrails, because the business rules are domain context that was never stated.
  • Constraints like one use per customer, no negative amounts, and required ordering of steps live in the product's policy the model cannot infer.
  • Models optimize for a feature that works when used as intended and do not add checks for the abusive sequences a real user will try.
  • There is no common code pattern for an arbitrary business rule, so unlike a known vulnerability class the assistant has nothing safe to copy.
  • As the assistant scaffolds more features and workflows, each carries its own unenforced rules, and the surface of abusable logic grows with the product.

Vibe coding

How vibe coding amplifies the risk

  • Vibe coding ships workflows whose missing rule enforcement is invisible in an unread diff, since each request looks valid.
  • Testing the intended flow once, the developer never tries the negative quantity or the skipped step, so the gap stays hidden.
  • Prompt-and-ship skips the review where someone would ask how a user could abuse this feature.
  • A prototype that demonstrated the feature is promoted to production without anyone hardening the rules.
  • Rapid growth adds revenue-touching workflows faster than anyone audits each for the limits and sequencing it should enforce.

Detection

How to find it

  • List each feature's intended rules (limits, ordering, uniqueness, allowed ranges) and confirm the server enforces every one.
  • Test abusive inputs such as negative or zero quantities, repeated submissions, and out-of-order steps.
  • Confirm multi-step workflows cannot be entered partway or completed with a required step skipped.
  • Re-verify limits and one-time rules on the server for every request, not just in the first interaction.

Remediation

How to fix it

  • Enforce every business rule explicitly on the server: limits, ordering, uniqueness, and valid value ranges.
  • Validate quantities and amounts against sensible bounds and reject negative or nonsensical values.
  • Make workflows enforce their required sequence so a step cannot be skipped or replayed.
  • Enforce one-time and per-user limits against durable server state, not client assumptions.
  • Add tests that exercise abusive sequences and inputs and fail the build if a business rule is violated.

Frequently asked questions

What is CWE-840 (business logic errors)?
CWE-840 is when an application fails to enforce its own business rules, so a user abuses a legitimate workflow to reach a forbidden outcome. The requests are valid and authenticated, but their sequence, quantity, or timing breaks a constraint the code never checked, such as a negative quantity, a skipped step, or a reused one-time discount.
How have AI assistants and vibe coding increased business logic error risk?
Assistants implement the happy path the prompt describes and omit guardrails, because the business rules are domain context never stated and there is no standard pattern to copy. Vibe coding ships those workflows unread, and because the developer tests only the intended flow, the missing enforcement of limits and sequencing stays invisible until a user abuses it.
How do I fix business logic errors?
Enforce every business rule explicitly on the server, validate quantities and amounts against sensible bounds, make workflows enforce their required sequence, enforce one-time and per-user limits against durable server state, and add tests that exercise abusive sequences and inputs.

Related weaknesses

Catch CWE-840 before you ship

Tethlyn Audit scans your codebase and its git history for CWE-840 and the rest of the business logic vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.

See Tethlyn Audit