Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 2 min read

Your Profile Endpoint Saved Every Field It Was Sent and One of Them Was the Role

The profile page let customers change their name, their phone number, and their delivery address. The endpoint was short. user = User.get(current_user_id) user.update(**request.json) user.save() Whatever the form sent

The profile page
let customers change
their name,
their phone number,
and their delivery address.

The endpoint was short.

user = User.get(current_user_id)
user.update(**request.json)
user.save()

Whatever the form sent,
it saved.

Three fields today.

New fields tomorrow
without touching the backend.

Everybody liked it.

Then somebody
opened the browser tools,
copied the request,
and added one line:

"role": "admin"

The endpoint
saved that too.

The user table
had a role column.

The update
did not know the difference
between a field
the form shows
and a field
the form happens not to show.

Neither did the developer,
by then.

The person who added
the role column
had never seen
the profile endpoint.

The person who wrote
the profile endpoint
had never imagined
a role column.

Each change
was safe on its own.

This has a name.

Mass assignment.

In 2012
a developer used it
against GitHub itself,
to add his own key
to the Rails organisation
and push a commit
to the Rails repository.

Rails changed its defaults
after that.

Newer versions make you list
the parameters you accept.

Your framework
may not have changed
anything.

Here is the thing to see.

The form is not
the interface.

The request is.

Anyone can send a request
with any field
the model has.

is_verified.

credit_limit.

account_id.

price.

Hiding a field
on the page
protects nothing.

The fix is an allowlist,
written next to the endpoint:

allowed = {"name", "phone", "address"}
changes = {k: v for k, v in request.json.items()
if k in allowed}

Or a separate input type
for each action,
with only the fields
that action may change.

Reject unknown fields
loudly,
rather than drop them quietly,
so the next attempt
shows up in your logs.

And test it
the way an attacker would.

Send the endpoint
a field it should never accept.

Then check the database,
not the response.

Convenience
that accepts anything
is a door
you forgot to draw
on the plan.

– Serguey Asael Shinder

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.