Mass assignment: when request bodies write fields they should not

How binding a whole JSON body to a model lets callers set roles, prices, or owners, how to spot it in Rails, Django, Express, and Spring code, and why explicit write schemas are the fix.

Lesson 33 of 69 · 11 min read · practice only with authorization

Diagram

A profile update that also changes the role

Follow the path. One example at a time.

Step 1 of 4

Form

The page sends a display name.

Illustration: Form
PATCH /api/users/311
{"display_name":"Ana R."}

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Framework binders

    What fails
    ORMs and binders map request keys onto attributes automatically.
    Who is affected
    Any account whose privileges live on the same model as editable profile fields.
    What you would notice
    update(**body), Object.assign(model, req.body), or @ModelAttribute on an entity.
    What you change
    Bind to a dedicated input type with only the writable fields.
  2. Fields that should never be writable

    What fails
    Role, tenant, owner, price, balance, verified, and created_at are common targets.
    Who is affected
    Other tenants, and the business when prices or credits can be set.
    What you would notice
    A successful update that echoes back a field the form never sent.
    What you change
    Mark them read-only and test that sending them has no effect.
  3. Nested objects

    What fails
    A body can carry nested relations that the binder also saves.
    Who is affected
    Users of linked records such as organization memberships.
    What you would notice
    accepts_nested_attributes_for or cascading saves on child objects.
    What you change
    Permit nested keys explicitly or handle the children in separate calls.

Testing it in an authorized lab

With a test account, send the normal update and then the same update with one extra field taken from the response schema. Read the response and fetch the object again. If the extra field changed, report the endpoint and the field; there is no need to try escalating further.

$ curl -s -X PATCH https://api.lab.test/api/users/311 -H "Authorization: Bearer $LAB_TOKEN" \
    -H "Content-Type: application/json" -d '{"display_name":"Ana R.","is_staff":true}' | jq .is_staff
true
# finding: is_staff writable by the account owner

Finding it in code

Search for whole-body binding calls and read which model they write. Pay attention to admin and user routes sharing one serializer: the admin form legitimately writes role, and the same class is reused for the self-service route.

$ rg -n "update\(\*\*request|Object\.assign\([a-z]+, req\.body|permit!" app/ src/

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

A self-service profile route and the admin route share one serializer that includes role. What is the fix?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapIDOR and broken object-level authorization: ids without an owner checkHow changing an id in a URL or API body returns someone else’s record, why it tops the OWASP API list, how to test it with two accounts you own, and the ownership-scoped query that fixes it.
  1. 01IDOR in exports and batch endpointsA list of ids is still a list of objects.
  2. 02Nested object authorizationA check on the parent record does not authorize every child id inside the request.
  3. 03Broken function-level authorization: admin actions any user can callWhy hiding an admin menu does not protect the admin endpoint, how to map routes against roles from an OpenAPI file, and how central authorization policies with deny-by-default tests close the gap.

Practice

  1. LabTenant deskTwo companies, one API. See what changes when the server checks ownership.

Glossary

  1. CardMass assignmentA client sends extra fields, such as a role or price, and the server writes them because it binds the whole object.