Skip to the page

Cookies, CSRF and cross-window attacks · mid

Open redirects and URL validation

An open redirect lets an attacker use your trusted domain to send users anywhere (/login?next=https://evil.example), which powers phishing and OAuth token theft; you prevent it by allowing only relative paths or an exact list of origins, checked with the URL parser.

The notes

What you must know

  • Typical places: ?next=, ?returnTo=, ?redirect_uri= after login, logout, language switch, payment return URLs.
  • Phishing: the link starts with your real domain, so users and filters trust it. In OAuth, a loose redirect_uri check can send the authorization code to the attacker.
  • URL tricks: //evil.example (protocol-relative), /\evil.example (browsers treat \ as / in http(s) URLs), https://bank.example@evil.example (userinfo), javascript: URLs, encoded characters.
  • Validate with new URL(value, yourOrigin) and compare url.origin to your origin (or an exact allow-list). Then redirect to url.pathname + url.search + url.hash, not the raw string.
  • Better: store the target server-side (or use an id) instead of trusting a URL from the query string.

The red pen

Common traps: mistake → fix

  • Mistake: "Check that next starts with '/'"

    Fix: '//evil.example' and '/\evil.example' start with '/' but leave your site.

  • Mistake: "Check that the URL contains our domain"

    Fix: https://bank.example.evil.io contains it.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is an open redirect? Why is it a security bug if no data is stolen?
  2. How would you validate the ?next= parameter after login?
  3. How do open redirects make OAuth flows dangerous?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview