Typical use in attacks
- Phishing — the link looks trustworthy
- OAuth redirect hijacking — substitute the redirect_uri in an OAuth flow
- Bypassing referer checks
Defence
- Whitelist allowed URLs or domains for redirection
- Do not accept external URLs in redirect parameters
- Compare the URL host with the current domain before redirecting
// Check: only relative URLs
function safeRedirect(string $url): string {
$parsed = parse_url($url);
// reject if scheme or host is present
if (isset($parsed['scheme']) || isset($parsed['host'])) {
return '/'; // fallback to home
}
return $url;
}