Skip to content

Open Redirect Vulnerability Fix

DodaTech Updated 2026-06-24 3 min read

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 (/dashboard instead of https://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-Site header 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

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. 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

What is an open redirect vulnerability?

An open redirect is an endpoint that redirects users to URLs specified in user-controlled parameters without validation, allowing attackers to redirect users to malicious sites. ||| How serious is an open redirect vulnerability? While open redirects do not directly compromise the server, they are used in phishing campaigns to make malicious links appear legitimate. Security scanners and bug bounty programs flag them as medium-severity issues. ||| Can I safely redirect to any URL within my domain? Yes, relative paths (starting with /) are safe because they cannot redirect to external domains. Always use relative paths when possible.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro