IDOR

Logged in is not the same as allowed to see this one

L216 MINSECURITY
PREDICT

Your API checks the session on every request, then loads /invoices/:id by id. A colleague proposes switching the ids from 101, 102, 103 to random UUIDs. After that change, can a logged-in customer from another company read your customer's invoice?

Most handlers answer two questions. Who is calling? That is authentication: a session, a token. May this caller touch this object? That is object-level authorisation, and it is the one that gets left out. The login check passes, the handler loads the row by the id in the URL, and the row goes back to whoever asked. OWASP calls this broken object-level authorisation; the older name is IDOR, insecure direct object reference.

The fix is to make the caller part of the lookup: WHERE id = ? AND tenant_id = ?. A foreign id then looks exactly like a missing one. Writes need the same filter, plus an allowlist of fields, or a body containing tenantId moves an object into someone else’s account.

▶ SIMULATION
ACME (alice, bob)
reads: —
pays: —
INVOICES TABLE
101 · acme · Acme payroll, acct 0112
102 · acme · Acme rent, acct 0457
103 · acme · Acme tax, acct 0901
104 · globex · Globex hosting, acct 7788
lookup: id only · ids: sequential
GLOBEX (mallory)
reads: —
refused (404): 0
last—
✓ no response hands a caller another tenant's invoice✓ no one writes to another tenant's invoice or changes an invoice's tenant
SCHEDULE
01[API]API: checks login, then loads the invoice by id
02[ALICE]alice GET /invoices/101 (her own)
03[MALLORY]mallory GET /invoices/104 (her own)
04[MALLORY]mallory changes 104 to 103 in the URL
05[MALLORY]mallory loops: 102, 101
06[API]report
Start of break: read another tenant scenario. 6 steps.
// Authenticated, but not authorised for THIS object.
app.get('/invoices/:id', requireLogin, async (req, res) => {
  const inv = await db.invoice.findOne({ id: req.params.id });
  if (!inv) return res.sendStatus(404);
  res.json(inv);
});

app.patch('/invoices/:id', requireLogin, async (req, res) => {
  const inv = await db.invoice.update({ id: req.params.id }, req.body); // any field
  res.json(inv);
});

Watch mallory’s reads and the INVOICES TABLE. In READ, every id she tries comes back with acme’s data. In WRITE + MASS ASSIGNMENT, a payee turns red with her account number and alice pays it. In RANDOM IDS, guessing fails and the leaked link still works. In FIX, every foreign id gets a 404 and both invariants stay green.

BREAK IT NOT YET

The API checks that the caller is logged in and then loads the invoice by id. Insert one request from mallory, before alice pays, so that alice ends up paying money into mallory's account (acct 6666).

GOAL: alice pays an invoice with mallory's account on it

FIX IT NOT YET

Write the two handlers. caller is { userId, tenantId } or null when not logged in. Any user in a tenant may read and update that tenant's invoices. Return the invoice, or null for "not found" (use the same answer for a missing invoice and someone else's, so ids cannot be probed). An update may change payee, amount and memo, nothing else. The grader tries your own invoice, a colleague's, every other id in sequence, a random-looking id, and updates that try to move an invoice to another tenant.

Mental model:

Authentication tells you who is asking; it says nothing about which object they may touch. Put the caller’s tenant in the query for every read and every write, return the same 404 for “not yours” and “not there”, and copy only the fields a customer may change. An unguessable id slows an attacker down; it never authorises anyone.

IN PRODUCTION

Every multi-tenant API: GET /invoices/:id, PATCH /projects/:id, file download links, GraphQL node(id) resolvers, mobile apps that call the API with ids from the screen. Broken object-level authorisation is item one (API1:2023) on the OWASP API Security Top 10, which asks for the check in every endpoint that receives an object id. A scanner cannot tell whose object is whose, so finding it takes two test accounts in two tenants. Tenant leaks through caches, jobs and search indexes, where every handler is correct, are the next lesson, tenant isolation.

IN INTERVIEWS

“How would you stop one customer reading another's data through your API?” Separate authentication from object-level authorisation, put the tenant in the query rather than a check after the load, cover writes and mass assignment as well as reads, explain why UUIDs are not authorisation, and say how you would test it: two accounts in two tenants, and every endpoint replayed with the other account's ids.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: SSRF →