BOLA and broken authorization
Understand the idea
A valid login plus an id is not proof that this caller owns that id.
The idea
This is the bug that shows up most often in real APIs. The handler asks who is calling, gets a real user, and then trusts the id in the path. Those are different questions. here is the query user being ada or bea. , broken object level authorization, is what happens next: the code loads notes[id] and sends it. Ada's session is valid. Note 2 exists. Nothing compares note.owner with ada. The first run is that bug. Status 200, body title Bea note. The anonymous run is 401, which is why the tests that only try a missing login stay green. The fixed handler keeps the login check, then returns 403 when the owner differs, and it does not put the note in that body. Ada can still read note 1, which is hers. Update and delete need the same compare. A role of editor is not a substitute when the rule is ownership.
What changes
| Request | What passed | Result |
|---|---|---|
| No user, any id | Nothing | 401 login |
| Ada asks for note 2, owner not checked | Authentication only | 200 and Bea's title |
| Ada asks for note 2, owner checked | Authentication, then a failed ownership compare | 403 and no note |
| Ada asks for note 1, owner checked | Both | 200 and Ada's title |
The command
Logged in, wrong owner, no ownership check
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Not logged in
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Same request, owner compared
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Owner reads their own note
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
The broken request prints 200 and Bea note. The anonymous request prints 401. The fixed request for note 2 prints 403 and forbidden, not the title. Note 1 still prints Ada note.
Guided challenge
Compare the owner
GET /notes/2?user=ada currently returns Bea's note because the handler stops after the login check. If the note's owner is not the user, return 403 and do not send the note.
Compare the owner
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
403 forbidden
Hints 0/2
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.