Open redirect

Unvalidated user-selected redirect URLs may enable phishing or abuse of authentication flows.

Description

Passing a request URL directly to sendRedirect lets the caller choose where a user goes after login or payment.

Potential impact

A service link can lead to phishing or malicious downloads. An OAuth or SSO flow that trusts the destination may disclose credentials or tokens.

Remediation

  • Map a fixed key such as next=profile to an internal path on the server. Reject unknown keys or use a default path.
  • If you must accept external URLs, parse and normalize the URL, require https, and match the host and port exactly against an allow-list. startsWith and contains cannot establish an exact host match.

Examples

These Spring controller excerpts omit the required imports and application-defined internal routes.

Before

java
@GetMapping("/login/success")
public void loginSuccess(
    @RequestParam("next") String next,
    HttpServletResponse response
) throws IOException {
    response.sendRedirect(next);
}

After

java
private static final Map<String, String> ALLOWED_REDIRECTS = Map.of(
    "profile", "/account/profile",
    "orders", "/account/orders"
);

@GetMapping("/login/success")
public void loginSuccess(
    @RequestParam(value = "next", defaultValue = "profile") String next,
    HttpServletResponse response
) throws IOException {
    String redirectPath = ALLOWED_REDIRECTS.getOrDefault(next, "/account/profile");
    response.sendRedirect(redirectPath);
}

profile and orders select server-defined paths. A missing or unrecognized next value leads to /account/profile.

References