Spring CSRF unrestricted RequestMapping

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: java-security/spring-csrf-requestmapping

Language: Java

Severity: Warning

Category: Security

CWE: 352

Description

Classes that contain methods annotated with RequestMapping are by default mapped to all the HTTP request methods.

Spring Security’s CSRF protection is not enabled by default for the HTTP request methods GETHEADTRACE, and OPTIONS.

For this reason, requests or routes with RequestMapping, and not narrowing the mapping to the HTTP request methods POSTPUTDELETE, or PATCH, makes them vulnerable to CSRF attacks.

Learn More

Non-Compliant Code Examples

@Controller
public class UnsafeController {

    @RequestMapping("/path")
    public void writeData() {
        // State-changing operations performed within this method.
    }
}

Compliant Code Examples

@Controller
public class SafeController {

    /**
     * For methods without side-effects use @GetMapping.
     */
    @GetMapping("/path")
    public String readData() {
        // No state-changing operations performed within this method.
        return "";
    }

    /**
     * For state-changing methods use either @PostMapping, @PutMapping, @DeleteMapping, or @PatchMapping.
     */
    @PostMapping("/path")
    public void writeData() {
        // State-changing operations performed within this method.
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: may/unit-testing