Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 39 ยท API Security

Authorization and Object-Level Access (BOLA)

Learn the difference between authentication and authorization, why broken object-level authorization is the top API risk, and how to enforce ownership and roles.

Authentication asks "who are you?". Authorization asks "are you allowed to do this to that thing?". Many APIs get the first right and the second wrong: the request carries a valid token, so the server happily returns whatever ID was in the URL. The result is the most common serious API flaw, called Broken Object Level Authorization (BOLA) and also known as IDOR (insecure direct object reference). It tops the OWASP API Security Top 10.

Here is the whole bug in one sentence: a logged-in user changes /notes/17 to /notes/18 and reads another user's note. This lesson shows the bug live, fixes it, and then covers roles, scopes and function-level checks.

Three levels of access control

Authentication Is this a known caller?
Function level May this role call this endpoint?
Object level May they touch this record?

Each level needs its own check.

1 / 2

See the bug

The API below stores notes for two users. GET /notes/:id in the vulnerable version only checks that you are logged in. The fixed version also compares the note's owner with the caller. For brevity the "token" is just the user's name; real code takes it from a verified token (see JSON Web Tokens).

Node.js BOLA: vulnerable and fixed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The attacker did nothing clever. No token theft, no injection, just a different number. Automated scanners and bug-bounty hunters try this on every ID they see.

How to prevent BOLA

  1. Check ownership on every object access, for reads, updates and deletes alike. Put the check in one shared place (a policy function or a query scope), not in each handler.
  2. Scope the query, not just the result. Instead of loading note 2 and then checking, query WHERE id = ? AND owner_id = ?. The row simply does not exist for other users, and you cannot forget the check afterwards.
  3. Take the user from the verified token, never from a request field. A body like {"userId": 5} is attacker-controlled.
  4. Use unpredictable IDs (UUIDs) as defence in depth. This slows enumeration but is not a fix: IDs leak in logs, emails and links.
  5. Return 404 or 403 consistently. 404 avoids confirming that another user's record exists; 403 is more honest about the rule. Pick one policy and keep it.

Function-level authorization

Some endpoints are for certain roles only: deleting users, listing all invoices, changing prices. Classic mistakes are hiding the admin button in the UI while leaving DELETE /users/:id open to everyone, or protecting /admin/users but forgetting /admin/users/export. Deny by default: every route requires an explicit permission, and a route without one is rejected.

RBAC (role-based access control) maps roles to permissions. ABAC (attribute-based) decides from attributes of the user, the object and the context: "editors may edit documents in their own department during business hours". Most APIs combine both.

Node.js A small policy function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that both checks matter: ana (editor) may write her own note but may not delete it because her role lacks note:delete, and vic may read his own note but not hers.

Scopes versus user permissions

A token can carry scopes (notes:read), and your user also has a role. A request is allowed only if both agree: the token's scope limits what the client may do for the user, and the user's rights limit what the user may do at all. The client application should never gain more than the user has. See OAuth 2.0 Flows and PKCE for scopes.

  • Broken property-level authorization: returning fields the caller should not see (passwordHash, isAdmin, internal notes). Use explicit response shapes ("serializers") that list allowed fields, instead of returning whole database rows.
  • Mass assignment: letting clients set fields they should not, covered in Input Validation and Mass Assignment.
  • Missing checks on nested resources: /projects/5/tasks/9 must verify that task 9 belongs to project 5 and that the user may access project 5.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is BOLA?

    Broken Object Level Authorization means the API checks that you are logged in but not that the object is yours.
  2. 2. Which query is safest for loading a note?

    Scoping the query by owner means another user's row is never loaded, so there is no check to forget.
  3. 3. Where should the current user's ID come from?

    Only the verified token is trustworthy. Anything in the request body or query can be set by the attacker.
  4. 4. Do UUID IDs fix BOLA?

    IDs leak through logs, links and responses. Authorization must not depend on secrecy of an ID.
  5. 5. A route has no permission rule defined. What should happen?

    Deny by default means forgotten routes are closed, not open.

Summary

  • Authentication identifies the caller; authorization decides each action, at function level and object level.
  • BOLA happens when the API trusts an ID without checking ownership; scope queries by owner and take the user from the verified token.
  • Use roles (RBAC) and attributes (ABAC) behind one can(user, action, object) policy and deny by default.
  • UUIDs are defence in depth, not a fix.
  • Return only allowed fields, and check parent-child relations on nested routes.

Next: Input Validation and Mass Assignment