Escaping the Wildcard — Java Bug Hunt
Inspired by the search boxes of the world: a user searches for the literal text 50% and gets every row containing "50", because % is a LIKE wildcard.
- Language: Java
- Layer: Database
- Difficulty: Medium
- Concepts: SQL, Escaping
- Modelled on: Search boxes
- Visible tests: a percent sign is literal; an underscore is literal; plain terms still search normally
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-3 (priority: Medium (data leak adjacent))
- Ticket: BUG-LIKE50
- Reported by: search
- Time to fix: 20 minutes, the target for a medium incident
Failing checks on the shipped build
- a percent sign is literal — % must not act as a wildcard: expected condition to hold
- an underscore is literal — _ must not match any char: expected condition to hold
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 data through the query or the repository code; compare the rows it returns with the rows the report expects.
- 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 search boxes of the world: a user searches for the literal text 50% and gets every row containing "50", because % is a LIKE wildcard. Underscores are worse — a_c matches "abc". User input must be escaped before it goes anywhere near a pattern.
The LIKE matcher (locked) supports \\ escapes. SearchQuery.java doesn't use them.
Bug report
BUG-LIKE50 · Priority: Medium (data leak adjacent) · Reported by: search
contains(text, term) — true iff text contains the LITERAL term:
- "%" and "_" in the term must match themselves, not act as wildcards
Observed: searching "50%" returns "50 cents" and "500 units"; searching "a_c" returns "abc".
Logs
[search] term="50%" matched 4,812 rows (expected 3)The code as shipped
SearchQuery.java (editable)
class SearchQuery {
// True iff text contains the literal term.
static boolean contains(String text, String term) {
return LikeMatcher.like(text, "%" + term + "%");
}
}Read-only context: LikeMatcher.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.