Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท Broken Access Control

IDOR and Object-Level Authorization

Learn how insecure direct object references (IDOR, BOLA) let users read or change other people's data by changing an id, and how to fix them with ownership checks and scoped queries.

An insecure direct object reference (IDOR) appears when an application uses an identifier from the request (an id in the URL, a filename, an account number) to fetch an object, and never checks that the current user is allowed to see it. API security lists call the same flaw BOLA, broken object-level authorization, and it is the most common serious API bug.

The classic scenario: you open /invoices/1042 and see your invoice. Change the number to 1043 and you see someone else's. No hacking tool was needed; only a missing if.

What the vulnerable code looks like

Python
# VULNERABLE: authenticated, but no ownership check
@app.get("/invoices/<int:invoice_id>")
def show_invoice(invoice_id):
    require_login()
    return db.query("SELECT * FROM invoices WHERE id = ?", invoice_id)

The handler asks "is the user logged in?" and stops there. The identifier is a direct reference to a record, and the record's owner is never compared with the caller.

IDORs hide in many places, not just URL paths:

  • Query strings: ?user_id=7, ?file=report_7.pdf
  • JSON bodies: {"account": 7, "amount": 20}
  • Hidden form fields and cookies
  • Download links, export jobs, avatar uploads, password reset or email-change forms
  • Write operations too: changing another user's address, deleting their comment

Reads leak data; writes and deletes corrupt it, so test both.

Seeing it in a simulation

Python A vulnerable endpoint and its fix

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 input()), it gets the next line from this box.

The fix is a scoped query: include the owner in the WHERE clause. The database then never returns rows the caller must not see, and a forgotten if afterwards cannot leak them.

Fix patterns

  1. Scope queries to the user. WHERE id = ? AND owner_id = ?, or in an ORM current_user.invoices().find(id). Prefer this over load-then-check.
  2. Central policy checks. Call a policy such as authorize('view', $invoice) in one shared place for every route.
  3. Use the session, not the request, for identity. Never accept user_id from the client for "my profile" endpoints; read it from the authenticated session.
  4. Return 404 for foreign objects. It does not confirm existence.
  5. Guard every verb. Check GET, PUT, PATCH, DELETE, and bulk or export endpoints.

Unpredictable ids help, but do not fix it

Sequential ids (1, 2, 3) make guessing trivial. Switching to random identifiers such as UUIDs makes enumeration harder and avoids leaking business size ("we have 50,000 customers"). But it is only defense in depth: an id can still leak through a link, a log, a referrer header or a shared screenshot, and then anyone holding it could read the object. The ownership check is the real fix.

Python Random ids do not replace checks

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 input()), it gets the next line from this box.

Mass assignment: a close cousin

If an endpoint copies every field of the request into a database record, a user can send extra fields such as owner_id or is_admin and change data they should not.

Python
# VULNERABLE: whatever fields arrive are saved
user.update(request.json)

# SAFER: allow-list the fields a user may change
ALLOWED = {"display_name", "bio"}
user.update({k: v for k, v in request.json.items() if k in ALLOWED})

Frameworks offer built-in allow-lists ($fillable in Laravel, strong parameters in Rails, DTO classes in Spring). Use them.

Python Mass assignment demo

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 input()), it gets the next line from this box.

Testing for IDOR in your own app

Use two test accounts, A and B. Perform an action as A (view, edit, delete an object), capture the request, then replay it with B's session. If B succeeds, you have found a hole. Automate this as integration tests: every endpoint that takes an object id gets a test that a second user is refused.

sequenceDiagram
  participant A as User A
  participant B as User B
  participant S as Server
  A->>S: GET /invoices/1042 (A's session)
  S-->>A: 200 invoice
  B->>S: GET /invoices/1042 (B's session)
  S-->>B: must be 404, not 200

The replay test: the same request with another user's session must fail.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the root cause of an IDOR?

    The object is reachable by id and no ownership or permission check ties it to the caller.
  2. 2. Which is the most robust fix?

    Authorization must be enforced on the server. Scoped queries and policies make it automatic.
  3. 3. Why are UUIDs not a complete fix?

    Random ids reduce guessing but are not an access control. Leaked ids still need an ownership check.
  4. 4. A profile update copies all JSON fields into the user record. What can go wrong?

    Without an allow-list, unexpected fields change protected attributes.

Summary

  • IDOR/BOLA happens when an object is fetched by a client-supplied id with no ownership or permission check.
  • It appears in URLs, bodies, cookies and write operations, not just page links.
  • Fix it with scoped queries, central policies, session-based identity and 404 for foreign objects.
  • Random ids and UUIDs help but never replace authorization.
  • Allow-list updatable fields to prevent mass assignment, and test every endpoint with a second user.

Next: Privilege Escalation and Function-Level Access