Skip to content

Open redirect

When is it relevant

A new or changed code path that redirects to, or links to, a location taken from input. Examples are returnTo, next, redirect_uri, the Referer header, OAuth state, and client-side window.location assignments.

Attack

The attacker sends a link on your trusted domain that bounces the victim to a phishing site. Worse, a login or OAuth flow redirects to the attacker with codes or tokens attached. Prefix checks are bypassed with values like //evil.com, /\evil.com, https://app.example.com.evil.com and javascript:.

Mitigation

  • Redirect only to relative paths, validated by parsing them against your own origin, or to an allowlist of exact origins.
  • Compare parsed origins, never string prefixes.
  • For OAuth, match registered redirect URIs exactly.

Test

  • Point-in-time: try the redirect parameter with //evil.example, /\evil.example, https://app.example.com.evil.example and javascript:. Each must land on your own origin.
  • Continuous: a unit test on the redirect validator with those cases, and a check that OAuth redirect URIs are registered exactly.

Examples

Mitigation options

Option When it fits Docs
Allowlist of destinations Few known targets OWASP redirects
Accept a relative path only "Return to where you were" after login Express API
Parse and compare the host Redirects between your own domains Express API

Vulnerable — redirect to whatever arrives

router.get('/login', (req, res) => {
  if (req.session.userId) return res.redirect(req.query.next || '/dashboard')
  res.render('login', { next: req.query.next })
})

Vulnerable — "must start with a slash"

const next = req.query.next || '/'
if (!next.startsWith('/')) return res.redirect('/')
res.redirect(next)                     // //evil.com and /\evil.com still escape

//evil.com is a protocol-relative URL and /\evil.com is treated as one by several browsers — both start with /.

Mitigated — allowlist

const DESTINATIONS = { dashboard: '/dashboard', billing: '/billing', help: '/help' }
res.redirect(DESTINATIONS[req.query.next] ?? '/dashboard')

Mitigated — relative path only

function safeNext(value) {
  if (typeof value !== 'string') return '/dashboard'
  // exactly one leading slash, no backslashes, no scheme
  if (!/^\/(?!\/)[\w\-./?=&%]*$/.test(value)) return '/dashboard'
  return value
}

res.redirect(safeNext(req.query.next))

Mitigated — compare the resolved host

const ALLOWED_HOSTS = new Set(['app.example.com', 'docs.example.com'])

const target = new URL(req.query.next, 'https://app.example.com')
const ok = target.protocol === 'https:' && ALLOWED_HOSTS.has(target.host)
res.redirect(ok ? target.href : '/dashboard')

Resolving against your own base first means a relative path stays relative and an absolute URL is judged on its real host.

Docs: res.redirect · WHATWG URL

Bypasses to check

  • //evil.com, /\evil.com, \/\/evil.com, https:/\evil.com.
  • Userinfo tricks: https://app.example.com@evil.com/ — the host is evil.com; a startsWith('https://app.example.com') check passes.
  • Suffix matching: endsWith('example.com') allows evil-example.com and example.com.evil.com.
  • Check then change: the value is validated, then decoded (decodeURIComponent) or concatenated with a prefix before the redirect.
  • Double encoding (%252f%252f) and unicode look-alikes decoded downstream.
  • The redirect happening somewhere other than res.redirect: a Location header set by hand, an HTML meta refresh, or window.location in a template.
  • OAuth and SSO redirect_uri/state, logout returnTo, and any ?url=/?target=/?continue= parameter.
  • Open redirect used as a step in SSRF or to leak a token in the Referer.

What to grep for

  • res.redirect(, res.location(, Location, meta http-equiv="refresh"
  • req.query.next, returnTo, redirect_uri, continue, callback
  • startsWith('/'), endsWith(, includes(

Mitigation options

Option When it fits Docs
Redirect to a route name (url_for) Default; the destination is never input url_for
Same-site path check on next Login flows that return the user to where they were urllib.parse.urlsplit
An allowlist of hosts Redirects to partner sites or other apps you own urlsplit

Vulnerable — redirect to whatever next says

@app.post('/login')
def login():
    ...
    return redirect(request.args.get('next', '/'))       # next=https://evil.example

Vulnerable — a prefix check

nxt = request.args.get('next', '/')
if nxt.startswith('/'):
    return redirect(nxt)          # //evil.example and /\evil.example both pass

Browsers treat //host as protocol-relative, and many treat \ like /.

Mitigated — only same-site paths

def safe_next(target, fallback='/'):
    if not isinstance(target, str) or not target.startswith('/') or target.startswith('//') or '\\' in target:
        return fallback
    parts = urlsplit(target)
    if parts.scheme or parts.netloc:
        return fallback
    return target

return redirect(safe_next(request.values.get('next')))

Docs: urlsplit · flask.redirect

Bypasses to check

  • Tabs and newlines: urlsplit strips them (as browsers do), so validate the value you actually redirect to.
  • next read from both the form and the query (request.values). Check that both paths go through the helper.
  • Redirect targets in Referer headers, OAuth redirect_uri, or logout return_to.
  • Check then change: the value is validated, then URL-decoded or joined with a base before redirecting.

What to grep for

  • redirect(, request.args.get('next', request.values.get(, next=, return_to, request.referrer

Resources