How to Fix Open Redirect Vulnerabilities
In this tutorial, you'll learn about How to Fix Open Redirect Vulnerabilities. We cover key concepts, practical examples, and best practices.
Open redirect vulnerabilities occur when an application accepts a redirect URL from user input and redirects the browser without validating the target domain, allowing attackers to create convincing phishing links.
Quick Fix
Wrong
app.get("/redirect", [](Request& req, Response& res) {
std::string target = req.param("url");
res.redirect(target);
});
If url is https://evil.com/phish, the user is redirected to a malicious site.
Right
std::set<std::string> allowedRedirects = {
"/dashboard",
"/settings",
"/profile"
};
app.get("/redirect", [](Request& req, Response& res) {
std::string target = req.param("url");
if (!allowedRedirects.contains(target)) {
res.status(400).send("Invalid redirect");
return;
}
res.redirect(target);
});
Fix with relative URL enforcement
bool isSafeRedirect(const std::string& url) {
// Only allow relative URLs
if (url.empty() || url[0] != '/") return false;
// Block protocol-relative URLs
if (url.size() > 1 && url[1] == "/') return false;
return true;
}
Fix with domain validation
bool isSameDomain(const std::string& url, const std::string& domain) {
auto parsed = parseUrl(url);
if (!parsed) return false;
return parsed->host == domain || parsed->host.empty();
}
std::string domain = req.host();
if (!isSameDomain(target, domain)) {
res.status(400).send("Invalid redirect");
}
Fix with mapping approach
// Use a mapping instead of raw URLs
// /redirect?to=profile instead of /redirect?url=https://example.com/profile
std::map<std::string, std::string> redirectMap = {
{"profile", "/user/profile"},
{"settings", "/user/settings"},
{"home", "/"},
};
Prevention
- Never accept raw URLs for redirect parameters.
- Use redirect IDs or keys that map to server-side redirect targets.
- Validate redirect URLs against a allowlist.
- Use relative paths (starting with
/) when possible. - Warn users when redirecting to external domains.
DodaTech Tools
Doda Browser's open redirect scanner checks all redirect endpoints for unvalidated URL parameters. DodaZIP archives redirect audit trails. Durga Antivirus Pro blocks phishing URLs that use open redirects.
Common Mistakes with redirect
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
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