← Java / Spring · Contents

JAVA-009 / Beginner

Direct entity binding in Spring MVC (mass assignment)

A01:2021 Broken Access Control · CWE-915 Mass Assignment

Why is this a problem?

Request JSON fields role and isAdmin are bound to the entity.

How to fix it

java
public record UserUpdateDto(@NotBlank String name, @Email String email) {}
@PutMapping("/users/me")
public void update(@Valid @RequestBody UserUpdateDto dto, @AuthenticationPrincipal Me me) { ... }

Further reading