The Percent That Escaped — Java Bug Hunt
Inspired by the "%2520 in every URL" class of bugs that has hit search engines and CDNs alike — percent signs that aren't themselves encoded turn "50% off"…
- Language: Java
- Layer: Backend
- Difficulty: Medium
- Concepts: Encoding, URLs
- Modelled on: URL encoding
- Visible tests: percent signs are encoded; spaces, ampersands and equals encode; plain text passes through
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-3
- Time to fix: 20 minutes, the target for a medium incident
Failing checks on the shipped build
- percent signs are encoded — expected 50%25%20off but got 50%%20off
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 "%2520 in every URL" class of bugs that has hit search engines and CDNs alike — percent signs that aren't themselves encoded turn "50% off" links into broken requests, while everything else was handled.
ParamEncoder.java encodes a query-string value. It forgot the most important character.
Bug report
BUG-2520 · Reported by: SEO (broken deep links in the wild)
encode(raw) — minimal table for our URLs:
- " " -> %20, "&" -> %26, "=" -> %3D, and "%" itself -> %25 (FIRST!)
- all other characters pass through
Observed: "50% off" encodes to "50%%20off" — the bare % corrupts the request.
Logs
[crawler] GET /deals?q=50%%20off -> 400 Bad RequestThe code as shipped
ParamEncoder.java (editable)
class ParamEncoder {
static String encode(String raw) {
return raw
.replace(" ", "%20")
.replace("&", "%26")
.replace("=", "%3D");
}
}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.