TExcelTAD
Home › Web Application Security: OWASP Top 10, APIs & Authentication › IDOR and BOLA: Object-Level Authorization

IDOR and BOLA: Object-Level Authorization

Broken access control has been number one in the OWASP Top 10 since 2021, and it stays at A01 in the 2025 edition. Its most common form is also the simplest: the user is logged in, the application checks that, and then serves whatever object the request names. Change /api/invoices/1001 to /api/invoices/1002 and you are reading someone else's invoice. In web applications this is called IDOR (insecure direct object reference); the OWASP API Security Top 10 calls it BOLA (broken object level authorization) and ranks it first, because APIs expose object ids everywhere. There is no clever payload: the bug is a missing line of code. That makes it hard for scanners and easy for humans, and this lesson shows both the attack and the detection in logs, then the line.

Definitions

  • Authentication: who is this? Authorization: may this user do this to this object?
  • Object-level authorization: the per-object check, "does invoice 1002 belong to (or is it shared with) the caller?", done on every request that takes an object id.
  • Direct object reference: an id from the client that maps straight to a database key. Not a bug by itself; the missing check is.
  • Horizontal escalation: reaching another user's objects at the same privilege level. Vertical: reaching functions of a higher role (next lesson).

The bug and the fix, on a toy API

The API (started on 127.0.0.1 in your sandbox) authenticates with bearer tokens and serves invoices. The vulnerable handler checks the token and fetches the invoice. The fixed handler adds the ownership condition to the query itself and answers 404 for objects the caller may not see, so the response doesn't even confirm that the id exists. Every request is written to access.log, as a real server would.

bash · try it yourself
cat > api.py <<'PY'
import json, sqlite3, sys
from wsgiref.simple_server import make_server, WSGIRequestHandler

FIXED = sys.argv[1] == "fixed"
TOKENS = {"tok-alice-lab": 1, "tok-bob-lab": 2}
db = sqlite3.connect(":memory:", check_same_thread=False)
db.executescript("""
CREATE TABLE invoices (id INTEGER PRIMARY KEY, owner_id INTEGER, customer TEXT, total REAL);
INSERT INTO invoices VALUES (1001, 1, 'Alice A.', 42.0), (1002, 2, 'Bob B.', 310.5),
                            (1003, 2, 'Bob B.', 18.0), (1004, 3, 'Carol C.', 99.9);
""")

class Logged(WSGIRequestHandler):
    def log_message(self, *args):
        pass

def app(environ, start_response):
    user = TOKENS.get(environ.get("HTTP_AUTHORIZATION", "").removeprefix("Bearer "))
    parts = environ["PATH_INFO"].strip("/").split("/")
    status, body = "404 Not Found", {"error": "not found"}
    if user is None:
        status, body = "401 Unauthorized", {"error": "login"}
    elif len(parts) == 3 and parts[:2] == ["api", "invoices"] and parts[2].isdigit():
        if FIXED:
            row = db.execute("SELECT id, customer, total FROM invoices WHERE id = ? AND owner_id = ?",
                             (int(parts[2]), user)).fetchone()
        else:
            row = db.execute("SELECT id, customer, total FROM invoices WHERE id = ?", (int(parts[2]),)).fetchone()
        if row:
            status, body = "200 OK", dict(zip(("id", "customer", "total"), row))
    with open("access.log", "a") as log:
        log.write(f"user={user} {environ['PATH_INFO']} {status.split()[0]}\n")
    start_response(status, [("Content-Type", "application/json")])
    return [json.dumps(body).encode() + b"\n"]

make_server("127.0.0.1", 8000, app, handler_class=Logged).serve_forever()
PY
get() { curl -s -H "Authorization: Bearer $1" -w ' [%{http_code}]' "http://127.0.0.1:8000$2" | tr -d '\n'; echo; }
for mode in vulnerable fixed; do
  : > access.log
  python3 api.py $mode &
  for i in $(seq 50); do curl -s -o /dev/null http://127.0.0.1:8000/ && break; sleep 0.1; done
  echo "=== $mode"
  printf '  alice, her invoice:   '; get tok-alice-lab /api/invoices/1001
  printf '  alice, bob'"'"'s invoice: '; get tok-alice-lab /api/invoices/1002
  printf '  no token:             '; get none /api/invoices/1001
  echo "  alice walks the ids 1000-1010:"
  : > access.log
  for id in $(seq 1000 1010); do curl -s -o /dev/null -H 'Authorization: Bearer tok-alice-lab' "http://127.0.0.1:8000/api/invoices/$id"; done
  awk '$1 == "user=1" && $2 ~ /^\/api\/invoices\// {n++; if ($3 == 200) ok++} END {printf "    %d requests, %d answered 200\n", n, ok}' access.log
  [ $mode = vulnerable ] && cp access.log walk.log
  kill %1; wait 2>/dev/null
