Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Setting fields you should not (mass assignment)

After this lesson: I can stop an API storing fields a caller should not set.

55 min lesson5 commandsOWASP Juice Shop lab: a vulnerable web shop to attack in your own copy

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

One extra word in a form

Mass assignment is when an API copies every field a caller sends into a stored object, including fields the caller was never meant to set.

A sign-up form sends an email and a password. The account it makes also has a role. What if your request sends the role too?

The one rule

Warning

Make accounts like these only in your own lab copy. Giving yourself rights on a real service is unauthorised access, even if the service lets you. In the UK that is a crime under the Computer Misuse Act 1990. Elsewhere, similar laws apply.

Quick check: the rule

Have a go

Quick check: the rule

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By the end of this lesson

Note

I can stop an API storing fields a caller should not set.

Key words

WordWhat it meansExample
Mass assignmentstoring every field a caller sends
Request bodythe data sent with a request
Allow-listthe fields a caller may set
Protected fielda field only the server setsrole
Token payloadthe readable middle part of a token

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Frameworks copy fields for you

Many frameworks turn a JSON body into a database row in one line. That is quick to write, and it copies every field the caller sent, known or not.

The first Run makes an ordinary account. The second sends one more field, "role":"admin". Compare the two replies.

An ordinary sign-up

Command
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Dee_12345","passwordRepeat":"Dee_12345"}' http://juice-shop:3000/api/Users | grep -o '"role":"[a-z]*"'

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

The same sign-up with a role added

Command
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Eli_12345","passwordRepeat":"Eli_12345","role":"admin"}' http://juice-shop:3000/api/Users | grep -o '"role":"[a-z]*"'

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Every field is open, not only role

The role is not the only protected field. An account also has an id the database should choose. The next Run sets that too.

Then eli signs in. A token's middle part is readable, so base64 -d shows what the shop now believes about eli.

Choose your own account id

Command
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Fin_12345","passwordRepeat":"Fin_12345","id":500}' http://juice-shop:3000/api/Users | grep -o '"id":[0-9]*'

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Sign eli in

Command
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Eli_12345"}' -o eli.json http://juice-shop:3000/rest/user/login

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read the role inside eli's token

Command
grep -o 'eyJ[^"]*' eli.json | cut -d. -f2 | base64 -d | grep -o '"role":"[a-z]*"'

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think hiding a field from the form protects it. The form only decides what an honest browser sends. A request can carry any field. Decide on the server which fields each endpoint accepts, and ignore or refuse the rest.

Copy only the fields you name

Fix: give each endpoint an allow-list of the fields a caller may set, and copy only those. The server sets protected fields such as role, id and price itself.

Many frameworks have this built in: a data transfer object, a schema, or a list of permitted fields. Never bind the whole body to the stored object.

Try it: an allow-list

Code: python
ALLOWED = {"email", "password", "passwordRepeat"}

def new_account(body):
    account = {"role": "customer"}
    for field in body:
        if field in ALLOWED:
            account[field] = body[field]
    return account

print(new_account({"email": "[email protected]", "password": "x", "role": "admin"}))
# Add "role" to ALLOWED and press Run again. What goes wrong?

Make a free account to save your work and track your progress.

Refused fields are worth logging

Detect: when a request carries a field outside the allow-list, log the field name and the account, then drop or refuse it. An honest app never sends one.

Also watch the results: an alert for any new account whose role is not customer catches the attack even where the allow-list was forgotten.

Match each field to who sets it

Have a go

Loading…

Your answers are marked here and are not kept. An account keeps your marks.

Assignment: an allow-list for updates

Write three functions for an endpoint that updates an account.

allowed_fields(body, allowed) returns a new dictionary holding only the keys of body that are in the set allowed. refused_fields(body, allowed) returns the sorted list of keys of body that are not in allowed. apply_update(record, body, allowed) returns a new dictionary: a copy of record with the allowed fields of body written over it. It must not change record itself.

Code: python
def allowed_fields(body, allowed):
    return body


def refused_fields(body, allowed):
    return []


def apply_update(record, body, allowed):
    record.update(body)
    return record

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.