IDOR
Logged in is not the same as allowed to see this one
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.
// 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);
});// The tenant is part of the lookup, not a check you might forget after it.
app.get('/invoices/:id', requireLogin, async (req, res) => {
const inv = await db.invoice.findOne({ id: req.params.id, tenantId: req.user.tenantId });
if (!inv) return res.sendStatus(404); // same answer for 'not yours' and 'not there'
res.json(inv);
});
app.patch('/invoices/:id', requireLogin, async (req, res) => {
const { payee, amount } = req.body; // allowlist: never tenantId, id, createdBy
const inv = await db.invoice.update({ id: req.params.id, tenantId: req.user.tenantId }, { payee, amount });
if (!inv) return res.sendStatus(404);
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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Authentication and authorisation for a multi-tenant SaaS and its public APISecurity
- Kubernetes and edge security: clusters, containers, CDN and WAFSecurity
- Running a security incident: command, customer updates, postmortems and tabletopsSecurity
- Secure code review and AppSecSecurity
- Security review when AI agents write the codeSecurity
- Triaging researcher reports: running a disclosure programme day to daySecurity
- Security testing: fuzzing, scanners and abuse casesSoftware Testing and Quality