Open Redirect Vulnerability Fix
In this tutorial, you'll learn about Open Redirect Vulnerability Fix. We cover key concepts, practical examples, and best practices.
Your application redirects to a URL specified in the redirect query parameter, and an attacker can use it to redirect users to phishing sites — open redirects occur when the server does not validate the target URL.
Step-by-Step Fix
1. Validate all redirect URLs
from urllib.parse import urlparse
# Wrong — redirecting to any URL
redirect_url = request.args.get("redirect")
return redirect(redirect_url)
# Can redirect to https://evil.com/phishing
# Right — only allow relative redirects
redirect_url = request.args.get("redirect")
if redirect_url.startswith("/"):
return redirect(redirect_url)
else:
return redirect("/")
2. Use an allowlist of domains
# Wrong — any external domain is allowed
redirect_url = request.args.get("next")
if not redirect_url:
redirect_url = "/"
return redirect(redirect_url)
# Right — verify the target domain is trusted
ALLOWED_HOSTS = [
"myapp.com",
"www.myapp.com",
"sub.myapp.com",
]
parsed = urlparse(redirect_url)
if parsed.hostname in ALLOWED_HOSTS or not parsed.hostname:
return redirect(redirect_url)
else:
return redirect("/")
3. Fix in Express (Node.js)
// Wrong — using user input as redirect target
app.get('/login', (req, res) => {
const redirect = req.query.redirect || '/';
res.redirect(redirect);
});
// Right — validate against known paths
const allowedRedirects = ['/', '/dashboard', '/profile'];
app.get('/login', (req, res) => {
const redirect = req.query.redirect || '/';
if (allowedRedirects.includes(redirect)) {
res.redirect(redirect);
} else {
res.redirect('/');
}
});
4. Use a redirect map
# Most secure — map to predefined targets
REDIRECT_MAP = {
"home": "/",
"dashboard": "/dashboard",
"profile": "/profile",
}
key = request.args.get("redirect_to")
target = REDIRECT_MAP.get(key, "/")
return redirect(target)
Common Mistakes
| Mistake | Fix |
|---|---|
Only checking for http:// prefix |
Attackers can use //evil.com, https://evil.com, or %2f%2evil.com |
| Allowing any URL starting with the app domain | https://myapp.com.evil.com starts with the app domain but is malicious |
| Using blacklist for evil domains | Use allowlist instead — blacklist is easy to bypass |
| Not validating URL in POST redirects | Validate all redirect parameters regardless of HTTP method |
| Storing redirect URLs in session without validation | Validate before storing, not just before redirecting |
Prevention
- Use relative redirects (
/dashboardinstead ofhttps://myapp.com/dashboard). - Maintain a map of allowed redirect targets instead of accepting raw URLs.
- When redirecting to external URLs is required, use a fixed mapping.
- Never redirect based on user input without strict validation.
- Use the
Sec-Fetch-Siteheader to detect cross-origin redirect requests.
DodaTech Tools
Doda Browser's URL validator checks redirect targets against a secure allowlist before navigation. DodaZIP archives redirect patterns for security auditing. Durga Antivirus Pro detects open redirect patterns and blocks known phishing destination domains.
Common Mistakes with redirect fix
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world OPEN code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro