The Open Redirect — JavaScript Bug Hunt
Inspired by the open-redirect advisories filed against practically every login page ever built.
- Language: JavaScript
- Layer: Backend
- Difficulty: Medium
- Concepts: Security, URLs
- Modelled on: Login pages everywhere
- Visible tests: our own absolute urls pass; a foreign url mentioning our host is blocked; relative paths pass, protocol-relative tricks do not
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-2 (priority: High (phishing vector))
- Ticket: BUG-302
- Reported by: bug bounty
- Time to fix: 20 minutes, the target for a medium incident
Failing checks on the shipped build
- a foreign url mentioning our host is blocked — expected "/" but got "https://evil.example/?next=https://good.com"
- relative paths pass, protocol-relative tricks do not — expected "/dashboard" but got "/"
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 open-redirect advisories filed against practically every login page ever built. The "is this URL ours?" check uses indexOf — so https://evil.example/?next=https://good.com passes because the good host appears somewhere in the string.
redirects.js picks the post-login destination.
Bug report
BUG-302 · Priority: High (phishing vector) · Reported by: bug bounty
safeRedirect(url, allowedHosts) -> the url if safe, else "/":
- allowed: same-site relative paths ("/dashboard") — but NOT "//host" tricks
- allowed: "https://<host>" or "https://<host>/..." for hosts in the list
- everything else -> "/"
Observed: attacker URLs containing our hostname anywhere sail through.
Logs
[login] redirect -> https://evil.example/phish?brand=good.com (allowed?!)The code as shipped
src/web/redirects.js (editable)
// Chooses the post-login redirect target.
exports.safeRedirect = function (url, allowedHosts) {
for (var i = 0; i < allowedHosts.length; i++) {
if (url.indexOf(allowedHosts[i]) !== -1) {
return url;
}
}
return "/";
};
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 JavaScript bug hunts.