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

  private static final Map<String, String> URL_MAPPING = new HashMap<>();
static {
URL_MAPPING.put("google", "https://www.google.com");
URL_MAPPING.put("openai", "https://www.openai.com");
URL_MAPPING.put("github", "https://www.github.com");
URL_MAPPING.put("root", "https://www.example.com");
}

String redirectUrl = URL_MAPPING.getOrDefault(request.getParameter("redirectTo"), "root");
response.addHeader("Location", redirectUrl);

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=java_lang_open_redirect

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

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

Ready to take the next step? Learn more about Bearer Cloud.