← Java / Spring · Contents

JAVA-014 / Beginner

Open redirect (redirect: view)

A01:2021 Broken Access Control · CWE-601 URL Redirection to Untrusted Site

Why is this a problem?

Users can be sent to phishing links.

How to fix it

java
@GetMapping("/go") String go(@RequestParam String next) {
    if (!next.matches("^/[A-Za-z0-9/_-]*$")) return "redirect:/";
    return "redirect:" + next;
}

Further reading