The Coupon That Stacked Itself — Java Bug Hunt
Inspired by the promo-code exploits that make it to Twitter every Black Friday: enter SAVE10, then save10, then Save10 — and watch the discounts stack.
- Language: Java
- Layer: Backend
- Difficulty: Easy
- Concepts: Idempotency, Pricing
- Modelled on: Black Friday
- Visible tests: case variants of one code apply once; distinct codes compound
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-2 (priority: High (margin))
- Ticket: BUG-STACK10
- Reported by: e-commerce
- Time to fix: 10 minutes, the target for an easy incident
Failing checks on the shipped build
- case variants of one code apply once — expected 9000 but got 7290
How to work this incident
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
- Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
- Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.
Briefing
Inspired by the promo-code exploits that make it to Twitter every Black Friday: enter SAVE10, then save10, then Save10 — and watch the discounts stack. Codes must apply once, case-insensitively.
Promotions.java applies a list of entered codes to a total.
Bug report
BUG-STACK10 · Priority: High (margin) · Reported by: e-commerce
applyCodes(totalCents, codes):
- each KNOWN code applies its percentage once, case-insensitive dedupe
- unknown codes are ignored
- discounts compound multiplicatively, integer cents floor at each step
Observed: "SAVE10" + "save10" gives 19%; script kiddies found it in hours.
Logs
[cart] codes=[SAVE10, save10, SAVE10] total 10000 -> 7290The code as shipped
Promotions.java (editable)
import java.util.*;
class Promotions {
static long applyCodes(long totalCents, String[] codes) {
long total = totalCents;
for (String code : codes) {
Integer pct = PromoTable.PERCENT.get(code.toLowerCase());
if (pct != null) {
total = total * (100 - pct) / 100;
}
}
return total;
}
}Read-only context: PromoTable.java.
Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More Java bug hunts.