Quick revision
The short answer
Spring MVC data binding sets every property of the target object whose name matches a request parameter, not only the fields the form shows. When the target is the User entity, a request that adds role=ADMIN sets the role too, and because the entity is managed, the change can be flushed to the database. The fix is to bind to a small form object that holds only the editable fields, copy them onto the entity in a service, and add an allow-list with setAllowedFields as a second line of defence.
What to remember
In this answer, we saw that Spring MVC binds every request parameter that matches a property, regardless of what the form shows, and that binding onto a JPA entity exposes internal fields like role and id and can write them to the database through dirty checking. The real fix is a dedicated form object or record copied onto the entity in a service, with the id taken from the logged-in user. An @InitBinder allow-list with setAllowedFields is a useful extra control, a deny-list is not, and Spring4Shell reminds us to keep the framework patched.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring MVC data binding sets every property of the target object whose name matches a request parameter, not only the fields the form shows. When the target is the User entity, a request that adds role=ADMIN sets the role too, and because the entity is managed, the change can be flushed to the database. The fix is to bind to a small form object that holds only the editable fields, copy them onto the entity in a service, and add an allow-list with setAllowedFields as a second line of defence.
Why this question is asked
Mass assignment is one of those bugs that looks harmless in code review. The controller is short, the form only has three fields, and everything works in the browser. The problem only appears when someone sends a request the form never would. Interviewers ask about it because it tests whether we understand what data binding really does, and whether we know the difference between a quick patch and a real fix. In this answer, we will recap how @ModelAttribute binding works, reproduce the attack, see why binding onto an entity is worse than binding onto a plain object, and then go through the fixes from strongest to weakest, ending with what Spring4Shell taught us about binding.
1A Quick Recap of Data Binding
When a controller method has a parameter annotated with @ModelAttribute, or simply a non-simple object parameter with no annotation at all, Spring MVC creates or finds an instance of that class and then fills it from the request. The part that fills it is the WebDataBinder, a helper object that walks over the request parameters one by one.
For each parameter, the binder asks a simple question: does the target object have a writable property with this name? If the form sends name=Ana, it calls setName("Ana"). It also understands nested paths, so address.city=Pune calls getAddress().setCity("Pune"). The binder does not know or care which inputs were in the HTML form. It only sees names and values in the HTTP request, and anyone can add more of those with a browser's developer tools or curl.
2Reproducing the Attack
Here is the controller from the question. The form shows name and email, and the entity has more fields than that.
6 messages in order. Play them, or step through them one at a time.
The binder sets every matching property. Nothing between the request and the UPDATE knows the role was never on the form.
Notice that the attacker did nothing clever. They added two parameters, and the binder found setRole and setId and called them. The id is just as dangerous as the role: with id=1, save() merges the changes into another user's row. Fields like balance, verified or tenantId fall the same way.
3Why Binding Onto an Entity Is Worse
Binding onto any object with too many setters is risky, but an entity adds a second problem. A JPA entity loaded inside a persistence context is managed, which means Hibernate watches it and writes any change back at flush time, without us calling save(). So a common variant of this bug looks safer and is not: a @ModelAttribute method loads the user from the repository, the binder then applies the request on top of it, and the transaction commits. Every bound field is written, including the ones the attacker added.
There is also a design cost. The entity now serves as the form model, so every new column becomes a new bindable parameter the day it is added. The person who adds a creditLimit column to the entity is not thinking about the profile form, and that is exactly how these bugs come back.
4Fix 1: Bind to a Form Object With Only the Editable Fields
The strongest fix is to give the form its own small class. It holds the fields the user may edit and nothing else. A record works well, and Spring binds records through their constructor.
Now the extra parameters have nowhere to go. There is no role property on ProfileForm, so the binder skips it. The user id comes from the authenticated principal, not from the request, so it cannot be swapped either. The service copies the two fields explicitly, which also makes the code honest about what this screen is allowed to change.
5Fix 2: An Allow-List in @InitBinder
When we cannot change the model yet, for example in a large legacy codebase, we can restrict the binder itself. The method below runs before binding for this controller.
Notice that we use setAllowedFields, an allow-list, and not setDisallowedFields, a deny-list. A deny-list must name every dangerous field, and it silently fails open when someone adds a new one. An allow-list fails closed: a new field is not bindable until someone decides it should be.
Records help here too. Constructor binding has worked for years, and Spring Framework 6.1 reworked it so that nested constructor binding works and a binder can be set to declarative binding, where only constructor arguments are bound and setters are never called. Check your version for the exact options, but the idea is the same: the shape of the type decides what can be bound.
6What Spring4Shell Taught Us
In 2022, CVE-2022-22965, known as Spring4Shell, showed how far this can go. Data binding follows nested property paths, and on JDK 9 and later a path starting with class.module.classLoader reached the class loader of the application through the getClass() method every object has. On Tomcat with war deployment, attackers set logging properties through that path and wrote a web shell to disk. Spring 5.3.18 and 5.2.20 closed the path by restricting what binding may reach on Class objects, and Spring has kept tightening binding defaults since.
The lesson for us is twofold. First, binding onto rich objects exposes far more than their own fields. Second, keep the framework patched, because some of these holes can only be fixed in the framework itself.
7Follow-up questions
Is a @JsonIgnore or a hidden form field enough?
No. A hidden field only changes what the browser shows; the attacker writes the request by hand. @JsonIgnore affects JSON bodies read by Jackson, not @ModelAttribute binding from form parameters, so it does not protect a form endpoint at all. Narrow input types and allow-lists work on the server, where the attacker has no control.
How would you find every endpoint with this problem?
Search for controller parameters whose type is an entity class, both with @ModelAttribute and without an annotation, and for @RequestBody parameters of entity types. An ArchUnit test can then fail the build if a controller method takes a class annotated with @Entity, so the problem cannot come back.
Does the same problem exist with @RequestBody and JSON?
Yes. Jackson sets any property that appears in the JSON and has a setter or a visible field. A JSON body with "role": "ADMIN" bound onto the entity has the same effect. The same fix applies: a request DTO with only the allowed fields.
8Conclusion
In this answer, we saw that Spring MVC binds every request parameter that matches a property, regardless of what the form shows, and that binding onto a JPA entity exposes internal fields like role and id and can write them to the database through dirty checking. The real fix is a dedicated form object or record copied onto the entity in a service, with the id taken from the logged-in user. An @InitBinder allow-list with setAllowedFields is a useful extra control, a deny-list is not, and Spring4Shell reminds us to keep the framework patched.
Open with
This is mass assignment. Spring's data binder does not know which inputs were on the form; it just walks the request parameters and calls any setter whose name matches.
Then, if they want more
So when the controller binds straight onto the User entity, a request with role=ADMIN calls setRole, and an id parameter can even point the save at someone else's row. Because it's a managed entity, the change can be flushed even without an explicit save.
The fix I'd apply is to bind to a dedicated form object, ideally a record with only name and email, validate it, and have a service load the user by the authenticated id and copy those two fields across. That way extra parameters have nowhere to land.
Where I can't change the model quickly, I'd add an @InitBinder with setAllowedFields as an allow-list, never setDisallowedFields, because a deny-list fails open when someone adds a column. Newer Spring versions also support constructor-only binding for records, which narrows things further.
I'd also mention Spring4Shell, which used the same binding mechanism to reach the class loader through nested paths; it was fixed in 5.3.18, and it's a good reason to keep Spring patched and to add an architecture test that stops controllers from taking entity types.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9, Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A security review finds that users can change their own role by adding role=ADMIN to a...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/db1c0a4c86/