Unsanitized user input in redirect

Description

A redirect using unsanitized user input is bad practice and puts your application at greater risk of phishing attacks.

Remediations

❌ Do not use unsanitized user input when constructing URLs

✅ Instead, ensure the input is validated by using a safe list or a mapping when constructing URLs

var URLMapping = map[string]string{
"google": "https://www.google.com",
"openai": "https://www.openai.com",
"github": "https://www.github.com",
"root": "https://www.example.com",
}

func safeRedirectHandler(w http.ResponseWriter, r *http.Request) {
// Get the redirectTo parameter from the query string
redirectTo := r.URL.Query().Get("redirectTo")

// Get the safe URL from the map, default to the URL for "root" if not found
redirectURL, ok := URLMapping[redirectTo]
if !ok {
redirectURL = URLMapping["root"] // Default to a predefined safe URL
}

// Set the Location header to redirect the user
http.Redirect(w, r, redirectURL, http.StatusFound)
}

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

Resources

Associated CWE

OWASP Top 10

Configuration

To skip this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --skip-rule=go_lang_open_redirect

To run only this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --only-rule=go_lang_open_redirect