lessons/authz-idor.md

Lesson 2 of 18

Authentication is not authorisation

A valid session proves who is calling. It does not prove the caller may act on this object.

Remember: Every ID from the client is a claim. Check it against the authenticated caller in the same query that uses it.

Authentication answers “who is calling?”. Authorisation answers “may this caller act on this specific object?”. A valid session proves identity; it says nothing about the account ID in the URL.

IDOR and BOLA

An Insecure Direct Object Reference (IDOR), called Broken Object Level Authorisation (BOLA) in the OWASP API Security Top 10, happens when an ID from the request selects a record and nothing checks that the record belongs to the caller.

const account = await db.account.findUnique({ where: { id: accountId } });
Vulnerable: the owner is never compared with the session.
const account = await db.account.findFirst({
where: { id: accountId, ownerId: session.userId },
});
Fixed: the owner is part of the query. Return 404 for both “not found” and “not yours”.

Why the owner goes in the query

If you read the record and check ownership in a separate step, it is easy to forget the check in one branch. Putting the owner in the same query means other users' records simply do not exist for the caller.

Function-level access (BFLA)

The same idea applies to actions. An admin-only endpoint must check the role on the server. Hiding a button, or a middleware that only checks that a cookie exists, is not a control.

UUIDs make IDs hard to guess, but they are not authorisation. IDs leak through logs, shared links and responses.

Questions to ask yourself

  1. Where does the owner of this record come from?
  2. Is the check on the server, and in the same query?
  3. What does a non-owner receive: a 403, a 404, or the data?

Practise this