done
echo "=== detection: distinct invoice ids per user in the vulnerable run's log (plus one normal request by bob)"
echo "user=2 /api/invoices/1002 200" >> walk.log
awk '{split($2, p, "/"); seen[$1 " " p[4]] = 1} END {for (k in seen) {split(k, a, " "); c[a[1]]++} for (u in c) printf "  %s touched %d distinct invoices%s\n", u, c[u], (c[u] > 5 ? "  <-- ALERT" : "")}' walk.log | sort
=== vulnerable
  alice, her invoice:   {"id": 1001, "customer": "Alice A.", "total": 42.0} [200]
  alice, bob's invoice: {"id": 1002, "customer": "Bob B.", "total": 310.5} [200]
  no token:             {"error": "login"} [401]
  alice walks the ids 1000-1010:
    11 requests, 4 answered 200
=== fixed
  alice, her invoice:   {"id": 1001, "customer": "Alice A.", "total": 42.0} [200]
  alice, bob's invoice: {"error": "not found"} [404]
  no token:             {"error": "login"} [401]
  alice walks the ids 1000-1010:
    11 requests, 1 answered 200
=== detection: distinct invoice ids per user in the vulnerable run's log (plus one normal request by bob)
  user=1 touched 11 distinct invoices  <-- ALERT
  user=2 touched 1 distinct invoices

In the vulnerable mode alice, authenticated with her own valid token, read Bob's invoice and, by walking the ids, every invoice in the table. Nothing about the requests is malformed: a WAF sees a normal API call. In the fixed mode she reads her own invoice only, and the other ids answer 404 exactly like ids that don't exist. The last part shows the detection signal: one user touching many distinct object ids in a short time, here an awk over the access log with a threshold of five.

How IDOR/BOLA is detected

  • Testing with two accounts (the essential technique): log in as user A and user B in two browsers or two proxy sessions, record every request A makes that contains an id (paths, query strings, JSON bodies, headers, GraphQL arguments), and replay each one with B's credentials. Every 200 for A's object is a finding. Burp's Autorize extension automates the replay; also try no credentials at all.
  • Ids that look unguessable are still tested: UUIDs leak in URLs, e-mails, logs and other API responses. Random ids reduce enumeration, not the missing check.
  • Code review: handlers that load an object by id from the request and use it without a condition on the caller (Invoice.objects.get(id=pk) instead of request.user.invoices.get(id=pk)); authorization done only in the UI by hiding buttons.
  • Logs and monitoring: per user, the number of distinct object ids accessed and the ratio of 403/404 answers; sequential id walks; one tenant's token touching another tenant's ids (if ids embed the tenant).

How it is fixed

  1. Scope every query by the caller: WHERE id = ? AND owner_id = ?, or load through the relationship (current_user.invoices.get(id)), or use row-level security in the database (PostgreSQL CREATE POLICY … USING (owner_id = current_setting('app.user_id')::int)).
  2. Centralise the decision in one authorization function or policy engine (can(user, "read", invoice)) called by every handler, so sharing rules (team members, admins, delegated access) live in one place.
  3. Deny by default: a new endpoint without an explicit policy refuses.
  4. Return 404 for objects the caller can't see (or 403 consistently), and log the denial.
  5. Test it: an automated test per endpoint that user B gets 404 for user A's object.

Pitfalls

  • Checking ownership on read but not on update or delete (PUT /api/invoices/1002 often has its own handler).
  • Trusting an owner_id or account_id in the request body for the check: the attacker sets it.
  • Nested objects: /api/orders/7/items/91 checks that order 7 is yours but not that item 91 belongs to order 7.
  • Exports and bulk endpoints (?ids=1,2,3, CSV export, search) that skip the per-object check.

Practice

1. A mobile API uses GET /api/v2/profile?user_id=812 and the token identifies user 812. What do you test and what is the fix?

Change user_id to another value with the same token. The fix is to ignore the parameter and take the user id from the token (/api/v2/me), or to check that it equals the token's user (or that the caller is an authorised admin).

2. Why did the fixed API answer 404 rather than 403 for Bob's invoice?

A 403 confirms that invoice 1002 exists, which leaks business information (number of invoices, valid ids). 404 for "doesn't exist or not yours" reveals nothing. Either is acceptable if applied consistently; the important part is the check.

Test yourself

What is the root cause of IDOR/BOLA?

  1. Sequential ids
  2. A missing per-object authorization check: the server serves whatever object id the authenticated caller names
  3. Missing HTTPS
  4. SQL injection

Which test finds IDOR most reliably?

  1. A vulnerability scanner with default settings
  2. Replaying user A's requests with user B's credentials
  3. Sending SQL quotes
  4. Checking security headers

In the session, which log signal flagged the attack?

  1. 500 errors
  2. One user touching many distinct invoice ids
  3. Long URLs
  4. Missing tokens

Do random UUIDs instead of sequential ids fix BOLA?

  1. Yes
  2. No: they make guessing harder, but leaked ids still work without an ownership check
  3. Only for GET
  4. Only with JWT