GET /invoices/{id} checks login. Why can another tenant read it?
Question
A multi-tenant billing API exposes GET /invoices/{id}. A logged-in user (user_101, tenant acme) calls get_invoice("invoice_9002", session) and receives beta's invoice with HTTP 200.
- Why did the login check miss this?
- Fix
get_invoiceso the data lookup enforces tenant ownership and blocks cross-tenant reads. - Would switching invoice IDs from
invoice_9002to random UUIDs fix the flaw?
INVOICES = {
"invoice_9001": {"tenant": "acme", "total": 420},
"invoice_9002": {"tenant": "beta", "total": 980},
}
def get_invoice(invoice_id, session):
if not session or not session.get("user_id"):
return 401, None
inv = INVOICES.get(invoice_id)
if inv is None:
return 404, None
return 200, invShow hints (3)
- "LOGIN VERSUS OWNERSHIP"
The handler checks that
sessionhas auser_id(authentication). Does it check whethersession["tenant"]ownsinvoice_id(authorization)? - "SCOPE THE LOOKUP"
Match both
invoice_idandsession["tenant"]when fetching the record, and reject any missing tenant or mismatch. - "GUESSING VERSUS PERMISSION"
Random UUIDs make guessing IDs harder, yet any leaked link, ticket, or log line still works if the server skips the tenant check.
Show checked explanation
The handler checks authentication (who you are) but skips object-level authorization (what you may read). Scope the lookup by both invoice_id and session["tenant"], and deny mismatches or missing tenants. Random UUIDs alone never fix this, because anyone with a leaked ID can still fetch the record.
INVOICES = {
"invoice_9001": {"tenant": "acme", "total": 420},
"invoice_9002": {"tenant": "beta", "total": 980},
}
def get_invoice(invoice_id, session):
if not session or not session.get("user_id"):
return 401, None
tenant = session.get("tenant")
inv = INVOICES.get(invoice_id)
if not tenant or inv is None or inv["tenant"] != tenant:
return 404, None
return 200, inv
acme = {"user_id": "user_101", "tenant": "acme"}
for iid in ("invoice_9001", "invoice_9002", "invoice_9999"):
print(iid, "->", get_invoice(iid, acme))
invoice_9001 -> (200, {'tenant': 'acme', 'total': 420})
invoice_9002 -> (404, None)
invoice_9999 -> (404, None)Code run on CPython 3.12.10 against synthetic tenant fixtures and an in-memory SQLite tenant-scoped query; official OWASP links checked. Checked by an editor other than the author.
Walkthrough
This bug is Broken Object Level Authorization (BOLA, listed as API1:2023 in the OWASP API Security Top 10, and often called IDOR). user_101 has a valid login, so the 401 guard passes. Because get_invoice looks up invoice_9002 by ID alone, acme reads beta's billing data.
| requested ID | starter (acme) | fixed (acme) |
|---|---|---|
invoice_9001 (acme) | 200, total 420 | 200, total 420 |
invoice_9002 (beta) | 200, total 980 (leak) | 404, None |
invoice_9999 (missing) | 404, None | 404, None |
Enforce ownership in the data query itself. In SQL, write SELECT id, total FROM invoices WHERE id = ? AND tenant_id = ? and bind tenant_id from the authenticated server session. Never read tenant_id from the request URL, headers, or JSON body. If session has no tenant, fail closed.
404 vs 403 trade-off. Notice the fixed function returns 404 for both invoice_9002 and invoice_9999. Across customer tenants, returning 404 hides whether an invoice ID exists in another company's account. Returning 403 tells the caller the record exists, which is useful only inside one shared team workspace where callers need a clear permission message.
Why UUIDs are not enough. Switching invoice_9002 to a random UUID stops simple sequential guessing, which is useful defense in depth. It is not access control. UUIDs still appear in browser history, Referer headers, support emails, and shared reports. Once another logged-in user sees a UUID, a server without an ownership check hands over the invoice.
Common mistakes
- Replacing sequential IDs with random UUIDs and skipping the tenant check. Any leaked link or copied UUID still exposes another customer's record.
- Reading
tenant_idfrom a query parameter or JSON body instead of the server-side session. Any caller can sendtenant_id=betaand pass the check. - Adding the ownership check to
GET /invoices/{id}while forgettingPATCH,DELETE, or/invoices/{id}/pdfroutes that query the same table. - Returning
403for another tenant's private ID and404for a missing ID. That difference lets any signed-in user probe which IDs exist.
Follow-up questions
- "When should an API return 403 instead of 404 for an unauthorized object?" Return
404across separate customer tenants so callers cannot confirm whether a private ID exists. Inside one organization's workspace, where members already see shared project IDs,403gives clearer feedback when someone lacks edit rights. - "How do you stop developers from forgetting the tenant filter on new endpoints?" Require a tenant context inside a shared repository helper or enforce database Row-Level Security so queries cannot omit
tenant_id. Add automated tests that call every route as a second synthetic tenant and assert404or403. - "If the route is
/tenants/{tenant_id}/invoices/{invoice_id}, is checkingtenant_idagainst the session enough?" No. A caller can put their owntenant_idin the path together with another tenant'sinvoice_id. The query must still filter by bothinvoice_idand the session'stenant_id.
What a strong answer covers
Separates authentication from object-level authorization (BOLA/IDOR) and scopes lookups by session tenant and object ID. Weighs 404 vs 403 existence leaks, and explains why UUIDs alone are not access control.
Open in interactive practice deck →