Skip to content

How to Fix Open Redirect Vulnerabilities

DodaTech Updated 2026-06-24 2 min read

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.

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

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to 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

Why are open redirects considered a security vulnerability?

Open redirects enable phishing attacks. Attackers create links that appear to go to a trusted domain but redirect to malicious sites. Users trust the initial domain and may enter credentials on the destination page.

Can open redirects be used for other attacks?

Yes, open redirects can bypass URL filters, enable CRLF injection, and be used in OAuth token theft (redirect URI manipulation). They also help attackers evade spam filters and deliver malicious content.

Is a 301 or 302 redirect more dangerous for open redirects?

Both are equally dangerous. The vulnerability is in accepting unvalidated redirect URLs, not in the HTTP status code. Attackers can use 301, 302, 307, or JavaScript redirects equally effectively.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro