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 questionsBy the end of this lesson
Note
I can stop an API storing fields a caller should not set.
Key words
| Word | What it means | Example |
|---|---|---|
| Mass assignment | storing every field a caller sends | |
| Request body | the data sent with a request | |
| Allow-list | the fields a caller may set | |
| Protected field | a field only the server sets | role |
| Token payload | the readable middle part of a token |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsFrameworks 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
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
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 questionsEvery 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
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
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Eli_12345"}' -o eli.json http://juice-shop:3000/rest/user/loginShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the role inside eli's token
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 questionsA 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
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.
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