Open redirect

Open redirect

Description

An open redirect occurs when untrusted input determines an HTTP redirect destination without adequate validation. Attackers can send users to phishing, malware, or other unwanted sites through a trusted application's redirect. A client-controlled parameter, Host header, or URL component becomes dangerous when the application uses it to select an external destination in http.Redirect.

Potential impact

  • Passing through a trusted domain can make phishing links more convincing.
  • Redirects to malicious sites can expose users to malware or browser exploits.
  • Abuse of the redirect can damage trust in the original service.

Remediation

  • For redirects within your site, accept a path such as /path/... and use the host from server configuration. Reject absolute URLs (http:// or https://) and network-path references (//host). The client supplies r.Host.
  • If you allow external redirects, limit them to approved domains. Parse the URL with url.Parse and resolve relative references with ResolveReference as needed. Before calling http.Redirect, verify the final scheme (http or https), host, and port.

Examples

Before

go
package main

import (
    "net/http"
)

// Before: use client-controlled Host and URL path directly
func redirectHandler(w http.ResponseWriter, r *http.Request) {
    // The client can control r.Host and r.URL.Path
    target := "https://" + r.Host + r.URL.Path
    if len(r.URL.RawQuery) > 0 {
        target += "?" + r.URL.RawQuery
    }

    // Redirect directly to the input-derived URL: open redirect
    http.Redirect(w, r, target, http.StatusTemporaryRedirect)
}

func main() {
    http.HandleFunc("/go", redirectHandler)
    http.ListenAndServe(":8080", nil)
}

After

go
package main

import (
    "net/http"
    "net/url"
    "strings"
)

var allowedHosts = map[string]bool{
    "example.com":      true,
    "www.example.com":  true,
}

// After: fixed domain with only relative paths accepted
func safeRedirectHandler(w http.ResponseWriter, r *http.Request) {
    // 1) Always use a host fixed by the server
    const baseURL = "https://www.example.com"

    // 2) Read the destination path from a parameter
    //    Example: /go?next=/products/123
    next := r.URL.Query().Get("next")
    if next == "" {
        next = "/" // Default path
    }

    // 3) Reject these absolute and protocol-relative URL prefixes
    if strings.HasPrefix(next, "http://") ||
        strings.HasPrefix(next, "https://") ||
        strings.HasPrefix(next, "//") {
        http.Error(w, "invalid redirect url", http.StatusBadRequest)
        return
    }

    // 4) Ensure the path starts with '/'
    if !strings.HasPrefix(next, "/") {
        next = "/" + next
    }

    // Build the final redirect URL
    u, err := url.Parse(baseURL)
    if err != nil {
        http.Error(w, "server error", http.StatusInternalServerError)
        return
    }

    u.Path = next

    // 5) Check the final host against the allow-list as an extra safeguard
    if !allowedHosts[u.Host] {
        http.Error(w, "invalid redirect host", http.StatusBadRequest)
        return
    }

    http.Redirect(w, r, u.String(), http.StatusTemporaryRedirect)
}

func main() {
    http.HandleFunc("/go", safeRedirectHandler)
    http.ListenAndServe(":8080", nil)
}

baseURL fixes the scheme and host. The handler rejects http://, https://, and // prefixes, ensures the next path starts with /, and assigns it to url.URL.Path. It checks the final host against the allow-list before redirecting.

References